Linzhan's picture
Initial release: prompts, metadata, T-pose renders, build pipeline (no commercial motion files)
a84fca7 verified
Raw
History Blame Contribute Delete
3.93 kB
"""Per-take motion: which `AnimationStack` in an FBX actually carries animation.
`fbx_takes.py` counts the stacks; this says which of them hold anything. The two
disagree on four files in this library, where a C4D export left an empty second
stack named `Take 001` beside the real motion — no layers with curves under it,
nothing to play. Counting stacks calls those files two-take; following the
connections shows they hold one action and one empty shell.
Walks `Connections` from each stack down through its layers and curve nodes to
the curves themselves, then reports how many of those curves move.
"""
import struct, sys, zlib, collections
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 analyse(path):
d=open(path,"rb").read(); u64=struct.unpack("<I",d[23:27])[0]>=7500; N=25 if u64 else 13
obj={}; conn=[]; curve_vals={}
def rp(p,want):
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,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,(t,struct.unpack("<%d%s"%(n,FMT[t]),raw[:n*ELEM[t]]))
except Exception: return p,(t,None)
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(t)
def rn(p,owner=None):
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"; props=[]
for _ in range(np_):
p,x=rp(p,want); props.append(x)
mine=owner
if nm in (b"AnimationStack",b"AnimationLayer",b"AnimationCurveNode",b"AnimationCurve") and len(props)>=2:
uid=struct.unpack("<q",props[0][1])[0]
obj[uid]=(nm.decode(), props[1][1].split(b"\x00\x01")[0].decode("utf8","replace"))
mine=uid
elif nm==b"C" and len(props)>=3:
try: conn.append((props[0][1], struct.unpack("<q",props[1][1])[0], struct.unpack("<q",props[2][1])[0]))
except Exception: pass
elif want and props and props[0][1]:
v=props[0][1]
if owner is not None: curve_vals[owner]=max(v)-min(v)
while p<e:
if d[p:p+N]==b"\x00"*N: p+=N; break
p=rn(p,mine)
return e
p=27
while p<len(d)-160:
if d[p:p+N]==b"\x00"*N: break
p=rn(p)
kids=collections.defaultdict(list)
for kind,child,parent in conn: kids[parent].append(child)
out=[]
for uid,(t,name) in obj.items():
if t!="AnimationStack": continue
curves=[]
for lay in [c for c in kids[uid] if obj.get(c,("",""))[0]=="AnimationLayer"]:
for cn in [c for c in kids[lay] if obj.get(c,("",""))[0]=="AnimationCurveNode"]:
for cv in [c for c in kids[cn] if obj.get(c,("",""))[0]=="AnimationCurve"]:
if cv in curve_vals: curves.append(curve_vals[cv])
moving=[r for r in curves if r>1e-6]
out.append((name, len(curves), len(moving), max(curves) if curves else 0.0))
return out
def takes_with_motion(path):
"""How many of the file's stacks actually carry a curve."""
return sum(1 for _, ncurves, _, _ in analyse(path) if ncurves)
if __name__=="__main__":
import os
for f in sys.argv[1:]:
print(os.path.basename(f))
for name,n,mv,mx in analyse(f):
print(" take %-24s curves=%-4d moving=%-4d max_range=%.4f%s"
%(name,n,mv,mx," <- empty" if not n else ""))