# X3D fixtures (tools/fixtures.py): assimp's X3D / X3DV / X3DB test models (test/models/X3D*, read from the nixpkgs
# assimp source, never committed), scenes and glTF sample models written by VTK's vtkX3DExporter, glTF sample models
# written by assimp's X3D exporter, and hand-written feature files (below), with assimp's X3D reader as the reference
# (`assimp info -r`: meshes, faces, materials; world bounds from its PreTransformVertices export). Then our encoder's
# X3D output (tools/fixtures/_vrml_encode.luau) read back by assimp.
# tests/gen/x3d_cases.luau { name, data, files = { name -> bytes }, assimp, primitives (bool) }
# tests/gen/x3d_xcheck.luau { name, case? / gltf? / wcase?, assimp }
import importlib.util, os, re, subprocess
INPUTS = ["src/Rosetta/codecs/model/Vrml", "src/Rosetta/model/x3d", "src/Rosetta/model/vrml", "tests/fixtures/gltf",
"tools/fixtures/_vrml_ref.py", "tools/fixtures/_model_ref.py", "tools/fixtures/fbx.py", "tools/fixtures/vrml.py",
"tools/fixtures/_vrml_encode.luau"]
FEATURES = {
"feat_sets.x3d": """
""",
"feat_proto.x3d": """
""",
"feat_classic.x3dv": """#X3D V3.3 utf8
PROFILE Interchange
COMPONENT Geometry2D:1
META "title" "classic"
UNIT length millimetre 0.001
DEF Root Transform { translation 1 2 3 children [
Shape { appearance Appearance { material Material { diffuseColor 0 1 0 } }
geometry IndexedFaceSet { coord Coordinate { point [ 0 0 0 1 0 0 1 1 0 0 1 0 ] } coordIndex [ 0 1 2 3 -1 ]
color ColorRGBA { color [ 1 0 0 1, 0 1 0 1, 0 0 1 1, 1 1 1 0.5 ] } } }
Shape { geometry IndexedTriangleSet { index [ 0 1 2 ] coord Coordinate { point [ 0 0 1 1 0 1 0 1 1 ] } } }
] }
""",
}
def load(ctx, name):
spec = importlib.util.spec_from_file_location(name, os.path.join(ctx.root, "tools", "fixtures", name + ".py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
def reference(ctx, M, Fbx, path, tmp):
a = M.assimp_info(ctx, [path], raw=True)[0]
if not a.get("failed"):
a.pop("min", None)
a.pop("max", None)
a.update(Fbx.assimp_extra(ctx, path))
a.update(Fbx.ptv_bounds(ctx, path, tmp))
return a
def generate(ctx):
M = load(ctx, "_model_ref")
Fbx = load(ctx, "fbx")
R = load(ctx, "_vrml_ref")
V = load(ctx, "vrml")
tmp = M.tempdir()
cases = []
root = os.path.join(Fbx.assimp_src(ctx), "test", "models")
for sub in ("X3D", "X3DV", "X3DB"):
d = os.path.join(root, sub)
for f in sorted(os.listdir(d)):
if f.lower().endswith((".x3d", ".x3dv", ".x3db")):
files = {}
if f.startswith("HelloWorld"):
files["earth-topo.png"] = open(os.path.join(d, "earth-topo.png"), "rb").read()
cases.append({"name": "assimp/%s/%s" % (sub, f), "path": os.path.join(d, f), "files": files})
picks = V.gltf_picks(ctx)
for name in R.vtk_export(ctx, tmp, [p["path"] for p in picks]):
cases.append({"name": "vtk/" + name + ".x3d", "path": os.path.join(tmp, name + ".x3d"), "files": {}})
for p in picks:
out = os.path.join(tmp, "assimp_" + p["name"].split("/")[0] + ".x3d")
try:
ctx.nix("assimp", ["assimp", "export", p["path"], out, "-fx3d"])
except subprocess.CalledProcessError:
continue
cases.append({"name": "assimp-export/" + os.path.basename(out), "path": out, "files": dict(p["files"])})
for name, text in sorted(FEATURES.items()):
path = os.path.join(tmp, name)
with open(path, "w") as f:
f.write(text)
cases.append({"name": "feature/" + name, "path": path, "files": {}})
for c in cases:
c["data"] = open(c["path"], "rb").read()
c["primitives"] = V.primitives(c["data"])
c["assimp"] = reference(ctx, M, Fbx, c["path"], tmp) if c["path"].endswith(".x3d") else {"skip": True}
del c["path"]
ctx.emit("x3d_cases", cases)
# assimp reads our X3D output
ours = M.luau_encode(ctx, "tools/fixtures/_vrml_encode.luau")
xtmp = M.tempdir()
out = []
for name in sorted(n for n in ours if n.endswith(".x3d")):
path = os.path.join(xtmp, name)
with open(path, "wb") as f:
# assimp 6 cannot read quoted MFString urls (url='"a.png"', which the X3D encoding requires)
f.write(re.sub(rb'url=""([^&"]*)""', rb'url="\1"', ours[name]))
e = {"name": name, "assimp": reference(ctx, M, Fbx, path, xtmp)}
m = re.match(r"(xcase|xgltf|wcase)_(\d+)\.x3d$", name)
if m:
e[m.group(1)] = int(m.group(2))
out.append(e)
ctx.emit("x3d_xcheck", out)