Files

68 lines
4.6 KiB
Python

"""Refit the 208 Octo-only (path-less natively) helm M2s to HD heads (owner queue 3b, 2026-11-23).
Per race/gender Blizzard moved every helm by one uniform scale + offset for HD heads; that transform is read
from the 79 helm pairs where native HD = exact affine image of Octo's 1.12 copy. Applied to vertex positions;
normals rotated by the linear part (renormalised); M2 bounding box + radius recomputed. Nothing else changes."""
import json, struct, sys, numpy as np
from pathlib import Path
from field import samples, PAIRS
STAGE = Path(sys.argv[1]); OUT = Path(__file__).resolve().parent / 'out2'; OUT.mkdir(exist_ok=True)
sys.path.insert(0, str(STAGE)); import frozen_store as fs
S = samples(); maps = {}; basis = {}
# Blizzard's HD helm refit is exactly uniform scale + offset per race/gender. Take it from pairs whose
# native file is an exact affine image of Octo's (<2% residual), median per race (accessories excluded).
from collections import Counter
for r, items in S.items():
seen = Counter()
for p, a, d in items:
b = a + d; X = np.c_[a, np.ones(len(a))]; M, *_ = np.linalg.lstsq(X, b, rcond=None)
if 'bunnyears' in p or 'monocle' in p: continue # accessories, individually placed
if np.linalg.norm(X @ M - b, axis=1).max() / max(np.ptp(a, 0).max(), 1e-9) < 0.02:
seen[tuple(np.round(np.r_[M[:3].diagonal().mean(), M[3]], 3))] += 1
if seen:
v = np.array([k for k, n in seen.items() for _ in range(n)]); s_, tx, ty, tz = np.median(v, 0)
maps[r] = np.vstack([np.eye(3) * s_, [tx, 0.0, tz]]); basis[r] = dict(scale=float(s_), offset=[float(tx), 0.0, float(tz)], pairs=len(v))
kept0 = json.load(open(PAIRS))['kept']
T = json.load(open(Path(__file__).resolve().parent / 'used-targets.json'))
already = {int(p.name[:-3]) for p in (Path(__file__).resolve().parent / 'out').glob('*.m2')}
kept = [dict(imported_fdid=int(f), path=v['octo'], race_override=v['race'], source='client-used') for f, v in T.items() if int(f) not in already]
store = fs.Store(); root = store.root; lay = store.layout
_seen = set(); kept = [k for k in kept if not (k['imported_fdid'] in _seen or _seen.add(k['imported_fdid']))] # scarfmask/scarfmasktroll share files
cells = fs.casc.root_controls_batch(root, lay, {k['imported_fdid'] for k in kept})
VT = np.dtype([('p', '<3f'), ('w', 'u1', 4), ('b', 'u1', 4), ('n', '<3f'), ('t', '<2f', 2)])
report = []
for k in kept:
fid = k['imported_fdid']; r = k.get('race_override') or k['path'].rsplit('_', 1)[1][:3]
keys = {c['content_key'] for c in cells.get(fid, [])}
if len(keys) != 1 or r not in maps: report.append(dict(k, skip=f'keys {len(keys)} race {r}')); continue
ck = keys.pop(); data = bytearray(store.read(ck)[2]); assert data[:4] == b'MD21'
m0 = 8; n, o = struct.unpack_from('<II', data, m0 + 60)
v = np.frombuffer(bytes(data[m0 + o:m0 + o + 48 * n]), VT).copy()
M = maps[r]; L = M[:3]
v['p'] = (np.c_[v['p'], np.ones(n)] @ M).astype('<f4')
nn = v['n'] @ L; nn /= np.maximum(np.linalg.norm(nn, axis=1, keepdims=True), 1e-9); v['n'] = nn.astype('<f4')
data[m0 + o:m0 + o + 48 * n] = v.tobytes()
s_, t = M[0, 0], M[3]
nb, ob = struct.unpack_from('<II', data, m0 + 44)
for i in range(nb): # M2CompBone 88 B: pivot @76; translation track @16 (keys scale by s)
b = m0 + ob + 88 * i; pv = np.array(struct.unpack_from('<3f', data, b + 76))
struct.pack_into('<3f', data, b + 76, *(s_ * pv + t))
if s_ != 1.0:
na, oa = struct.unpack_from('<II', data, b + 16 + 12)
for j in range(na):
nk, ok = struct.unpack_from('<II', data, m0 + oa + 8 * j)
for q in range(nk):
at = m0 + ok + 12 * q; struct.pack_into('<3f', data, at, *(s_ * np.array(struct.unpack_from('<3f', data, at))))
for off, stride, pos in ((240, 40, 8), (296, None, 8), (288, None, 8), (264, None, 4)): # attachments, particles, ribbons, lights
n2, o2 = struct.unpack_from('<II', data, m0 + off)
if not n2: continue
if stride is None: stride = {296: 492, 288: 176, 264: 156}[off]
for i in range(n2):
at = m0 + o2 + stride * i + pos; p0 = np.array(struct.unpack_from('<3f', data, at))
struct.pack_into('<3f', data, at, *(s_ * p0 + t))
lo, hi = v['p'].min(0), v['p'].max(0)
struct.pack_into('<7f', data, m0 + 160, *lo, *hi, float(np.linalg.norm(hi - lo) / 2))
(OUT / f'{fid}.m2').write_bytes(bytes(data))
report.append(dict(k, content_key=ck, verts=int(n), race=r))
json.dump(dict(transforms=basis, helms=report), open(OUT / 'report.json', 'w'), indent=1)
print('refit', sum('skip' not in x for x in report), 'skipped', [x for x in report if 'skip' in x][:3])