| |
| """Count animation frames in a binary FBX (Kaydara 7.x). |
| Parses the node tree, reads AnimationCurve KeyTime arrays; frames = max key count. |
| Also reports the AnimationStack time span as a cross-check.""" |
| import struct, zlib, sys |
|
|
| FBX_KTIME = 46186158000 |
|
|
| def parse(path): |
| d = open(path, "rb").read() |
| assert d[:21] == b"Kaydara FBX Binary \x00", "not a binary FBX" |
| version = struct.unpack("<I", d[23:27])[0] |
| u64 = version >= 7500 |
| pos = 27 |
| keytime_lens = [] |
| stack_span = [0] |
|
|
| def read_prop(p): |
| t = chr(d[p]); p += 1 |
| if t in "YCIFDL": |
| sz = {"Y":2,"C":1,"I":4,"F":4,"D":8,"L":8}[t] |
| val = None |
| if t=="L": val = struct.unpack("<q", d[p:p+8])[0] |
| if t=="I": val = struct.unpack("<i", d[p:p+4])[0] |
| p += sz |
| return p, ("scalar", t, val) |
| if t in "fidlb": |
| length, enc, clen = struct.unpack("<III", d[p:p+12]); p += 12 |
| raw = d[p:p+clen]; p += clen |
| return p, ("array", t, length) |
| if t in "SR": |
| ln = struct.unpack("<I", d[p:p+4])[0]; p += 4 |
| s = d[p:p+ln]; p += ln |
| return p, ("str", t, s) |
| raise ValueError("bad prop type %r at %d" % (t, p)) |
|
|
| def read_node(p): |
| if u64: |
| end, nprop, plen = struct.unpack("<QQQ", d[p:p+24]); p += 24 |
| else: |
| end, nprop, plen = struct.unpack("<III", d[p:p+12]); p += 12 |
| nl = d[p]; p += 1 |
| name = d[p:p+nl]; p += nl |
| if end == 0: |
| return None, p |
| props = [] |
| for _ in range(nprop): |
| p, pr = read_prop(p) |
| props.append(pr) |
| |
| if name == b"KeyTime": |
| for kind,t,v in props: |
| if kind=="array": keytime_lens.append(v) |
| |
| if name == b"P" and props and props[0][2] in (b"LocalStop", b"LocalStop|LocalStop"): |
| for kind,t,v in props: |
| if kind=="scalar" and t=="L" and v: stack_span[0]=max(stack_span[0], v) |
| |
| while p < end - (13 if not u64 else 25): |
| child, p = read_node(p) |
| if child is None: break |
| p = end |
| return name, p |
|
|
| footer = len(d) - 160 |
| while pos < footer: |
| node, pos = read_node(pos) |
| if node is None: break |
| return { |
| "version": version, |
| "keys_max": max(keytime_lens) if keytime_lens else 0, |
| "span_frames": round(stack_span[0]/FBX_KTIME*30) if stack_span[0] else 0, |
| } |
|
|
| if __name__ == "__main__": |
| for f in sys.argv[1:]: |
| try: |
| r = parse(f) |
| print("%-45s keys=%-5s span30=%-5s ver=%s" % (f.split('/')[-1], r["keys_max"], r["span_frames"], r["version"])) |
| except Exception as e: |
| print("%-45s ERR %s" % (f.split('/')[-1], e)) |
|
|