File size: 2,719 Bytes
a84fca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
"""Motion fingerprint of a binary FBX: the per-curve value ranges.

Two exports of the same motion that differ only by a trailing hold share every
curve's (min, max) pair, while genuinely different motions do not. Comparing the
whole multiset is far stricter than comparing a single peak-to-peak figure,
which can coincide by chance.
"""
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, nd=3):
    d = open(path, "rb").read()
    if d[:21] != b"Kaydara FBX Binary  \x00":
        raise ValueError("not a binary FBX")
    u64 = struct.unpack("<I", d[23:27])[0] >= 7500
    NREC = 25 if u64 else 13
    curves = []

    def rd_prop(p, want):
        t = chr(d[p]); s = p; p += 1
        if t in SCALAR: p += SCALAR[t]; return p, None
        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
            if want and n:
                try:
                    if enc: raw = zlib.decompress(raw)
                    return p, struct.unpack("<%d%s" % (n, FMT[t]), raw[:n*ELEM[t]])
                except Exception: return p, None
            return p, None
        if t in "SR":
            ln = struct.unpack("<I", d[p:p+4])[0]; p += 4 + ln; return p, None
        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"; vals = None
        for _ in range(nprop):
            p, v = rd_prop(p, want)
            if v is not None: vals = v
        if want and vals:
            curves.append((round(min(vals), nd), round(max(vals), nd), len(vals)))
        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 = tuple(sorted((a, b) for a, b, _ in curves))     # ignores key counts
    return {"num_curves": len(curves), "ranges": ranges,
            "keys": tuple(sorted(c for _, _, c in curves))}


if __name__ == "__main__":
    for f in sys.argv[1:]:
        r = probe(f)
        print("%-30s curves=%-5d fingerprint=%s" % (
            os.path.basename(f), r["num_curves"], hash(r["ranges"])))