File size: 2,982 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
#!/usr/bin/env python3
"""Rig fingerprint of a binary FBX: joint names plus their rest offsets.

Joint count alone cannot tell two skeletons apart — two rigs can share a count
and still differ in proportions. Each joint's `Lcl Translation` is its offset
from its parent, i.e. the bone vector, so the (name, offset) set pins down both
topology and bone lengths.
"""
import math, 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}


def probe(path, nd=3):
    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
    bones = {}

    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, cur=None):
        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 = cur
        if name == b"Model" and len(props) >= 3 and props[2][1] in (b"LimbNode", b"Root"):
            mine = props[1][1].split(b"\x00\x01")[0].decode("utf-8", "replace")
            bones.setdefault(mine, (0.0, 0.0, 0.0))
        elif name == b"P" and cur is not None and props and props[0][1] == b"Lcl Translation":
            vals = [struct.unpack("<d", v)[0] for t, v in props if t == "D" and v and len(v) == 8]
            if len(vals) >= 3:
                bones[cur] = tuple(vals[-3:])
        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)
    sig = tuple(sorted((n, round(x, nd), round(y, nd), round(z, nd))
                       for n, (x, y, z) in bones.items()))
    total = sum(math.sqrt(x * x + y * y + z * z) for _, (x, y, z) in bones.items())
    return {"num_joints": len(bones), "signature": sig, "bone_length_sum": round(total, 3)}


if __name__ == "__main__":
    for f in sys.argv[1:]:
        r = probe(f)
        print("%-34s joints=%-4d bone_len_sum=%-12.3f" % (
            os.path.basename(f), r["num_joints"], r["bone_length_sum"]))