Files
rosetta/tools/fixtures/dae.py
T

107 lines
5.4 KiB
Python

# Collada fixtures (tools/fixtures.py): assimp's Collada test models (read from the nixpkgs assimp source at fixture
# time, never committed: test/models/Collada, licences in that folder's licence.md), glTF sample models converted to
# .dae by assimp's Collada exporter (skins, animations, cameras, textures), with reference values from `assimp info -r`
# (meshes, faces, vertices, bounds, materials, animations, cameras, lights, bones). Blender 5 has no Collada support.
# tests/gen/dae_cases.luau { name, main, data, files = { name -> bytes }, assimp, gltf? + gltfFiles? (the original
# of an assimp export: the test compares skins and animations with it) }
# tests/gen/dae_xcheck.luau our encoder's .dae of every case (tools/fixtures/_dae_encode.luau) read back by assimp:
# { name, case, assimp }
import importlib.util, os, re, subprocess
INPUTS = ["src/Formats/model/dae", "src/Formats/codecs/Xml", "tools/fixtures/_model_ref.py", "tools/fixtures/_dae_encode.luau",
"tests/fixtures/gltf"]
def ref_module(ctx):
spec = importlib.util.spec_from_file_location("_model_ref", os.path.join(ctx.root, "tools", "fixtures", "_model_ref.py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
def assimp_src():
out = subprocess.run(["nix", "build", "nixpkgs#assimp.src", "--no-link", "--print-out-paths"], capture_output=True, check=True)
return out.stdout.decode().strip().split("\n")[-1]
def info(ctx, M, path):
a = M.assimp_info(ctx, [path], raw=True)[0]
try:
txt = ctx.nix("assimp", ["assimp", "info", path, "-r"]).decode("utf-8", "replace")
except subprocess.CalledProcessError:
return {"failed": True}
for label in ("Animations", "Cameras", "Lights", "Bones", "Materials"):
m = re.search(r"^" + label + r":\s+(\d+)", txt, re.M)
if m:
a[label.lower()] = int(m.group(1))
# assimp's root node transform (its Collada importer bakes up axis / unit conversion into it): not reported by
# `assimp info`; the test applies the conversion itself
return a
IMAGE_EXT = (".jpg", ".jpeg", ".png", ".tga", ".bmp")
def generate(ctx):
M = ref_module(ctx)
src = os.path.join(assimp_src(), "test", "models", "Collada")
cases = []
images = {f: os.path.join(src, f) for f in sorted(os.listdir(src)) if f.lower().endswith(IMAGE_EXT)}
for f in sorted(os.listdir(src)):
if not (f.lower().endswith(".dae") or f.lower().endswith(".zae")) or "_triangulate" in f:
continue # the *_triangulate files are the same models again (fixture size)
path = os.path.join(src, f)
data = open(path, "rb").read()
files = {}
text = data.decode("latin-1")
for im, ip in images.items():
if im in text:
files[im] = open(ip, "rb").read()
cases.append({"name": "assimp/" + f, "main": f, "path": path, "files": files})
# glTF samples -> assimp's Collada exporter
tmp = M.tempdir()
groot = os.path.join(ctx.root, "tests", "fixtures", "gltf")
for model, variant, main in (("SimpleSkin", "glTF", "SimpleSkin.gltf"), ("RiggedSimple", "glTF-Binary", "RiggedSimple.glb"),
("BoxTextured", "glTF", "BoxTextured.gltf"), ("AnimatedTriangle", "glTF", "AnimatedTriangle.gltf"),
("Cameras", "glTF", "Cameras.gltf"), ("BoxVertexColors", "glTF-Binary", "BoxVertexColors.glb"),
("SimpleMeshes", "glTF", "SimpleMeshes.gltf"), ("InterpolationTest", "glTF-Binary", "InterpolationTest.glb"),
("MultiUVTest", "glTF-Binary", "MultiUVTest.glb")):
d = os.path.join(groot, model, variant)
if not os.path.exists(os.path.join(d, main)):
continue
out = os.path.join(tmp, model + ".dae")
try:
ctx.nix("assimp", ["assimp", "export", os.path.join(d, main), out, "-fcollada"])
except subprocess.CalledProcessError:
continue
files = {}
text = open(out, "rb").read().decode("latin-1")
for f in os.listdir(tmp):
if f.lower().endswith(IMAGE_EXT) and f in text:
files[f] = open(os.path.join(tmp, f), "rb").read()
for f in os.listdir(d):
if f.lower().endswith(IMAGE_EXT) and f in text:
files[f] = open(os.path.join(d, f), "rb").read()
orig = {f: open(os.path.join(d, f), "rb").read() for f in sorted(os.listdir(d)) if f != main}
cases.append({"name": "assimp-export/" + model, "main": model + ".dae", "path": out, "files": files,
"gltf": open(os.path.join(d, main), "rb").read(), "gltfFiles": orig})
for c in cases:
c["assimp"] = info(ctx, M, c["path"])
c["data"] = open(c["path"], "rb").read()
del c["path"]
ctx.emit("dae_cases", cases)
# assimp reads our output
ours = M.luau_encode(ctx, "tools/fixtures/_dae_encode.luau")
xtmp = M.tempdir()
names = []
for name, data in sorted(ours.items()):
with open(os.path.join(xtmp, name), "wb") as f:
f.write(data)
if name.endswith(".dae") or name.endswith(".zae"):
names.append(name)
out = []
for n in names:
out.append({"name": n, "case": int(n.split("_")[1].split(".")[0]), "assimp": info(ctx, M, os.path.join(xtmp, n))})
ctx.emit("dae_xcheck", out)