Files

62 lines
4.3 KiB
Python

"""#157 owner 2026-11-27: High Elf HD (ChrModel 19 male / 20 female) faces 7-10 have no material for skins 11-16
(native data stops at the older skin list), so the head shows another skin than the body. For every (face, skin)
pair without a face material where another face of the same model has one, paint the face's texture from its nearest
available skin (closest mean colour) onto the colour statistics of the nearest face that has the target skin.
Writes new textures (DXT1 BLP2 like the native HD faces) and add-rows plans for Element / Material / TextureFileData."""
import io, json, sys
import numpy as np
from PIL import Image
from pathlib import Path
H = Path(__file__).resolve().parent; sys.path.insert(0, str(H)); import getdb2, blpw
D = Path(sys.argv[1]) # Db2Bake dumps: choice.tsv option.tsv element.tsv ChrCustomizationMaterial.tsv TextureFileData.tsv
MODELS = {19: (110, 111), 20: (119, 120)} # ChrModelID -> (skin option, face option)
FDID0, MRID0, MAT0, ELEM0 = 8860000, 2210000, 310000, 410000
def rows(name):
out = {}
for l in open(D / f'{name}.tsv'):
f = l.rstrip('\n').split('\t'); r = dict(x.split('=', 1) for x in f[1:] if '=' in x)
out[int(f[0])] = r
return out
ch, el, mat, tfd = rows('choice'), rows('element'), rows('ChrCustomizationMaterial'), rows('TextureFileData')
tex_of = {}
for fid, r in tfd.items(): tex_of.setdefault(int(r['MaterialResourcesID']), []).append(int(r['FileDataID']))
pair = {} # (face, skin) -> {target: material}
for r in el.values():
m = int(r['ChrCustomizationMaterialID'])
if m: pair.setdefault((int(r['ChrCustomizationChoiceID']), int(r['RelatedChrCustomizationChoiceID'])), {})[int(mat[m]['ChrModelTextureTargetID'])] = m
_img = {}
def img(m):
if m not in _img:
f, = tex_of[int(mat[m]['MaterialResourcesID'])]; _img[m] = np.asarray(Image.open(io.BytesIO(getdb2.get(f))).convert('RGB')).astype(float)
return _img[m]
stat = lambda a: (a.reshape(-1, 3).mean(0), a.reshape(-1, 3).std(0) + 1e-6)
adds = dict(ChrCustomizationElement=[], ChrCustomizationMaterial=[], TextureFileData=[]); report = []
n = 0
for model, (skin_opt, face_opt) in MODELS.items():
order = lambda c: int(ch[c]['OrderIndex'])
skins = sorted((c for c, r in ch.items() if int(r['ChrCustomizationOptionID']) == skin_opt), key=order)
faces = sorted((c for c, r in ch.items() if int(r['ChrCustomizationOptionID']) == face_opt), key=order)
for F in faces:
for S in skins:
if (F, S) in pair: continue
refs = [G for G in faces if (G, S) in pair and G != F]
if not refs or not any((F, s) in pair for s in skins): continue
G = min(refs, key=lambda g: abs(order(g) - order(F)))
for target, gm in pair[(G, S)].items():
cands = [s for s in skins if target in pair.get((F, s), {})]
gmu, gsd = stat(img(gm))
S0 = min(cands, key=lambda s: np.abs(stat(img(pair[(F, s)][target]))[0] - gmu).sum())
src = img(pair[(F, S0)][target]); smu, ssd = stat(src)
out = np.clip((src - smu) / ssd * gsd + gmu, 0, 255).astype('uint8')
fd, mr, mid, eid = FDID0 + n, MRID0 + n, MAT0 + n, ELEM0 + n; n += 1
im = Image.fromarray(out, 'RGB'); blpw.write_blp(im, H / 'inputs/plan/textures' / f'{fd}.blp')
Image.fromarray(np.hstack([src.astype('uint8'), out, img(gm).astype('uint8')])).resize((768, 256)).save(H / 'preview' / f'{fd}.png')
adds['TextureFileData'].append(dict(ID=fd, FileDataID=fd, UsageType=0, MaterialResourcesID=mr))
adds['ChrCustomizationMaterial'].append(dict(ID=mid, ChrModelTextureTargetID=target, MaterialResourcesID=mr))
adds['ChrCustomizationElement'].append(dict(ID=eid, ChrCustomizationChoiceID=F, RelatedChrCustomizationChoiceID=S, ChrCustomizationMaterialID=mid))
report.append(dict(model=model, face=F, skin=S, target=target, ref_face=G, src_skin=S0, fdid=fd,
path=f'character/bloodelf/{"male" if model == 19 else "female"}/octo_hd_face_{F}_skin_{S}_t{target}.blp'))
for t, v in adds.items(): (H / 'inputs/plan' / f'add-{t}.json').write_text(json.dumps(v, indent=1) + '\n')
(H / 'inputs/plan/report.json').write_text(json.dumps(report, indent=1) + '\n')
print(len(report), 'textures', {m: sum(r['model'] == m for r in report) for m in MODELS})