# Valve SMD fixtures (tools/fixtures.py): SMD files written here (a skeleton of random bones, triangles with Source # links / GoldSrc parent-only vertices, several materials, a multi-frame skeleton, comments, quoted names; an # animation-only SMD); references: assimp's triangle soup (positions and texture coordinates per material mesh) # and the writer's own values (bind pose TRS, per-vertex weights, frame poses); our encoder's output # (tools/fixtures/_smd_encode.luau) read back by assimp (vertex / face counts, bounds). # tests/gen/smd_src.luau { { name, data } } # tests/gen/smd.luau { cases = { { name, data, bones, frames, tris, materials, verts, assimp } }, ours = { [name] = assimp info } } import importlib.util, json, math, os, shutil, struct, subprocess, tempfile INPUTS = ["tools/fixtures/_quake_ref.py", "tools/fixtures/_smd_encode.luau", "src/Rosetta/model/smd"] def mod(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 angle_quat(x, y, z): sy, cy = math.sin(z / 2), math.cos(z / 2) sp, cp = math.sin(y / 2), math.cos(y / 2) sr, cr = math.sin(x / 2), math.cos(x / 2) return [sr * cp * cy - cr * sp * sy, cr * sp * cy + sr * cp * sy, cr * cp * sy - sr * sp * cy, cr * cp * cy + sr * sp * sy] def r6(v): return float("%.6f" % v) def smd(rng, nbones, nframes, ntris, materials, links=True, anim_only=False): lines = ["// written by tools/fixtures/smd.py", "version 1", "nodes"] bones = [] for i in range(nbones): parent = -1 if i == 0 else rng.randrange(i) bones.append({"name": "bone %d" % i if i % 2 else "bone_%d" % i, "parent": parent + 1}) lines.append('%d "%s" %d' % (i, bones[-1]["name"], parent)) lines.append("end") lines.append("skeleton") frames = [] for f in range(nframes): lines.append("time %d" % f) pose = [] for i in range(nbones): v = [r6(rng.uniform(-5, 5)) for _ in range(3)] + [r6(rng.uniform(-math.pi, math.pi)) for _ in range(3)] lines.append("%d %s" % (i, " ".join("%.6f" % x for x in v))) pose.append({"t": v[:3], "q": angle_quat(*v[3:])}) frames.append(pose) lines.append("end") tris = [] if not anim_only: lines.append("triangles") for t in range(ntris): mat = materials[t % len(materials)] lines.append(mat) corners = [] for c in range(3): parent = rng.randrange(nbones) p = [r6(rng.uniform(-20, 20)) for _ in range(3)] n = [rng.uniform(-1, 1) for _ in range(3)] ln = math.sqrt(sum(x * x for x in n)) n = [r6(x / ln) for x in n] uv = [r6(rng.random()), r6(rng.random())] s = "%d %s" % (parent, " ".join("%.6f" % x for x in p + n + uv)) weights = {parent: 1.0} if links and rng.random() < 0.7: k = rng.randrange(1, 3) picks = rng.sample(range(nbones), min(k, nbones)) ws = [r6(rng.uniform(0.1, 0.45)) for _ in picks] s += " %d %s" % (len(picks), " ".join("%d %.6f" % (b, w) for b, w in zip(picks, ws))) weights = {} for b, w in zip(picks, ws): weights[b] = weights.get(b, 0) + w rest = 1 - sum(ws) if rest > 0.001: weights[parent] = weights.get(parent, 0) + rest lines.append(s) corners.append({"p": p, "n": n, "uv": uv, "w": [[b, w] for b, w in sorted(weights.items(), key=lambda x: -x[1])]}) tris.append({"material": mat, "corners": corners}) lines.append("end") return "\r\n".join(lines) + "\r\n", {"bones": bones, "frames": frames, "tris": tris} def assimp_info(ctx, data, Q): tmp = tempfile.mkdtemp(prefix="formats-smd-") try: p = os.path.join(tmp, "in.smd") open(p, "wb").write(data) try: txt = ctx.nix("assimp", ["assimp", "info", p]).decode("utf-8", "replace") except subprocess.CalledProcessError: return {"failed": True} finally: shutil.rmtree(tmp) import re def num(label): m = re.search(r"^" + label + r":\s+(\d+)", txt, re.M) return int(m.group(1)) if m else -1 return {"faces": num("Faces"), "bones": num("Bones"), "animations": num("Animations")} def generate(ctx): rng = ctx.rng(83) Q = mod(ctx, "_quake_ref") specs = [("source", 5, 3, 12, ["skin.bmp", "metal.vtf"], True, False), ("goldsrc", 3, 1, 8, ["chrome.bmp"], False, False), ("animation", 4, 6, 0, [], True, True)] cases, src = [], [] for name, nb, nf, nt, mats, links, anim in specs: text, info = smd(rng, nb, nf, nt, mats, links, anim) data = text.encode() src.append({"name": name, "data": data}) info.update({"name": name, "data": data, "assimp": Q.assimp_soup(ctx, data, "smd") if not anim else {"failed": True}, "materials": mats}) cases.append(info) ctx.emit("smd_src", src) import importlib ours = {} m = mod(ctx, "_model_ref") for k, b in m.luau_encode(ctx, "tools/fixtures/_smd_encode.luau").items(): ours[k] = assimp_info(ctx, b, Q) ours[k]["soup"] = Q.assimp_soup(ctx, b, "smd") if not k.endswith("-anim") else {"failed": True} ctx.emit("smd", {"cases": cases, "ours": ours})