| |
| """Build the dataset indexes: walk every .glb once and write both CSVs. |
| |
| Reads only each file's glTF JSON chunk, so the whole collection is probed in |
| seconds regardless of total size. |
| |
| python3 build_dataset.py [--src DIR] [--link] |
| |
| --link additionally hardlinks the source assets into ../glb/. |
| """ |
| import argparse, csv, json, os, re, shutil, sys, time |
| HERE = os.path.dirname(os.path.abspath(__file__)); sys.path.insert(0, HERE) |
| from glb_probe import read_gltf_json, probe |
|
|
| ROOT = os.path.dirname(HERE) |
| XL = re.compile(r"^([0-9a-f]{24})_(fbx|glb|gltf)\.glb$") |
| SKFB = re.compile(r"^([0-9a-f]{32})\.glb$") |
|
|
| ASSET_COLS = ["file", "object_id", "id_family", "source_format", "size_bytes", |
| "num_vertices", "num_meshes", "num_nodes", "num_joints", |
| "num_skeleton_roots", "single_tree", "num_animations", |
| "total_duration_sec", "max_keyframes", "animated_joints", "generator"] |
| CLIP_COLS = ["file", "object_id", "clip_index", "clip_name", "duration_sec", |
| "keyframes", "num_channels", "animated_nodes", "animated_joints", |
| "drives_skeleton"] |
|
|
|
|
| def identify(fname): |
| m = XL.match(fname) |
| if m: |
| return m.group(1), m.group(2), "objaverse_xl" |
| s = SKFB.match(fname) |
| if s: |
| return s.group(1), "", "objaverse_sketchfab" |
| return os.path.splitext(fname)[0], "", "unknown" |
|
|
|
|
| def clips_of(gltf, joints): |
| accs = gltf.get("accessors", []) or [] |
| for i, a in enumerate(gltf.get("animations", []) or []): |
| dur = kf = 0 |
| for smp in (a.get("samplers") or []): |
| k = smp.get("input") |
| if k is None or k >= len(accs): |
| continue |
| acc = accs[k] |
| kf = max(kf, 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 |
| tgt = {(c.get("target") or {}).get("node") for c in (a.get("channels") or [])} |
| tgt.discard(None) |
| yield {"clip_index": i, "clip_name": (a.get("name") or "")[:120], |
| "duration_sec": round(dur, 4), "keyframes": kf, |
| "num_channels": len(a.get("channels") or []), |
| "animated_nodes": len(tgt), "animated_joints": len(tgt & joints)} |
|
|
|
|
| def main(): |
| ap = argparse.ArgumentParser() |
| ap.add_argument("--src", default=os.path.join(ROOT, "glb")) |
| ap.add_argument("--link", action="store_true", help="hardlink --src assets into ../glb/") |
| args = ap.parse_args() |
|
|
| if args.link and os.path.abspath(args.src) != os.path.abspath(os.path.join(ROOT, "glb")): |
| dst = os.path.join(ROOT, "glb"); os.makedirs(dst, exist_ok=True) |
| for f in sorted(os.listdir(args.src)): |
| if f.endswith(".glb") and not os.path.exists(os.path.join(dst, f)): |
| try: os.link(os.path.join(args.src, f), os.path.join(dst, f)) |
| except OSError: shutil.copy2(os.path.join(args.src, f), os.path.join(dst, f)) |
| args.src = dst |
|
|
| files = sorted(f for f in os.listdir(args.src) if f.endswith(".glb")) |
| t0 = time.time(); assets = []; clips = []; failed = [] |
| for n, f in enumerate(files, 1): |
| path = os.path.join(args.src, f) |
| oid, fmt, fam = identify(f) |
| try: |
| info = probe(path) |
| _, gltf = read_gltf_json(path) |
| except Exception as e: |
| failed.append((f, str(e)[:80])); continue |
| joints = set() |
| for s in (gltf.get("skins", []) or []): |
| joints.update(s.get("joints") or []) |
| cs = list(clips_of(gltf, joints)) |
| for c in cs: |
| c.update(file="glb/" + f, object_id=oid, |
| drives_skeleton=str(c["animated_joints"] > 0).lower()) |
| clips.append(c) |
| assets.append({ |
| "file": "glb/" + f, "object_id": oid, "id_family": fam, "source_format": fmt, |
| "size_bytes": os.path.getsize(path), |
| "num_vertices": info["num_vertices"], "num_meshes": info["num_meshes"], |
| "num_nodes": info["num_nodes"], |
| "num_joints": info["num_joints"], "num_skeleton_roots": info["num_skeleton_roots"], |
| "single_tree": str(info["num_skeleton_roots"] == 1).lower(), |
| "num_animations": info["num_animations"], |
| "total_duration_sec": round(sum(c["duration_sec"] for c in cs), 4), |
| "max_keyframes": info["max_keyframes"], "animated_joints": info["animated_joints"], |
| "generator": info["generator"], |
| }) |
| if n % 2000 == 0: |
| print(" %d/%d %.0fs" % (n, len(files), time.time() - t0), flush=True) |
|
|
| for name, cols, rows in (("metadata.csv", ASSET_COLS, assets), |
| ("animations.csv", CLIP_COLS, clips)): |
| with open(os.path.join(ROOT, name), "w", newline="", encoding="utf-8") as fh: |
| w = csv.DictWriter(fh, fieldnames=cols); w.writeheader() |
| w.writerows({k: r.get(k, "") for k in cols} for r in rows) |
| print("%-15s -> %d rows" % (name, len(rows))) |
| if failed: |
| print("failed: %d" % len(failed)) |
| for f, e in failed[:5]: print(" ", f, e) |
| print("%.0fs" % (time.time() - t0)) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|