Files

73 lines
3.3 KiB
Python

"""#121 (2026-11-26): lean a retargeted Tauren sequence forward from the spine.
Owner: Tauren /rude and /dancespecial look like he stands on his heels; the Undead has a slightly hunched back.
For each listed native bone, every rotation key of the sequence gets an extra world-space pitch about the model's side axis
(+Y, so the head moves towards +X = forward) applied at that joint: L' = Wp^-1 * Qy(deg) * Wp * L, with Wp the parent's world
rotation at the key time. Bones below the joint (pelvis, legs, feet grounding) are unchanged.
usage: lean.py native.m2 seq in.anim out.anim bone:deg[,bone:deg...]"""
import math
import os
import struct
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from fkcheck import native, qm, slerp
def sample(tr, t):
if not tr: return (0, 0, 0, 1)
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: return slerp(a, b, 0 if tb == ta else (t - ta) / (tb - ta))
return tr[-1][1]
def world(bones, i, t):
q = (0, 0, 0, 1); ch = []
while i >= 0: ch.append(i); i = bones[i]['par']
for j in reversed(ch): q = qm(q, sample(bones[j]['r'], t))
return q
def dq(v): return (v + 32768 if v < 0 else v - 32767) / 32767
def enc(q):
return struct.pack('<4h', *(round(c * 32767 - 32768 if c >= 0 else c * 32767 + 32767) for c in (max(-1, min(1, c)) for c in q)))
def lean_head(bones, t):
from fkcheck import fk
P = fk(bones, t); head = next(i for i, b in enumerate(bones) if b['key'] == 6); root = 0
d = [P[head][k] - P[root][k] for k in range(3)]; return math.degrees(math.atan2(d[0], d[2]))
def main(m2, seq, src, dst, spec):
seq = int(seq); A = bytearray(open(src, 'rb').read())
B = open(m2, 'rb').read(); N = B[8:8 + struct.unpack_from('<I', B, 4)[0]]
bc, bo = struct.unpack_from('<II', N, 0x2C)
before = native(m2, src, seq)
for item in spec.split(','):
j, deg = item.split(':'); j = int(j); h = math.radians(float(deg)) / 2
Q = (0.0, math.sin(h), 0.0, math.cos(h))
open(dst, 'wb').write(A); bones = native(m2, dst, seq)
assert bones[j]['r'], f'bone {j} has no rotation keys in seq {seq}'
it, gs, tc, to, vc, vo = struct.unpack_from('<HhIIII', N, bo + j * 88 + 36)
n, o = struct.unpack_from('<II', N, to + seq * 8); n2, o2 = struct.unpack_from('<II', N, vo + seq * 8)
ts = struct.unpack_from(f'<{n}I', A, o); prev = None
for k, t in enumerate(ts):
L = tuple(dq(c) for c in struct.unpack_from('<4h', A, o2 + k * 8))
Wp = world(bones, bones[j]['par'], t) if bones[j]['par'] >= 0 else (0, 0, 0, 1)
Wi = (-Wp[0], -Wp[1], -Wp[2], Wp[3])
L2 = qm(qm(qm(Wi, Q), Wp), L)
if prev and sum(a * b for a, b in zip(prev, L2)) < 0: L2 = tuple(-c for c in L2)
prev = L2; A[o2 + k * 8:o2 + k * 8 + 8] = enc(L2)
open(dst, 'wb').write(A)
after = native(m2, dst, seq); dur = max(k[0] for b in before for tr in (b['r'], b['t']) if tr for k in tr) or 1
lb = [lean_head(before, t) for t in range(0, dur, 100)]; la = [lean_head(after, t) for t in range(0, dur, 100)]
print(f'{os.path.basename(dst)}: root->head lean mean {sum(lb)/len(lb):.1f} -> {sum(la)/len(la):.1f} deg')
if __name__ == '__main__':
main(*sys.argv[1:6])