#!/usr/bin/env python3 """Count animation takes in a binary FBX and read each take's time span. An FBX may hold several `AnimationStack` objects — one per take. Keyframe counts cannot reveal this; the stacks have to be enumerated. Each stack's LocalStart / LocalStop live in its Properties70 block, in FBX ktime units. """ import os, struct, sys SCALAR = {"Y": 2, "C": 1, "I": 4, "F": 4, "D": 8, "L": 8} ELEM = {"f": 4, "d": 8, "l": 8, "i": 4, "b": 1} KTIME = 46186158000 def probe(path): d = open(path, "rb").read() if d[:21] != b"Kaydara FBX Binary \x00": raise ValueError("not a binary FBX") version = struct.unpack("= 7500 NREC = 25 if u64 else 13 stacks = [] # (name, seconds) layers = 0 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: n, enc, clen = struct.unpack("= 2: nm = props[1][1] or b"" mine = [nm.split(b"\x00\x01")[0].decode("utf-8", "replace"), 0.0] stacks.append(mine) elif name == b"AnimationLayer": layers += 1 elif name == b"P" and stack is not None and props: key = props[0][1] if key in (b"LocalStop", b"LocalStart"): for t, v in props: if t == "L" and v is not None and len(v) == 8: val = struct.unpack("