Datasets:
Add dataset card, copyright notice, indexes and tooling
Browse files- scripts/glb_probe.py +110 -0
scripts/glb_probe.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Extract structural metadata from a .glb by parsing only its JSON chunk.
|
| 3 |
+
|
| 4 |
+
GLB layout: 12-byte header ('glTF', version, length) then chunks
|
| 5 |
+
(uint32 length, uint32 type, data). Chunk 0 is JSON, so we never touch the
|
| 6 |
+
binary payload — probing 23 GB of assets costs only a few hundred MB of reads.
|
| 7 |
+
"""
|
| 8 |
+
import json, os, struct, sys
|
| 9 |
+
|
| 10 |
+
def read_gltf_json(path, max_json=64 << 20):
|
| 11 |
+
with open(path, "rb") as f:
|
| 12 |
+
head = f.read(12)
|
| 13 |
+
if len(head) < 12 or head[:4] != b"glTF":
|
| 14 |
+
raise ValueError("not a GLB")
|
| 15 |
+
ver, total = struct.unpack("<II", head[4:12])
|
| 16 |
+
ch = f.read(8)
|
| 17 |
+
if len(ch) < 8:
|
| 18 |
+
raise ValueError("truncated")
|
| 19 |
+
clen, ctype = struct.unpack("<II", ch)
|
| 20 |
+
if ctype != 0x4E4F534A: # 'JSON'
|
| 21 |
+
raise ValueError("first chunk not JSON")
|
| 22 |
+
if clen > max_json:
|
| 23 |
+
raise ValueError("json chunk too large: %d" % clen)
|
| 24 |
+
raw = f.read(clen)
|
| 25 |
+
if len(raw) < clen:
|
| 26 |
+
raise ValueError("truncated json chunk")
|
| 27 |
+
return ver, json.loads(raw.decode("utf-8", "replace"))
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def probe(path):
|
| 31 |
+
ver, g = read_gltf_json(path)
|
| 32 |
+
nodes = g.get("nodes", []) or []
|
| 33 |
+
skins = g.get("skins", []) or []
|
| 34 |
+
anims = g.get("animations", []) or []
|
| 35 |
+
accs = g.get("accessors", []) or []
|
| 36 |
+
meshes= g.get("meshes", []) or []
|
| 37 |
+
|
| 38 |
+
# parent map from node children
|
| 39 |
+
parent = {}
|
| 40 |
+
for i, n in enumerate(nodes):
|
| 41 |
+
for c in (n.get("children") or []):
|
| 42 |
+
parent[c] = i
|
| 43 |
+
|
| 44 |
+
joints = set()
|
| 45 |
+
per_skin = []
|
| 46 |
+
for s in skins:
|
| 47 |
+
js = s.get("joints") or []
|
| 48 |
+
per_skin.append(len(js))
|
| 49 |
+
joints.update(js)
|
| 50 |
+
|
| 51 |
+
# kinematic-tree roots among joints: a joint whose parent is not a joint
|
| 52 |
+
roots = [j for j in joints if parent.get(j) not in joints]
|
| 53 |
+
|
| 54 |
+
# animation stats: max time from each sampler's input accessor max
|
| 55 |
+
dur = 0.0
|
| 56 |
+
channels = 0
|
| 57 |
+
keyframes = 0
|
| 58 |
+
anim_names = []
|
| 59 |
+
for a in anims:
|
| 60 |
+
anim_names.append(a.get("name") or "")
|
| 61 |
+
channels += len(a.get("channels") or [])
|
| 62 |
+
for smp in (a.get("samplers") or []):
|
| 63 |
+
ai = smp.get("input")
|
| 64 |
+
if ai is None or ai >= len(accs):
|
| 65 |
+
continue
|
| 66 |
+
acc = accs[ai]
|
| 67 |
+
keyframes = max(keyframes, acc.get("count", 0) or 0)
|
| 68 |
+
mx = acc.get("max")
|
| 69 |
+
if isinstance(mx, list) and mx:
|
| 70 |
+
try: dur = max(dur, float(mx[0]))
|
| 71 |
+
except (TypeError, ValueError): pass
|
| 72 |
+
|
| 73 |
+
# does any animation actually drive a joint?
|
| 74 |
+
animated_nodes = set()
|
| 75 |
+
for a in anims:
|
| 76 |
+
for c in (a.get("channels") or []):
|
| 77 |
+
t = (c.get("target") or {}).get("node")
|
| 78 |
+
if t is not None: animated_nodes.add(t)
|
| 79 |
+
animated_joints = len(animated_nodes & joints)
|
| 80 |
+
|
| 81 |
+
verts = 0
|
| 82 |
+
for m in meshes:
|
| 83 |
+
for p in (m.get("primitives") or []):
|
| 84 |
+
ai = (p.get("attributes") or {}).get("POSITION")
|
| 85 |
+
if ai is not None and ai < len(accs):
|
| 86 |
+
verts += accs[ai].get("count", 0) or 0
|
| 87 |
+
|
| 88 |
+
gen = (g.get("asset") or {}).get("generator", "")
|
| 89 |
+
return {
|
| 90 |
+
"gltf_version": ver,
|
| 91 |
+
"generator": gen[:80],
|
| 92 |
+
"num_nodes": len(nodes),
|
| 93 |
+
"num_meshes": len(meshes),
|
| 94 |
+
"num_vertices": verts,
|
| 95 |
+
"num_skins": len(skins),
|
| 96 |
+
"num_joints": len(joints),
|
| 97 |
+
"max_skin_joints": max(per_skin) if per_skin else 0,
|
| 98 |
+
"num_skeleton_roots": len(roots),
|
| 99 |
+
"num_animations": len(anims),
|
| 100 |
+
"num_anim_channels": channels,
|
| 101 |
+
"animated_joints": animated_joints,
|
| 102 |
+
"max_keyframes": keyframes,
|
| 103 |
+
"duration_sec": round(dur, 4),
|
| 104 |
+
"anim_names": "|".join(n for n in anim_names if n)[:200],
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
for p in sys.argv[1:]:
|
| 109 |
+
try: print(os.path.basename(p), json.dumps(probe(p)))
|
| 110 |
+
except Exception as e: print(os.path.basename(p), "ERROR", e)
|