| """Decode FBX animation curve values and compare two clips frame by frame. |
| |
| Range or amplitude comparisons pair up motions that merely span the same |
| extents; matching the actual key values settles it. |
| """ |
| 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(); 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 |
| 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 same_motion(a,b,eps=1e-4): |
| ca,cb=curves(a),curves(b) |
| ma=[c for c in ca if max(c)-min(c)>1e-6]; mb=[c for c in cb if max(c)-min(c)>1e-6] |
| if len(ma)!=len(mb): return False,"moving curves %d vs %d"%(len(ma),len(mb)) |
| ka=sorted(ma,key=lambda c:(len(c),c[0])); kb=sorted(mb,key=lambda c:(len(c),c[0])) |
| matched=0 |
| for x,y in zip(ka,kb): |
| n=min(len(x),len(y)) |
| if n and all(abs(x[i]-y[i])<eps for i in range(n)): matched+=1 |
| return matched==len(ka), "%d/%d curves identical frame by frame"%(matched,len(ka)) |
| if __name__=="__main__": |
| for i in range(1,len(sys.argv),2): |
| a,b=sys.argv[i],sys.argv[i+1] |
| ok,msg=same_motion(a,b) |
| print(" %-26s vs %-26s %s (%s)"%(os.path.basename(a),os.path.basename(b), |
| "same motion" if ok else "different", msg)) |
|
|