Files

92 lines
4.7 KiB
Python

"""Forward kinematics: Octo source seq vs retargeted native seq; joint positions normalized by height, both at the same times."""
import struct,math,sys,json
def qm(a,b):
ax,ay,az,aw=a;bx,by,bz,bw=b
return (aw*bx+ax*bw+ay*bz-az*by, aw*by-ax*bz+ay*bw+az*bx, aw*bz+ax*by-ay*bx+az*bw, aw*bw-ax*bx-ay*by-az*bz)
def rot(q,v):
x,y,z,w=q; p=qm(qm(q,(v[0],v[1],v[2],0)),(-x,-y,-z,w)); return p[:3]
def slerp(a,b,t):
d=sum(x*y for x,y in zip(a,b))
if d<0: b=tuple(-c for c in b)
r=tuple(x+(y-x)*t for x,y in zip(a,b)); l=math.sqrt(sum(c*c for c in r)) or 1; return tuple(c/l for c in r)
def sample(tr,t,isq):
if not tr: return (0,0,0,1) if isq else (0,0,0)
if t<=tr[0][0] or len(tr)==1: return tr[0][1]
for (ta,a),(tb,b) in zip(tr,tr[1:]):
if ta<=t<=tb:
f=0 if tb==ta else (t-ta)/(tb-ta); return slerp(a,b,f) if isq else tuple(x+(y-x)*f for x,y in zip(a,b))
return tr[-1][1]
def fk(bones,t):
W={};P={}
def go(i):
if i in W: return
b=bones[i]; R=sample(b['r'],t,True); T=sample(b['t'],t,False)
if b['par']<0: Wp,Pp,pp=(0,0,0,1),(0,0,0),(0,0,0)
else: go(b['par']); Wp,Pp=W[b['par']],P[b['par']]
# joint position = parent transform applied to (pivot + T)
if b['par']<0: P[i]=tuple(a+c for a,c in zip(b['piv'],T))
else:
pb=bones[b['par']]['piv']; local=tuple(a-c+d for a,c,d in zip(b['piv'],pb,T))
P[i]=tuple(a+c for a,c in zip(Pp,rot(Wp,local)))
W[i]=qm(Wp,R)
for i in range(len(bones)): go(i)
return P
def dq(v):
v=v+32768 if v<0 else v-32767; return v/32767
def octo(p,aid):
O=open(p,'rb').read(); sc,so=struct.unpack_from('<II',O,0x1C)
si=next(i for i in range(sc) if struct.unpack_from('<HH',O,so+i*68)==(aid,0)); w0,w1=struct.unpack_from('<II',O,so+si*68+4)
bc,bo=struct.unpack_from('<II',O,0x34); out=[]
for i in range(bc):
at=bo+i*108; b=dict(key=struct.unpack_from('<i',O,at)[0],par=struct.unpack_from('<h',O,at+8)[0],piv=struct.unpack_from('<3f',O,at+96))
for name,off,st in (('t',12,12),('r',40,16)):
it,gs,nr,ro,nt,to,nk,ko=struct.unpack_from('<Hh6I',O,at+off); tr=None
if nt:
times=struct.unpack_from(f'<{nt}I',O,to); keys=[struct.unpack_from(f'<{st//4}f',O,ko+k*st) for k in range(nk)]
if gs>=0 or nr==0: tr=[(0,keys[0])]
else:
a,e=struct.unpack_from('<II',O,ro+si*8)
if 0<=a<=e<nt: tr=[(times[k]-w0,keys[k]) for k in range(a,e+1)]
b[name]=tr
out.append(b)
return out,w1-w0
def native(m2,anim,seq):
B=open(m2,'rb').read(); A=open(anim,'rb').read()
if B[:4]==b'SKL1': # #56: .skel bones live in SKB1 (offsets relative to the chunk); anim = raw AFSB payload
at=0
while True:
t,n=struct.unpack_from('<4sI',B,at)
if t==b'SKB1': N=B[at+8:at+8+n]; break
at+=8+n
bc,bo=struct.unpack_from('<II',N,0); out=[]
else:
N=B[8:8+struct.unpack_from('<I',B,4)[0]]
bc,bo=struct.unpack_from('<II',N,0x2C); out=[]
for i in range(bc):
at=bo+i*88; b=dict(key=struct.unpack_from('<i',N,at)[0],par=struct.unpack_from('<h',N,at+8)[0],piv=struct.unpack_from('<3f',N,at+76))
for name,off in (('t',16),('r',36)):
it,gs,tc,to,vc,vo=struct.unpack_from('<HhIIII',N,at+off); tr=None
if gs<0 and tc>seq:
n,o=struct.unpack_from('<II',N,to+seq*8); n2,o2=struct.unpack_from('<II',N,vo+seq*8)
if n:
ts=struct.unpack_from(f'<{n}I',A,o)
vals=[tuple(dq(c) for c in struct.unpack_from('<4h',A,o2+k*8)) for k in range(n2)] if name=='r' else [struct.unpack_from('<3f',A,o2+k*12) for k in range(n2)]
tr=list(zip(ts,vals))
b[name]=tr
out.append(b)
return out
if __name__=='__main__':
op,aid,m2,anim,seq=sys.argv[1],int(sys.argv[2]),sys.argv[3],sys.argv[4],int(sys.argv[5])
ob,dur=octo(op,aid); nb=native(m2,anim,seq)
ko={b['key']:i for i,b in enumerate(ob) if b['key']>=0}; kn={b['key']:i for i,b in enumerate(nb) if b['key']>=0}
Ho=max(b['piv'][2] for b in ob)-min(b['piv'][2] for b in ob); Hn=max(b['piv'][2] for b in nb)-min(b['piv'][2] for b in nb)
names={6:'Head',13:'HandL',8:'HandR',0:'ArmL',1:'ArmR'}
for t in (0,dur//4,dur//2,3*dur//4):
Po,Pn=fk(ob,t),fk(nb,t)
row=[]
for k,nm in names.items():
# hand relative to head, normalized
ro=tuple((a-c)/Ho for a,c in zip(Po[ko[k]],Po[ko[6]])); rn=tuple((a-c)/Hn for a,c in zip(Pn[kn[k]],Pn[kn[6]]))
if k!=6: row.append(f"{nm} o{tuple(round(v,2) for v in ro)} n{tuple(round(v,2) for v in rn)}")
print('t',t,' | '.join(row))