110 lines
4.3 KiB
Python
110 lines
4.3 KiB
Python
"""#56 (2026-11-25): add a new external sequence (e.g. anim 211 /dancespecial) to a native MD21 character model.
|
|
|
|
Appends one M2Sequence (copied from a template sequence index, id/variation/next/alias rewritten), grows every
|
|
per-sequence track array whose outer count equals the sequence count (bone T/R/S, camera tracks, event track bases,
|
|
any other listed track) by one empty entry, adds the AFID row (anim id, 0, new .anim FDID) and inserts the id into the
|
|
native lookup table (client probe path i += p*p from id % size, first free slot). The new sequence's
|
|
bone data is written afterwards by retarget.py into the new .anim, at sequence index = old count.
|
|
|
|
usage: add_sequence.py in.m2 template_seq_index anim_id anim_fdid out.m2
|
|
"""
|
|
import struct
|
|
import sys
|
|
|
|
|
|
def chunks(b):
|
|
at = 0
|
|
out = []
|
|
while at + 8 <= len(b):
|
|
tag, n = struct.unpack_from('<4sI', b, at)
|
|
out.append([tag, bytearray(b[at + 8:at + 8 + n])])
|
|
at += 8 + n
|
|
assert at == len(b), 'trailing bytes'
|
|
return out
|
|
|
|
|
|
def main(src, template, anim_id, anim_fdid, dst):
|
|
template, anim_id, anim_fdid = int(template), int(anim_id), int(anim_fdid)
|
|
parts = chunks(open(src, 'rb').read())
|
|
md = next(p for p in parts if p[0] == b'MD21')
|
|
N = md[1]
|
|
assert N[:4] == b'MD20'
|
|
K, so = struct.unpack_from('<II', N, 0x1C)
|
|
ids = [struct.unpack_from('<H', N, so + 64 * i)[0] for i in range(K)]
|
|
assert anim_id not in ids, 'sequence already present'
|
|
|
|
def append(data):
|
|
N.extend(bytes((-len(N)) % 16))
|
|
off = len(N)
|
|
N.extend(data)
|
|
return off
|
|
|
|
# 1. sequence array + new entry (template copy: flags, frequency, blend, bounds)
|
|
entry = bytearray(N[so + 64 * template:so + 64 * template + 64])
|
|
struct.pack_into('<HH', entry, 0, anim_id, 0)
|
|
struct.pack_into('<hH', entry, 60, -1, K)
|
|
new_so = append(bytes(N[so:so + 64 * K]) + bytes(entry))
|
|
struct.pack_into('<II', N, 0x1C, K + 1, new_so)
|
|
|
|
# 2. grow every per-sequence array sized K
|
|
grown = {'full': 0, 'base': 0}
|
|
|
|
def grow_full(at):
|
|
tc, to, vc, vo = struct.unpack_from('<IIII', N, at + 4)
|
|
if tc != K:
|
|
return
|
|
assert vc == K, ('values count', vc, K)
|
|
nt = append(bytes(N[to:to + 8 * K]) + bytes(8))
|
|
nv = append(bytes(N[vo:vo + 8 * K]) + bytes(8))
|
|
struct.pack_into('<IIII', N, at + 4, K + 1, nt, K + 1, nv)
|
|
grown['full'] += 1
|
|
|
|
def grow_base(at):
|
|
tc, to = struct.unpack_from('<II', N, at + 4)
|
|
if tc != K:
|
|
return
|
|
nt = append(bytes(N[to:to + 8 * K]) + bytes(8))
|
|
struct.pack_into('<II', N, at + 4, K + 1, nt)
|
|
grown['base'] += 1
|
|
|
|
bc, bo = struct.unpack_from('<II', N, 0x2C)
|
|
for j in range(bc):
|
|
for f in (16, 36, 56):
|
|
grow_full(bo + 88 * j + f)
|
|
for off, size, fields in ((0x48, 40, (0, 20)), (0x58, 20, (0,)), (0x60, 60, (0, 20, 40)), (0xF0, 40, (20,)),
|
|
(0x108, 156, (16, 36, 56, 76, 96, 116, 136)), (0x110, 116, (12, 44, 76, 96))):
|
|
c, po = struct.unpack_from('<II', N, off)
|
|
for k in range(c):
|
|
for f in fields:
|
|
grow_full(po + size * k + f)
|
|
ec, eo = struct.unpack_from('<II', N, 0x100)
|
|
for k in range(ec):
|
|
grow_base(eo + 36 * k + 24)
|
|
for off in (0x120, 0x128):
|
|
assert struct.unpack_from('<I', N, off)[0] == 0, 'ribbons/particles not handled'
|
|
|
|
# 3. lookup: first free slot on the client's probe path
|
|
lc, lo = struct.unpack_from('<II', N, 0x24)
|
|
# The client probes i += p*p (p = 1, 2, 3...): every displaced native entry sits on that path (2026-11-25;
|
|
# linear probing put 211 where the client never looks on Tauren/Gnome).
|
|
s, p = anim_id % lc, 0
|
|
while struct.unpack_from('<h', N, lo + 2 * s)[0] != -1:
|
|
p += 1
|
|
if p >= lc:
|
|
raise SystemExit('lookup table full')
|
|
s = (s + p * p) % lc
|
|
struct.pack_into('<h', N, lo + 2 * s, K)
|
|
|
|
# 4. AFID row
|
|
afid = next((p for p in parts if p[0] == b'AFID'), None)
|
|
assert afid is not None, 'model without AFID'
|
|
afid[1].extend(struct.pack('<HHI', anim_id, 0, anim_fdid))
|
|
|
|
out = b''.join(struct.pack('<4sI', t, len(d)) + bytes(d) for t, d in parts)
|
|
open(dst, 'wb').write(out)
|
|
print('seq index', K, 'tracks grown', grown, 'lookup slot', s, 'of', lc, 'size', len(out))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main(*sys.argv[1:6])
|