59 lines
3.0 KiB
Python
59 lines
3.0 KiB
Python
"""#120 (2026-11-26): keep a retargeted sequence on the ground.
|
|
|
|
The Octo->native rotation retarget gives the legs a different reach than the Octo rig, so the lowest leg joint swings
|
|
above (owner: Tauren /dancespecial floats; first pass used every joint and made it jump) and below the native stand ground line. For every translation key of the
|
|
pelvis bone (key bone 26) in the new sequence, run native forward kinematics and shift the pelvis Z so the lowest
|
|
non-root joint sits at the native stand sequence's mean lowest-joint height. Works for MD21 models (.anim with the new
|
|
sequence) and .skel rigs (SKB1 bones, raw AFSB payload).
|
|
|
|
usage: ground.py native.m2|skel seq_index seq.anim stand_md20_or_anim stand_seq out.anim"""
|
|
import os, struct, sys
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
from fkcheck import fk, native
|
|
|
|
|
|
def lowest(bones, t, feet):
|
|
P = fk(bones, t)
|
|
return min(P[i][2] for i in feet)
|
|
|
|
|
|
def main(m2, seq, anim, stand_anim, stand_seq, out):
|
|
seq, stand_seq = int(seq), int(stand_seq)
|
|
B = open(m2, 'rb').read()
|
|
if B[:4] == b'SKL1':
|
|
at = 0
|
|
while True:
|
|
t, n = struct.unpack_from('<4sI', B, at)
|
|
if t == b'SKB1': N = B[at + 8:at + 8 + n]; boff = 0; break
|
|
at += 8 + n
|
|
else:
|
|
N = B[8:8 + struct.unpack_from('<I', B, 4)[0]]; boff = 0x2C
|
|
bones = native(m2, anim, seq); stand = native(m2, stand_anim, stand_seq)
|
|
roots = {i for i, b in enumerate(bones) if b['par'] < 0}
|
|
# Feet = joints that sit at the ground in the stand pose (lowest 6% of the rig height). Hands, tail or weapon bones
|
|
# that dip low during the dance must not lift the body (owner 2026-11-26: "the tauren now jumps").
|
|
P0 = fk(stand, 0); zs = [P0[i][2] for i in P0 if i not in roots]; H = max(zs) - min(zs)
|
|
feet = {i for i in P0 if i not in roots and P0[i][2] <= min(zs) + 0.06 * H}
|
|
roots = feet
|
|
pel = next(i for i, b in enumerate(bones) if b['key'] == 26)
|
|
sdur = max((k[0] for b in stand for tr in (b['t'], b['r']) if tr for k in tr), default=1000) or 1000
|
|
ground = sum(lowest(stand, t, roots) for t in range(0, sdur, 50)) / len(range(0, sdur, 50))
|
|
bc, bo = struct.unpack_from('<II', N, boff)
|
|
it, gs, tc, to, vc, vo = struct.unpack_from('<HhIIII', N, bo + pel * 88 + 16)
|
|
n, o = struct.unpack_from('<II', N, to + seq * 8); n2, o2 = struct.unpack_from('<II', N, vo + seq * 8)
|
|
assert n and n == n2, 'pelvis has no translation keys in this sequence'
|
|
A = bytearray(open(anim, 'rb').read())
|
|
ts = struct.unpack_from(f'<{n}I', A, o)
|
|
before, after = [], []
|
|
for k, t in enumerate(ts):
|
|
low = lowest(bones, t, roots); before.append(low)
|
|
x, y, z = struct.unpack_from('<3f', A, o2 + k * 12)
|
|
struct.pack_into('<3f', A, o2 + k * 12, x, y, z + (ground - low))
|
|
open(out, 'wb').write(A)
|
|
check = native(m2, out, seq)
|
|
after = [lowest(check, t, roots) for t in ts]
|
|
print(f'ground {ground:.3f}; lowest joint before {min(before):.3f}..{max(before):.3f}, after {min(after):.3f}..{max(after):.3f}')
|
|
|
|
|
|
if __name__ == '__main__': main(*sys.argv[1:7])
|