Linzhan commited on
Commit
5b4bd51
·
verified ·
1 Parent(s): c25699c

Add dataset card, copyright notice, indexes and tooling

Browse files
Files changed (1) hide show
  1. scripts/build_dataset.py +120 -0
scripts/build_dataset.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Build the dataset indexes: walk every .glb once and write both CSVs.
3
+
4
+ Reads only each file's glTF JSON chunk, so the whole collection is probed in
5
+ seconds regardless of total size.
6
+
7
+ python3 build_dataset.py [--src DIR] [--link]
8
+
9
+ --link additionally hardlinks the source assets into ../glb/.
10
+ """
11
+ import argparse, csv, json, os, re, shutil, sys, time
12
+ HERE = os.path.dirname(os.path.abspath(__file__)); sys.path.insert(0, HERE)
13
+ from glb_probe import read_gltf_json, probe
14
+
15
+ ROOT = os.path.dirname(HERE)
16
+ XL = re.compile(r"^([0-9a-f]{24})_(fbx|glb|gltf)\.glb$")
17
+ SKFB = re.compile(r"^([0-9a-f]{32})\.glb$")
18
+
19
+ ASSET_COLS = ["file", "object_id", "id_family", "source_format", "size_bytes",
20
+ "num_vertices", "num_meshes", "num_nodes", "num_skins", "num_joints",
21
+ "num_skeleton_roots", "single_tree", "num_animations",
22
+ "total_duration_sec", "max_keyframes", "animated_joints", "generator"]
23
+ CLIP_COLS = ["file", "object_id", "clip_index", "clip_name", "duration_sec",
24
+ "keyframes", "num_channels", "animated_nodes", "animated_joints",
25
+ "drives_skeleton"]
26
+
27
+
28
+ def identify(fname):
29
+ m = XL.match(fname)
30
+ if m:
31
+ return m.group(1), m.group(2), "objaverse_xl"
32
+ s = SKFB.match(fname)
33
+ if s:
34
+ return s.group(1), "", "objaverse_sketchfab"
35
+ return os.path.splitext(fname)[0], "", "unknown"
36
+
37
+
38
+ def clips_of(gltf, joints):
39
+ accs = gltf.get("accessors", []) or []
40
+ for i, a in enumerate(gltf.get("animations", []) or []):
41
+ dur = kf = 0
42
+ for smp in (a.get("samplers") or []):
43
+ k = smp.get("input")
44
+ if k is None or k >= len(accs):
45
+ continue
46
+ acc = accs[k]
47
+ kf = max(kf, acc.get("count", 0) or 0)
48
+ mx = acc.get("max")
49
+ if isinstance(mx, list) and mx:
50
+ try: dur = max(dur, float(mx[0]))
51
+ except (TypeError, ValueError): pass
52
+ tgt = {(c.get("target") or {}).get("node") for c in (a.get("channels") or [])}
53
+ tgt.discard(None)
54
+ yield {"clip_index": i, "clip_name": (a.get("name") or "")[:120],
55
+ "duration_sec": round(dur, 4), "keyframes": kf,
56
+ "num_channels": len(a.get("channels") or []),
57
+ "animated_nodes": len(tgt), "animated_joints": len(tgt & joints)}
58
+
59
+
60
+ def main():
61
+ ap = argparse.ArgumentParser()
62
+ ap.add_argument("--src", default=os.path.join(ROOT, "glb"))
63
+ ap.add_argument("--link", action="store_true", help="hardlink --src assets into ../glb/")
64
+ args = ap.parse_args()
65
+
66
+ if args.link and os.path.abspath(args.src) != os.path.abspath(os.path.join(ROOT, "glb")):
67
+ dst = os.path.join(ROOT, "glb"); os.makedirs(dst, exist_ok=True)
68
+ for f in sorted(os.listdir(args.src)):
69
+ if f.endswith(".glb") and not os.path.exists(os.path.join(dst, f)):
70
+ try: os.link(os.path.join(args.src, f), os.path.join(dst, f))
71
+ except OSError: shutil.copy2(os.path.join(args.src, f), os.path.join(dst, f))
72
+ args.src = dst
73
+
74
+ files = sorted(f for f in os.listdir(args.src) if f.endswith(".glb"))
75
+ t0 = time.time(); assets = []; clips = []; failed = []
76
+ for n, f in enumerate(files, 1):
77
+ path = os.path.join(args.src, f)
78
+ oid, fmt, fam = identify(f)
79
+ try:
80
+ info = probe(path)
81
+ _, gltf = read_gltf_json(path)
82
+ except Exception as e:
83
+ failed.append((f, str(e)[:80])); continue
84
+ joints = set()
85
+ for s in (gltf.get("skins", []) or []):
86
+ joints.update(s.get("joints") or [])
87
+ cs = list(clips_of(gltf, joints))
88
+ for c in cs:
89
+ c.update(file="glb/" + f, object_id=oid,
90
+ drives_skeleton=str(c["animated_joints"] > 0).lower())
91
+ clips.append(c)
92
+ assets.append({
93
+ "file": "glb/" + f, "object_id": oid, "id_family": fam, "source_format": fmt,
94
+ "size_bytes": os.path.getsize(path),
95
+ "num_vertices": info["num_vertices"], "num_meshes": info["num_meshes"],
96
+ "num_nodes": info["num_nodes"], "num_skins": info["num_skins"],
97
+ "num_joints": info["num_joints"], "num_skeleton_roots": info["num_skeleton_roots"],
98
+ "single_tree": str(info["num_skeleton_roots"] == 1).lower(),
99
+ "num_animations": info["num_animations"],
100
+ "total_duration_sec": round(sum(c["duration_sec"] for c in cs), 4),
101
+ "max_keyframes": info["max_keyframes"], "animated_joints": info["animated_joints"],
102
+ "generator": info["generator"],
103
+ })
104
+ if n % 2000 == 0:
105
+ print(" %d/%d %.0fs" % (n, len(files), time.time() - t0), flush=True)
106
+
107
+ for name, cols, rows in (("metadata.csv", ASSET_COLS, assets),
108
+ ("animations.csv", CLIP_COLS, clips)):
109
+ with open(os.path.join(ROOT, name), "w", newline="", encoding="utf-8") as fh:
110
+ w = csv.DictWriter(fh, fieldnames=cols); w.writeheader()
111
+ w.writerows({k: r.get(k, "") for k in cols} for r in rows)
112
+ print("%-15s -> %d rows" % (name, len(rows)))
113
+ if failed:
114
+ print("failed: %d" % len(failed))
115
+ for f, e in failed[:5]: print(" ", f, e)
116
+ print("%.0fs" % (time.time() - t0))
117
+
118
+
119
+ if __name__ == "__main__":
120
+ main()