Files
3d-model-renderer/tools/make_test_models.py
T
gs5579andDevin AI d842285390 Add OSG/Assimp/ImGui model renderer with PMX/PMD/FBX loading, posing and morphs
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-27 09:51:44 +00:00

170 lines
7.3 KiB
Python

#!/usr/bin/env python3
"""Writes small PMX/PMD test models (and a checker texture) into tests/data.
Geometry is authored in the viewer's right-handed space and converted to
MMD's left-handed convention (z mirrored, triangle winding reversed) on write.
"""
import os
import struct
import zlib
OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "tests", "data")
RINGS = [0.0, 2.5, 5.0, 7.5, 10.0]
CORNERS = [(1, 1), (-1, 1), (-1, -1), (1, -1)] # (x, z) counter-clockwise seen from +Y
def column():
"""Square column: 5 rings x 4 corners, outward faces CCW (right-handed)."""
verts = []
for y in RINGS:
for i, (x, z) in enumerate(CORNERS):
verts.append(((x, y, z), (x * 0.7071, 0.0, z * 0.7071), (i / 4.0, y / 10.0)))
tris = []
for r in range(len(RINGS) - 1):
for i in range(4):
a = r * 4 + i
b = r * 4 + (i + 1) % 4
c = a + 4
d = b + 4
# Seen from outside: a (bottom), b (bottom, next corner), d, c (top)
tris.append((a, c, b))
tris.append((b, c, d))
return verts, tris
def lh(v):
return (v[0], v[1], -v[2])
def write_png(path, size=8):
rows = b""
for y in range(size):
rows += b"\x00"
for x in range(size):
on = ((x // 2) + (y // 2)) % 2 == 0
rows += bytes((230, 60, 60, 255) if on else (240, 240, 240, 255))
def chunk(tag, data):
return struct.pack(">I", len(data)) + tag + data + struct.pack(">I", zlib.crc32(tag + data) & 0xFFFFFFFF)
png = b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", size, size, 8, 6, 0, 0, 0))
png += chunk(b"IDAT", zlib.compress(rows)) + chunk(b"IEND", b"")
with open(path, "wb") as f:
f.write(png)
def pmx_text(s):
b = s.encode("utf-16-le")
return struct.pack("<i", len(b)) + b
def write_pmx(path):
verts, tris = column()
out = b"PMX " + struct.pack("<f", 2.0) + bytes([8, 0, 0, 2, 1, 1, 1, 1, 1])
out += pmx_text("テストモデル") + pmx_text("TestModel") + pmx_text("comment") + pmx_text("")
out += struct.pack("<i", len(verts))
for idx, (p, n, uv) in enumerate(verts):
y = p[1]
out += struct.pack("<3f", *lh(p)) + struct.pack("<3f", *lh(n)) + struct.pack("<2f", *uv)
if y < 4:
out += bytes([0]) + struct.pack("<b", 0) # BDEF1 center
elif y < 6:
out += bytes([1]) + struct.pack("<bbf", 0, 1, 0.5) # BDEF2
elif y < 9:
out += bytes([3]) + struct.pack("<bbf", 1, 0, 1.0) + struct.pack("<9f", *([0.0] * 9)) # SDEF
else:
out += bytes([2]) + struct.pack("<bbbb", 1, -1, -1, -1) + struct.pack("<4f", 1, 0, 0, 0) # BDEF4
out += struct.pack("<f", 1.0)
out += struct.pack("<i", len(tris) * 3)
for a, b, c in tris:
out += struct.pack("<3H", a, c, b) # reverse winding for left-handed
out += struct.pack("<i", 1) + pmx_text("tex\\Checker.PNG")
out += struct.pack("<i", 1)
out += pmx_text("材質") + pmx_text("material")
out += struct.pack("<4f", 1, 1, 1, 1) + struct.pack("<3f", 0, 0, 0) + struct.pack("<f", 0)
out += struct.pack("<3f", 0.5, 0.5, 0.5) + bytes([0x10]) + struct.pack("<4f", 0, 0, 0, 1) + struct.pack("<f", 1)
out += struct.pack("<bb", 0, -1) + bytes([0]) + bytes([1, 0]) + pmx_text("") + struct.pack("<i", len(tris) * 3)
def bone(name, en, pos, parent, flags, extra):
return pmx_text(name) + pmx_text(en) + struct.pack("<3f", *lh(pos)) + struct.pack("<b", parent) + \
struct.pack("<i", 0) + struct.pack("<H", flags) + extra
base = 0x0002 | 0x0008 | 0x0010
out += struct.pack("<i", 4)
out += bone("センター", "center", (0, 0, 0), -1, base | 0x0001 | 0x0004, struct.pack("<b", 1))
out += bone("上半身", "upper", (0, 5, 0), 0, base, struct.pack("<3f", 0, 5, 0))
out += bone("目", "eye", (0, 9, 0), 1, base | 0x0100, struct.pack("<3f", 0, 1, 0) + struct.pack("<bf", 1, 0.5))
ik = struct.pack("<3f", 0, 0, 0) + struct.pack("<bif", 2, 10, 1.0) + struct.pack("<i", 1)
ik += struct.pack("<b", 1) + bytes([1]) + struct.pack("<6f", -1, 0, 0, 1, 0, 0)
out += bone("IK", "ik", (0, 10, 0), -1, base | 0x0004 | 0x0020, ik)
out += struct.pack("<i", 5)
out += pmx_text("あ") + pmx_text("mouth_a") + bytes([3, 1]) + struct.pack("<i", 4)
for i in range(16, 20):
out += struct.pack("<H", i) + struct.pack("<3f", *lh((1.0, 0.0, 0.5)))
out += pmx_text("グループ") + pmx_text("group") + bytes([4, 0]) + struct.pack("<i", 1) + struct.pack("<bf", 0, 0.5)
out += pmx_text("ボーン") + pmx_text("bone_morph") + bytes([4, 2]) + struct.pack("<i", 1)
out += struct.pack("<b", 1) + struct.pack("<3f", 0, 1, 0) + struct.pack("<4f", 0, 0, 0, 1)
out += pmx_text("UV") + pmx_text("uv_morph") + bytes([4, 3]) + struct.pack("<i", 1)
out += struct.pack("<H", 0) + struct.pack("<4f", 0.1, 0, 0, 0)
out += pmx_text("材質モーフ") + pmx_text("material_morph") + bytes([4, 8]) + struct.pack("<i", 1)
out += struct.pack("<b", 0) + bytes([0]) + struct.pack("<28f", *([0.0] * 28))
out += struct.pack("<i", 0) * 3 # display frames, rigid bodies, joints
with open(path, "wb") as f:
f.write(out)
def sjis(s, n):
b = s.encode("cp932")
assert len(b) <= n
return b + b"\x00" * (n - len(b))
def write_pmd(path):
verts, tris = column()
out = b"Pmd" + struct.pack("<f", 1.0) + sjis("テストPMD", 20) + sjis("comment", 256)
out += struct.pack("<I", len(verts))
for p, n, uv in verts:
y = p[1]
b0, b1, w = (0, 0, 100) if y < 4 else (0, 1, 50) if y < 6 else (0, 1, 0) # top: all weight on 2nd bone
out += struct.pack("<3f", *lh(p)) + struct.pack("<3f", *lh(n)) + struct.pack("<2f", *uv)
out += struct.pack("<HHBB", b0, b1, w, 0)
out += struct.pack("<I", len(tris) * 3)
for a, b, c in tris:
out += struct.pack("<3H", a, c, b)
out += struct.pack("<I", 1)
out += struct.pack("<4f", 1, 1, 1, 1) + struct.pack("<f", 5) + struct.pack("<3f", 0, 0, 0)
out += struct.pack("<3f", 0.5, 0.5, 0.5) + bytes([0, 1]) + struct.pack("<I", len(tris) * 3)
out += sjis("checker.png", 20)
out += struct.pack("<H", 2)
out += sjis("センター", 20) + struct.pack("<HHBH", 0xFFFF, 1, 1, 0) + struct.pack("<3f", 0, 0, 0)
out += sjis("上半身", 20) + struct.pack("<HHBH", 0, 0, 0, 0) + struct.pack("<3f", *lh((0, 5, 0)))
out += struct.pack("<H", 0) # IK
out += struct.pack("<H", 2) # morphs: base + one
top = list(range(16, 20))
out += sjis("base", 20) + struct.pack("<IB", len(top), 0)
for i in top:
out += struct.pack("<I", i) + struct.pack("<3f", *lh(verts[i][0]))
out += sjis("あ", 20) + struct.pack("<IB", len(top), 3)
for k in range(len(top)):
out += struct.pack("<I", k) + struct.pack("<3f", *lh((1.0, 0.0, 0.5)))
out += bytes([0]) + bytes([0]) + struct.pack("<I", 0) # display lists
out += bytes([0]) # no English names
for i in range(10):
out += sjis("toon%02d.bmp" % (i + 1), 100)
with open(path, "wb") as f:
f.write(out)
if __name__ == "__main__":
os.makedirs(os.path.join(OUT, "tex"), exist_ok=True)
write_png(os.path.join(OUT, "tex", "checker.png"))
write_png(os.path.join(OUT, "checker.png"))
write_pmx(os.path.join(OUT, "column.pmx"))
write_pmd(os.path.join(OUT, "column.pmd"))
print("wrote test models to", os.path.normpath(OUT))