File size: 3,015 Bytes
12b5ac7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""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  # ktime units per second

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  # null terminator record
        props = []
        for _ in range(nprop):
            p, pr = read_prop(p)
            props.append(pr)
        # KeyTime array => keyframe count
        if name == b"KeyTime":
            for kind,t,v in props:
                if kind=="array": keytime_lens.append(v)
        # AnimationStack LocalStop (int64 ktime) via Properties70 "P" records
        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)
        # recurse into nested nodes until `end`
        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))