| |
| """Read a binary FBX (Kaydara 7.x): skeleton, animation length and root joint. |
| |
| Walks the node tree, collecting LimbNode/Root models as joints and the longest |
| KeyTime track as the timeline, then reads the `Connections` block to find which |
| joint sits at the top of the hierarchy. Frame rate is measured from the key |
| times themselves rather than assumed. Self-contained; no external deps. |
| """ |
| import os, struct, sys, zlib |
|
|
| SCALAR = {"Y": 2, "C": 1, "I": 4, "F": 4, "D": 8, "L": 8} |
| ELEM = {"f": 4, "d": 8, "l": 8, "i": 4, "b": 1} |
| FBX_KTIME = 46186158000 |
|
|
|
|
| def _parse(path): |
| d = open(path, "rb").read() |
| if d[:21] != b"Kaydara FBX Binary \x00": |
| raise ValueError("not a binary FBX") |
| version = struct.unpack("<I", d[23:27])[0] |
| u64 = version >= 7500 |
| NREC = 25 if u64 else 13 |
| joints, keytimes = [], [] |
| models, links = {}, [] |
| animated = [] |
|
|
| def rd_prop(p): |
| t = chr(d[p]); s = p; p += 1 |
| if t in SCALAR: |
| p += SCALAR[t]; return p, (t, d[s + 1:p]) |
| if t in ELEM: |
| length, enc, clen = struct.unpack("<III", d[p:p + 12]); p += 12 |
| body = clen if enc else length * ELEM[t] |
| start = p; p += body |
| return p, (t, b"", length, (start, body, enc)) |
| if t in "SR": |
| ln = struct.unpack("<I", d[p:p + 4])[0]; p += 4 |
| val = d[p:p + ln]; p += ln |
| return p, (t, val) |
| raise ValueError("bad prop %r" % t) |
|
|
| def rd_node(p): |
| if u64: end, nprop, plen = struct.unpack("<QQQ", d[p:p + 24]); q = p + 24 |
| else: end, nprop, plen = struct.unpack("<III", d[p:p + 12]); q = p + 12 |
| if end == 0 and nprop == 0 and plen == 0: |
| return p + NREC |
| nl = d[q]; q += 1; name = d[q:q + nl]; q += nl |
| p = q; props = [] |
| for _ in range(nprop): |
| p, pr = rd_prop(p); props.append(pr) |
| if name == b"Model" and len(props) >= 3: |
| uid = struct.unpack("<q", props[0][1])[0] if props[0][0] == "L" else 0 |
| nm = props[1][1].split(b"\x00\x01")[0].decode("utf-8", "replace") |
| models[uid] = nm |
| if props[2][1] in (b"LimbNode", b"Root"): |
| joints.append((uid, nm)) |
| elif name == b"KeyTime" and props and len(props[0]) == 4: |
| keytimes.append((props[0][2], props[0][0], props[0][3])) |
| elif name == b"C" and len(props) >= 3 and props[0][1] in (b"OO", b"OP"): |
| try: |
| a = struct.unpack("<q", props[1][1])[0] |
| b = struct.unpack("<q", props[2][1])[0] |
| except (struct.error, TypeError): |
| return end |
| (links if props[0][1] == b"OO" else animated).append((a, b)) |
| while p < end: |
| if d[p:p + NREC] == b"\x00" * NREC: p += NREC; break |
| p = rd_node(p) |
| return end |
|
|
| p = 27 |
| while p < len(d) - 160: |
| if d[p:p + NREC] == b"\x00" * NREC: break |
| p = rd_node(p) |
| return d, version, joints, keytimes, links, animated |
|
|
|
|
| ELEM_FMT = {"f": "f", "d": "d", "l": "q", "i": "i", "b": "b"} |
|
|
|
|
| def _timeline(d, keytimes): |
| """Frames and frame rate, read off the longest key-time track. |
| |
| The track is decoded rather than counted so the spacing is measured: every |
| clip in this library keys on a strict 1/30 s grid, and a clip that did not |
| would report its own rate instead of inheriting an assumed one. |
| """ |
| if not keytimes: |
| return 0, 0.0 |
| n, t, (start, body, enc) = max(keytimes, key=lambda k: k[0]) |
| raw = d[start:start + body] |
| try: |
| if enc: |
| raw = zlib.decompress(raw) |
| vals = struct.unpack("<%d%s" % (n, ELEM_FMT[t]), raw[:n * ELEM[t]]) |
| except Exception: |
| return n, 0.0 |
| steps = sorted({vals[i + 1] - vals[i] for i in range(len(vals) - 1)}) |
| step = steps[0] if steps else 0 |
| return n, round(FBX_KTIME / step, 3) if step else 0.0 |
|
|
|
|
| def probe(path): |
| d, version, joints, keytimes, links, animated = _parse(path) |
| frames, fps = _timeline(d, keytimes) |
| |
| |
| |
| |
| |
| ids = {uid for uid, _ in joints} |
| parent, children = {}, {} |
| for child, par in links: |
| if child in ids and child not in parent: |
| parent[child] = par |
| children.setdefault(par, []).append(child) |
|
|
| def subtree(uid, seen=None): |
| seen = seen or set() |
| if uid in seen: |
| return 0 |
| seen.add(uid) |
| return 1 + sum(subtree(c, seen) for c in children.get(uid, ())) |
|
|
| def depth(uid, seen=None): |
| seen = seen or set() |
| d = 0 |
| while uid in parent and uid not in seen: |
| seen.add(uid); uid = parent[uid]; d += 1 |
| return d |
|
|
| driven = {par for _, par in animated} & ids |
| pool = driven or ids |
| roots = sorted(((uid, nm) for uid, nm in joints if uid in pool), |
| key=lambda t: (depth(t[0]), -subtree(t[0]))) |
| names = [nm for _, nm in joints] |
| return { |
| "fbx_version": version, |
| "num_joints": len(set(names)), |
| "keyframes": frames, |
| "fps": fps, |
| "duration_sec": round((frames - 1) / fps, 4) if fps and frames > 1 else 0.0, |
| "root_joint": roots[0][1] if roots else "", |
| "num_animated": len(driven), |
| "joints": sorted(set(names)), |
| "joint_names": "|".join(sorted(set(names)))[:400], |
| } |
|
|
|
|
| if __name__ == "__main__": |
| for p in sys.argv[1:]: |
| try: |
| r = probe(p) |
| print("%-34s ver=%s joints=%-4d keys=%-5d %-5s fps %.2fs root=%s" % ( |
| os.path.basename(p), r["fbx_version"], r["num_joints"], |
| r["keyframes"], r["fps"], r["duration_sec"], r["root_joint"])) |
| except Exception as e: |
| print("%-34s ERROR %s" % (os.path.basename(p), str(e)[:60])) |
|
|