Files
rosetta/tools/fixtures/pmx.py
T

78 lines
4.4 KiB
Python

# PMX / PMD fixtures (tools/fixtures.py): assimp's MMD test model (test/models-nonbsd/MMD/Alicia_blade.pmx, read from
# the nixpkgs assimp source, never committed) and synthetic models from tools/fixtures/_pmx_writer.py (PMX 2.0 / 2.1,
# UTF-16 / UTF-8, index sizes 1 / 2 / 4, additional UVs, every deform and morph type), with assimp's counts
# (`assimp info -r`: meshes, faces, vertices, materials, bones) and world bounds (assimp's PreTransformVertices
# export), plus the generator's own facts (vertex / bone / morph counts, sample vertices). The same synthetic models as
# PMD 1.0 (no reference reads PMD: the test compares them with the PMX decode).
# tests/gen/pmx_cases.luau { name, data, files = { name -> bytes }, assimp, expect? }
# tests/gen/pmx_pmd.luau { name, data, pmx (the case index of the same model) }
import importlib.util, os, struct, zlib
INPUTS = ["tools/fixtures/_pmx_writer.py", "tools/fixtures/_model_ref.py", "tools/fixtures/fbx.py"]
def load(ctx, name):
spec = importlib.util.spec_from_file_location(name, os.path.join(ctx.root, "tools", "fixtures", name + ".py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
def tiny_png():
raw = b"".join(b"\0" + bytes([200, 40, 40]) * 2 for _ in range(2))
def chunk(t, d):
return struct.pack(">I", len(d)) + t + d + struct.pack(">I", zlib.crc32(t + d))
return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", 2, 2, 8, 2, 0, 0, 0)) + chunk(b"IDAT", zlib.compress(raw)) + chunk(b"IEND", b"")
def generate(ctx):
M = load(ctx, "_model_ref")
Fbx = load(ctx, "fbx")
W = load(ctx, "_pmx_writer")
tmp = M.tempdir()
cases, pmds = [], []
src = os.path.join(Fbx.assimp_src(ctx), "test", "models-nonbsd", "MMD", "Alicia_blade.pmx")
if os.path.exists(src):
cases.append({"name": "assimp/Alicia_blade.pmx", "path": src, "files": {}})
variants = [
("small_utf16_i1.pmx", dict(version=2.0, utf8=False, sizes=(1, 1, 1, 1, 1, 1), uva=0), dict(rings=4, seg=6, nbones=3, nmat=2)),
("utf8_i2_uva2.pmx", dict(version=2.0, utf8=True, sizes=(2, 2, 2, 2, 2, 2), uva=2), dict(rings=12, seg=16, nbones=5, nmat=3)),
("v21_utf16_i4_uva4.pmx", dict(version=2.1, utf8=False, sizes=(4, 4, 4, 4, 4, 4), uva=4), dict(rings=30, seg=20, nbones=8, nmat=3)),
("v21_utf8_mixed.pmx", dict(version=2.1, utf8=True, sizes=(2, 1, 1, 2, 1, 1), uva=1), dict(rings=8, seg=10, nbones=4, nmat=3, deform=[4, 3, 1], flips=True)),
]
png = tiny_png()
for fname, wopts, topts in variants:
uva = wopts["uva"]
model = W.tube(version=wopts["version"], uva=uva, seed=len(cases), name=fname[:-4], **topts)
data = W.Pmx(**wopts).build(model)
path = os.path.join(tmp, fname)
with open(path, "wb") as f:
f.write(data)
vs = model["vertices"]
samples = []
for i in (0, 1, 2, 3, len(vs) - 1):
v = vs[i]
samples.append({"index": i, "pos": list(v["pos"]), "uv": list(v["uv"]), "deform": v["deform"], "bones": v["bones"], "weights": v["weights"]})
types = {}
for mo in model["morphs"]:
types[str(mo["type"])] = types.get(str(mo["type"]), 0) + 1
expect = {"vertices": len(vs), "bones": len(model["bones"]), "morphs": len(model["morphs"]), "morphTypes": types,
"textures": len(model["textures"]), "uva": uva, "samples": samples, "name": model["name"], "version": wopts["version"],
"rigid": len(model["rigid"]), "joints": len(model["joints"]), "frames": len(model["frames"])}
cases.append({"name": "gen/" + fname, "path": path, "files": {"tex/body.png": png}, "expect": expect})
if wopts["version"] == 2.0 and wopts["sizes"][0] <= 2:
pmds.append({"name": "gen/" + fname[:-4] + ".pmd", "data": W.pmd(model), "pmx": len(cases)})
infos = M.assimp_info(ctx, [c["path"] for c in cases], raw=True)
for c, a in zip(cases, infos):
a.update(Fbx.assimp_extra(ctx, c["path"]))
a.pop("min", None)
a.pop("max", None)
a.update(Fbx.ptv_bounds(ctx, c["path"], tmp))
if a.get("failed") and c.get("expect", {}).get("morphTypes", {}).get("9"):
a = {"skip": True} # flip / impulse morphs: assimp's MMD reader stops there
c["assimp"] = a
c["data"] = open(c["path"], "rb").read()
del c["path"]
ctx.emit("pmx_cases", cases)
ctx.emit("pmx_pmd", pmds)