37 lines
1.5 KiB
Python
37 lines
1.5 KiB
Python
# Shared helpers for the Quake-family model fixtures (md2.py, md3.py, mdl.py): assimp's triangle soup.
|
|
# assimp_soup(ctx, data, ext) -> { pos = [floats], uv = [floats], meshes, faces } | { failed = True }: every triangle
|
|
# mesh of `assimp export -f assjson`, faces in order, each face's corners in assimp's index order (positions x3,
|
|
# texture coordinates u, v of set 0); per = [ { name, start } ]: each mesh's name and first face
|
|
import json, os, shutil, subprocess, tempfile
|
|
|
|
|
|
def assimp_soup(ctx, data, ext):
|
|
tmp = tempfile.mkdtemp(prefix="formats-quake-")
|
|
try:
|
|
src = os.path.join(tmp, "in." + ext)
|
|
open(src, "wb").write(data)
|
|
out = os.path.join(tmp, "out.json")
|
|
try:
|
|
ctx.nix("assimp", ["assimp", "export", src, out, "-f", "assjson"])
|
|
except subprocess.CalledProcessError:
|
|
return {"failed": True}
|
|
j = json.load(open(out))
|
|
finally:
|
|
shutil.rmtree(tmp)
|
|
pos, uv, faces, meshes, per = [], [], 0, 0, []
|
|
for m in j.get("meshes", []):
|
|
if m.get("primitivetypes") != 4:
|
|
continue
|
|
meshes += 1
|
|
per.append({"name": m.get("name", ""), "start": faces})
|
|
v = m["vertices"]
|
|
t = (m.get("texturecoords") or [[]])[0]
|
|
nc = (m.get("numuvcomponents") or [2])[0]
|
|
for f in m["faces"]:
|
|
faces += 1
|
|
for i in f:
|
|
pos += v[i * 3:i * 3 + 3]
|
|
if t:
|
|
uv += t[i * nc:i * nc + 2]
|
|
return {"pos": pos, "uv": uv, "faces": faces, "meshes": meshes, "per": per}
|