Files
rosetta/tools/fixtures/3ds.py
T

102 lines
4.3 KiB
Python

# 3DS fixtures (tools/fixtures.py): assimp's 3DS test models (test/models/3DS and test/models-nonbsd/3DS: meshes,
# materials, textures, keyframer hierarchies, pivots, rotation / camera / target animation, cameras, lights; read from
# the nixpkgs assimp source, never committed) and files written by assimp's 3DS exporter from glTF sample models, with
# reference values from assimp (`assimp info -r`: meshes, faces, vertices, materials, animations, cameras, lights,
# nodes; world bounds from its PreTransformVertices export). Then our encoder's output (tools/fixtures/_3ds_encode.luau,
# from every case and the glTF models) read back by assimp. (Blender 5's 3DS importer is an extension that is not
# installed by default, so assimp is the only reference.)
# tests/gen/3ds_cases.luau { name, data, assimp }
# tests/gen/3ds_gltf.luau glTF sample models for the writer round trips: { name, data, files }
# tests/gen/3ds_xcheck.luau { name, case? | gltf?, assimp }
import importlib.util, os
INPUTS = ["src/Rosetta/model/3ds", "tests/fixtures/gltf", "tools/fixtures/_model_ref.py", "tools/fixtures/fbx.py",
"tools/fixtures/_3ds_encode.luau"]
def load(ctx, name):
spec = importlib.util.spec_from_file_location("_" + name.replace(".", "_"), os.path.join(ctx.root, "tools", "fixtures", name + ".py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
GLTF = ("Box", "BoxTextured", "BoxVertexColors", "Cameras", "SimpleMeshes", "RiggedSimple", "SimpleSkin", "MultiUVTest",
"NegativeScaleTest", "PointLightIntensityTest", "AnimatedTriangle", "TextureCoordinateTest", "TextureTransformTest",
"InterpolationTest", "LightVisibility")
def reference(ctx, M, X, paths, tmp):
out = []
for p, a in zip(paths, M.assimp_info(ctx, paths, raw=True)):
a.update(X.assimp_extra(ctx, p))
a.pop("min", None)
a.pop("max", None)
a.pop("perMesh", None)
if not a.get("failed"):
a.update(X.ptv_bounds(ctx, p, tmp))
out.append(a)
return out
def generate(ctx):
M = load(ctx, "_model_ref")
X = load(ctx, "fbx")
tmp = M.tempdir()
src = X.assimp_src(ctx)
cases = []
for sub in ("models", "models-nonbsd"):
d = os.path.join(src, "test", sub, "3DS")
for f in sorted(os.listdir(d)):
if f.lower().endswith(".3ds"):
cases.append({"name": "assimp/%s/%s" % (sub, f), "path": os.path.join(d, f)})
# assimp's exporter
gl = os.path.join(ctx.root, "tests", "fixtures", "gltf")
picks = []
for model in GLTF:
d0 = os.path.join(gl, model)
if not os.path.isdir(d0):
continue
for variant in sorted(os.listdir(d0)):
d = os.path.join(d0, variant)
if not os.path.isdir(d):
continue
mains = [f for f in sorted(os.listdir(d)) if f.endswith(".gltf") or f.endswith(".glb")]
if not mains:
continue
main = os.path.join(d, mains[0])
picks.append({"name": model + "/" + variant, "data": open(main, "rb").read(),
"files": {f: open(os.path.join(d, f), "rb").read() for f in sorted(os.listdir(d)) if f != mains[0]}})
out = os.path.join(tmp, model + ".3ds")
try:
ctx.nix("assimp", ["assimp", "export", main, out, "-f3ds"])
cases.append({"name": "assimp-export/" + model + ".3ds", "path": out})
except Exception:
pass
break
refs = reference(ctx, M, X, [c["path"] for c in cases], tmp)
for c, a in zip(cases, refs):
c["assimp"] = a
c["data"] = open(c["path"], "rb").read()
del c["path"]
ctx.emit("3ds_cases", cases)
ctx.emit("3ds_gltf", picks)
# assimp reads our files
ours = M.luau_encode(ctx, "tools/fixtures/_3ds_encode.luau")
xtmp = M.tempdir()
names = sorted(n for n in ours if n.endswith(".3ds"))
paths = []
for n in names:
p = os.path.join(xtmp, n)
with open(p, "wb") as f:
f.write(ours[n])
paths.append(p)
xs = []
for n, a in zip(names, reference(ctx, M, X, paths, xtmp)):
kind, idx = n[:-4].split("_")
e = {"name": n, "assimp": a}
e["case" if kind == "case" else "gltf"] = int(idx)
xs.append(e)
ctx.emit("3ds_xcheck", xs)