| |
| """Decide whether a binary FBX actually contains motion. |
| |
| Keyframe *count* says nothing: a clip can carry 91 keys that all hold the same |
| value. This decodes the `KeyValueFloat` arrays of every animation curve (FBX |
| stores them zlib-compressed) and reports the largest peak-to-peak range found. |
| A file whose every curve is flat is static. |
| """ |
| 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} |
| FMT = {"f": "f", "d": "d", "l": "q", "i": "i", "b": "b"} |
|
|
|
|
| def probe(path, eps=1e-6): |
| 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 |
| curves = [] |
|
|
| def rd_prop(p, want_values): |
| 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("<III", d[p:p + 12]); p += 12 |
| body = clen if enc else n * ELEM[t] |
| raw = d[p:p + body]; p += body |
| vals = None |
| if want_values and n: |
| try: |
| if enc: raw = zlib.decompress(raw) |
| vals = struct.unpack("<%d%s" % (n, FMT[t]), raw[:n * ELEM[t]]) |
| except Exception: |
| vals = None |
| return p, (t, vals, n) |
| if t in "SR": |
| ln = struct.unpack("<I", d[p:p + 4])[0]; p += 4 |
| v = d[p:p + ln]; p += ln |
| return p, (t, v) |
| 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 |
| want = name == b"KeyValueFloat" |
| props = [] |
| for _ in range(nprop): |
| p, pr = rd_prop(p, want); props.append(pr) |
| if want and props and len(props[0]) == 3 and props[0][1]: |
| v = props[0][1] |
| curves.append((max(v) - min(v), len(v))) |
| 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) |
|
|
| ranges = [r for r, _ in curves] |
| return { |
| "num_curves": len(curves), |
| "max_range": max(ranges) if ranges else 0.0, |
| "moving_curves": sum(1 for r in ranges if r > eps), |
| "static": bool(ranges) and max(ranges) <= eps, |
| "no_curves": not ranges, |
| } |
|
|
|
|
| if __name__ == "__main__": |
| for f in sys.argv[1:]: |
| try: |
| r = probe(f) |
| print("%-40s curves=%-5d moving=%-5d max_range=%-12.6g %s" % ( |
| os.path.basename(f), r["num_curves"], r["moving_curves"], |
| r["max_range"], "STATIC" if r["static"] else ("NO_CURVES" if r["no_curves"] else ""))) |
| except Exception as e: |
| print("%-40s ERROR %s" % (os.path.basename(f), str(e)[:50])) |
|
|