File size: 4,025 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
"""Look for discontinuities in an FBX's animation curves.

Reports the largest frame-to-frame step in each curve, relative to how that
curve normally moves. Rotation tracks are stored in degrees and wrap at ±180,
so a 179° -> -179° step is a 2° move, not a 358° one; those are unwrapped first,
otherwise nearly every turning clip looks broken.

A curve is called discontinuous when its biggest step is large in absolute terms
and also several times the *second* largest step. Fast but smooth motion has many
comparable steps, so nothing stands out; a teleport or a snapped pose produces one
isolated spike. Comparing against a percentile instead fails here, because most
curves barely move and their percentile sits at ~0, making every ratio explode.
"""
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 _curves(path):
    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
    N = 25 if u64 else 13
    out = []

    def rp(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, cl = struct.unpack("<III", d[p:p+12]); p += 12
            body = cl 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")

    def rn(p):
        if u64: e, np_, pl = struct.unpack("<QQQ", d[p:p+24]); q = p+24
        else:   e, np_, pl = struct.unpack("<III", d[p:p+12]); q = p+12
        if e == 0 and np_ == 0 and pl == 0: return p + N
        nl = d[q]; q += 1; nm = d[q:q+nl]; q += nl
        p = q; want = nm == b"KeyValueFloat"; v = None
        for _ in range(np_):
            p, x = rp(p, want)
            if x is not None: v = x
        if want and v: out.append(v)
        while p < e:
            if d[p:p+N] == b"\x00"*N: p += N; break
            p = rn(p)
        return e

    p = 27
    while p < len(d) - 160:
        if d[p:p+N] == b"\x00"*N: break
        p = rn(p)
    return out


def probe(path, abs_min=45.0, ratio=4.0):
    worst = 0.0; worst_ratio = 0.0; bad = 0; n = 0
    for c in _curves(path):
        if len(c) < 3: continue
        rng = max(c) - min(c)
        if rng <= 1e-6: continue
        n += 1
        degrees = rng > 20.0            # rotation tracks span tens of degrees
        deltas = []
        for i in range(1, len(c)):
            dv = c[i] - c[i-1]
            if degrees and abs(dv) > 180.0:
                dv -= 360.0 if dv > 0 else -360.0      # unwrap
            deltas.append(abs(dv))
        if not deltas: continue
        s = sorted(deltas, reverse=True)
        mx = s[0]; second = s[1] if len(s) > 1 else 0.0
        r = mx / second if second > 1e-9 else (float("inf") if mx > abs_min else 0.0)
        if mx > worst: worst = mx
        if r != float("inf") and r > worst_ratio: worst_ratio = r
        if mx >= abs_min and r >= ratio: bad += 1
    return {"curves": n, "max_step": round(worst, 3),
            "max_ratio": round(worst_ratio, 1), "discontinuous_curves": bad}


if __name__ == "__main__":
    for f in sys.argv[1:]:
        try:
            r = probe(f)
            print("%-34s curves=%-4d max_step=%-9.2f ratio=%-7.1f bad=%d" % (
                os.path.basename(f), r["curves"], r["max_step"], r["max_ratio"],
                r["discontinuous_curves"]))
        except Exception as e:
            print("%-34s ERROR %s" % (os.path.basename(f), str(e)[:50]))