ws-game: model/md2 (Quake II MD2 -> Scene: frames as morph targets + grouped animations, skins as URIs); codecs/model/Rig.normals
This commit is contained in:
1 parent
ec32f359bc
commit
de1d2c2bba
8 files changed
+499
No files matched your search
@@ -0,0 +1,35 @@
|
||||
# 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)
|
||||
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 = [], [], 0, 0
|
||||
for m in j.get("meshes", []):
|
||||
if m.get("primitivetypes") != 4:
|
||||
continue
|
||||
meshes += 1
|
||||
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}
|
||||
Reference in new issue
Block a user