File size: 3,877 Bytes
c25699c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
"""Extract structural metadata from a .glb by parsing only its JSON chunk.

GLB layout: 12-byte header ('glTF', version, length) then chunks
(uint32 length, uint32 type, data). Chunk 0 is JSON, so we never touch the
binary payload — probing 23 GB of assets costs only a few hundred MB of reads.
"""
import json, os, struct, sys

def read_gltf_json(path, max_json=64 << 20):
    with open(path, "rb") as f:
        head = f.read(12)
        if len(head) < 12 or head[:4] != b"glTF":
            raise ValueError("not a GLB")
        ver, total = struct.unpack("<II", head[4:12])
        ch = f.read(8)
        if len(ch) < 8:
            raise ValueError("truncated")
        clen, ctype = struct.unpack("<II", ch)
        if ctype != 0x4E4F534A:            # 'JSON'
            raise ValueError("first chunk not JSON")
        if clen > max_json:
            raise ValueError("json chunk too large: %d" % clen)
        raw = f.read(clen)
        if len(raw) < clen:
            raise ValueError("truncated json chunk")
    return ver, json.loads(raw.decode("utf-8", "replace"))


def probe(path):
    ver, g = read_gltf_json(path)
    nodes = g.get("nodes", []) or []
    skins = g.get("skins", []) or []
    anims = g.get("animations", []) or []
    accs  = g.get("accessors", []) or []
    meshes= g.get("meshes", []) or []

    # parent map from node children
    parent = {}
    for i, n in enumerate(nodes):
        for c in (n.get("children") or []):
            parent[c] = i

    joints = set()
    per_skin = []
    for s in skins:
        js = s.get("joints") or []
        per_skin.append(len(js))
        joints.update(js)

    # kinematic-tree roots among joints: a joint whose parent is not a joint
    roots = [j for j in joints if parent.get(j) not in joints]

    # animation stats: max time from each sampler's input accessor max
    dur = 0.0
    channels = 0
    keyframes = 0
    anim_names = []
    for a in anims:
        anim_names.append(a.get("name") or "")
        channels += len(a.get("channels") or [])
        for smp in (a.get("samplers") or []):
            ai = smp.get("input")
            if ai is None or ai >= len(accs):
                continue
            acc = accs[ai]
            keyframes = max(keyframes, acc.get("count", 0) or 0)
            mx = acc.get("max")
            if isinstance(mx, list) and mx:
                try: dur = max(dur, float(mx[0]))
                except (TypeError, ValueError): pass

    # does any animation actually drive a joint?
    animated_nodes = set()
    for a in anims:
        for c in (a.get("channels") or []):
            t = (c.get("target") or {}).get("node")
            if t is not None: animated_nodes.add(t)
    animated_joints = len(animated_nodes & joints)

    verts = 0
    for m in meshes:
        for p in (m.get("primitives") or []):
            ai = (p.get("attributes") or {}).get("POSITION")
            if ai is not None and ai < len(accs):
                verts += accs[ai].get("count", 0) or 0

    gen = (g.get("asset") or {}).get("generator", "")
    return {
        "gltf_version": ver,
        "generator": gen[:80],
        "num_nodes": len(nodes),
        "num_meshes": len(meshes),
        "num_vertices": verts,
        "num_skins": len(skins),
        "num_joints": len(joints),
        "max_skin_joints": max(per_skin) if per_skin else 0,
        "num_skeleton_roots": len(roots),
        "num_animations": len(anims),
        "num_anim_channels": channels,
        "animated_joints": animated_joints,
        "max_keyframes": keyframes,
        "duration_sec": round(dur, 4),
        "anim_names": "|".join(n for n in anim_names if n)[:200],
    }

if __name__ == "__main__":
    for p in sys.argv[1:]:
        try: print(os.path.basename(p), json.dumps(probe(p)))
        except Exception as e: print(os.path.basename(p), "ERROR", e)