# Shared helpers for the model fixture generators (obj.py, gltf.py): reference tools through nix. # assimp_info(ctx, paths, raw?) -> [ { meshes, materials, vertices, faces, nodes, min, max, perMesh = [ { vertices, faces } ] } ] # trimesh_metrics(ctx, paths) -> [ { area, faces, bounds, volume? } ] (trimesh, process=False, every geometry merged) # luau_encode(ctx, script) -> { name: bytes }: runs a Luau script (repo-relative, .luaurc aliases) that prints # "\t" lines: how a generator gets our encoder's output for the reference tools to read back. import json, os, re, subprocess, tempfile, shutil FLOAT = r"(-?[\d.]+(?:e[-+]?\d+)?|-?nan|-?inf)" def assimp_info(ctx, paths, raw=False): # raw: `assimp info -r` (no post-processing: no joined vertices, no merged identical meshes) out = [] for p in paths: try: txt = ctx.nix("assimp", ["assimp", "info", p] + (["-r"] if raw else [])).decode("utf-8", "replace") except subprocess.CalledProcessError: out.append({"failed": True}) continue def num(label): m = re.search(r"^" + label + r":\s+(\d+)", txt, re.M) return int(m.group(1)) if m else -1 def point(label): m = re.search(label + r"\s+\(" + FLOAT + " " + FLOAT + " " + FLOAT + r"\)", txt) return [float(m.group(i)) for i in (1, 2, 3)] if m else [] per = [{"vertices": int(a), "faces": int(c)} for a, b, c in re.findall(r"^\s+\d+ \(.*?\): \[(\d+) / (\d+) / (\d+) \|", txt, re.M)] out.append({ "meshes": num("Meshes"), "materials": num("Materials"), "vertices": num("Vertices"), "faces": num("Faces"), "nodes": num("Nodes"), "min": point("Minimum point"), "max": point("Maximum point"), "perMesh": per, }) return out TRIMESH = r""" import json, sys, trimesh, numpy as np res = [] for p in json.loads(sys.stdin.read()): try: m = trimesh.load(p, force="mesh", process=False) d = {"area": float(m.area), "faces": int(len(m.faces)), "bounds": [float(v) for v in np.asarray(m.bounds).ravel()] if len(m.faces) else []} except Exception as e: d = {"failed": True} res.append(d) print(json.dumps(res)) """ def trimesh_metrics(ctx, paths): out = ctx.python(["trimesh", "numpy", "networkx", "lxml", "pillow"], TRIMESH, input=json.dumps(paths).encode()) return json.loads(out) def luau_encode(ctx, script): exe = shutil.which("luau") cmd = [exe, script] if exe else ["nix", "shell", "nixpkgs#luau", "-c", "luau", script] res = subprocess.run(cmd, cwd=ctx.root, capture_output=True, check=True) files = {} for line in res.stdout.decode().split("\n"): if "\t" in line: name, hexdata = line.split("\t", 1) files[name] = bytes.fromhex(hexdata.strip()) return files def tempdir(): return tempfile.mkdtemp(prefix="formats-model-")