File size: 3,625 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
"""Integrity check for binary FBX.

Beyond "does it parse": walks the whole node tree and verifies the walk lands in
the footer region, which catches truncated or padded files that a shallow parse
would accept. Also reports skeleton and geometry presence so empty shells show up.
"""
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}


def check(path):
    size = os.path.getsize(path)
    r = {"size": size, "ok": False, "reason": "", "version": None,
         "joints": 0, "meshes": 0, "verts": 0, "curves": 0, "end_gap": None}
    if size == 0:
        r["reason"] = "zero bytes"; return r
    d = open(path, "rb").read()
    if d[:21] != b"Kaydara FBX Binary  \x00":
        r["reason"] = "bad magic (not a binary FBX)"; return r
    r["version"] = struct.unpack("<I", d[23:27])[0]
    u64 = r["version"] >= 7500
    NREC = 25 if u64 else 13
    joints = set(); meshes = 0; verts = 0; curves = 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], None)
        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, 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, None)
        raise ValueError("bad property type %r at %d" % (t, s))

    def rd_node(p, depth=0):
        nonlocal meshes, verts, curves
        if depth > 64: raise ValueError("node nesting too deep")
        if p + (24 if u64 else 12) > len(d): raise ValueError("header past EOF")
        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
        if end > len(d): raise ValueError("node end offset past EOF")
        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)
        if name == b"Model" and len(props) >= 3 and props[2][1] in (b"LimbNode", b"Root"):
            joints.add(props[1][1])
        elif name == b"Geometry": meshes += 1
        elif name == b"Vertices" and props and props[0][2]: verts += props[0][2] // 3
        elif name == b"AnimationCurve": curves += 1
        while p < end:
            if d[p:p+NREC] == b"\x00" * NREC: p += NREC; break
            p = rd_node(p, depth + 1)
        return end

    try:
        p = 27
        while p < len(d) - 160:
            if d[p:p+NREC] == b"\x00" * NREC: p += NREC; break
            p = rd_node(p)
    except Exception as e:
        r["reason"] = "parse: %s" % str(e)[:70]; return r
    r.update(joints=len(joints), meshes=meshes, verts=verts, curves=curves,
             end_gap=len(d) - p)
    if r["end_gap"] < 0:
        r["reason"] = "walk ran past EOF"; return r
    if r["end_gap"] > 4096:
        r["reason"] = "%d trailing bytes unparsed (truncated or padded)" % r["end_gap"]; return r
    if not joints and not meshes:
        r["reason"] = "no skeleton and no geometry"; return r
    r["ok"] = True
    return r


if __name__ == "__main__":
    for f in sys.argv[1:]:
        r = check(f)
        print("%-38s %-4s v%-5s joints=%-4d mesh=%-3d verts=%-8d curves=%-6d %s" % (
            os.path.basename(f), "OK" if r["ok"] else "BAD", r["version"],
            r["joints"], r["meshes"], r["verts"], r["curves"], r["reason"]))