File size: 3,291 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/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("<I", d[23:27])[0]
    u64 = version >= 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("<III", d[p:p + 12]); p += 12
            p += clen if enc else n * ELEM[t]
            return p, (t, None)
        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, stack=None):
        nonlocal layers
        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)
        mine = stack
        if name == b"AnimationStack" and len(props) >= 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("<q", v)[0]
                        if key == b"LocalStop":
                            stack[1] = max(stack[1], val / KTIME)
        while p < end:
            if d[p:p + NREC] == b"\x00" * NREC: p += NREC; break
            p = rd_node(p, mine)
        return end

    p = 27
    while p < len(d) - 160:
        if d[p:p + NREC] == b"\x00" * NREC: break
        p = rd_node(p)
    return {"num_takes": len(stacks), "num_layers": layers,
            "takes": [(n, round(s, 3)) for n, s in stacks],
            "total_sec": round(sum(s for _, s in stacks), 3)}


if __name__ == "__main__":
    for f in sys.argv[1:]:
        try:
            r = probe(f)
            print("%-34s takes=%-4d layers=%-4d total=%.2fs  %s" % (
                os.path.basename(f), r["num_takes"], r["num_layers"], r["total_sec"],
                [n for n, _ in r["takes"]][:4]))
        except Exception as e:
            print("%-34s ERROR %s" % (os.path.basename(f), str(e)[:50]))