Skip to content

armgit / forma

Public Imported from GitHub
0

A 1.2 MB pure-Rust rewrite of headless Blender built for AI agents — 3D B-rep modeling engine, CLI, and MCP server

19.8 KB
Raw Blame
"""12-task 3D modeling benchmark suite (Hard-surface booleans, Assemblies, Subdiv/Organic).

Coordinate convention: right-handed Z-up, units in meters.
Each task defines:
  - id, category, title
  - prompt: exact geometric specification given identically to every model & backend
  - expected_bbox: ((xmin, ymin, zmin), (xmax, ymax, zmax)) tolerance-checked
  - ref_bpy: canonical Blender 5.3 script generating the ground-truth reference mesh
"""
from __future__ import annotations

import os
import shutil
import subprocess
import sys
import tempfile
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, List, Tuple

BLENDER_BIN = (
    os.environ.get("BLENDER_BIN")
    or shutil.which("blender")
    or "/Applications/Blender.app/Contents/MacOS/Blender"
)
REF_DIR = Path(__file__).resolve().parent / "ref_meshes"


@dataclass
class TaskSpec:
    id: str
    category: str
    title: str
    prompt: str
    expected_bbox: Tuple[Tuple[float, float, float], Tuple[float, float, float]]
    ref_bpy: str


HEADER_BPY = """
import bpy, bmesh, math
from mathutils import Vector, Matrix

bpy.ops.wm.read_factory_settings(use_empty=True)

def make_obj(name, bm):
    mesh = bpy.data.meshes.new(name)
    bm.normal_update()
    bm.to_mesh(mesh)
    bm.free()
    obj = bpy.data.objects.new(name, mesh)
    bpy.context.collection.objects.link(obj)
    return obj

def bool_op(target, cutter, op='DIFFERENCE'):
    mod = target.modifiers.new(name="Bool", type='BOOLEAN')
    mod.operation = op
    mod.solver = 'MANIFOLD'
    mod.object = cutter
    bpy.context.view_layer.objects.active = target
    bpy.ops.object.modifier_apply(modifier=mod.name)
    bpy.data.objects.remove(cutter, do_unlink=True)
    return target

def box(name, sx, sy, sz, loc=(0,0,0)):
    bm = bmesh.new()
    bmesh.ops.create_cube(bm, size=1.0, matrix=Matrix.Translation(loc) @ Matrix.Diagonal((sx, sy, sz, 1.0)))
    return make_obj(name, bm)

def cyl(name, r, h, segs=32, loc=(0,0,0), rot=(0,0,0)):
    bm = bmesh.new()
    m = Matrix.Translation(loc) @ Matrix.Rotation(rot[0], 4, 'X') @ Matrix.Rotation(rot[1], 4, 'Y') @ Matrix.Rotation(rot[2], 4, 'Z')
    bmesh.ops.create_cone(bm, cap_ends=True, cap_tris=False, segments=segs, radius1=r, radius2=r, depth=h, matrix=m)
    return make_obj(name, bm)

def cone(name, r1, r2, h, segs=32, loc=(0,0,0)):
    bm = bmesh.new()
    bmesh.ops.create_cone(bm, cap_ends=True, cap_tris=False, segments=segs, radius1=r1, radius2=r2, depth=h, matrix=Matrix.Translation(loc))
    return make_obj(name, bm)

def sphere(name, r, u=32, v=16, loc=(0,0,0)):
    bm = bmesh.new()
    bmesh.ops.create_uvsphere(bm, u_segments=u, v_segments=v, radius=r, matrix=Matrix.Translation(loc))
    return make_obj(name, bm)
"""

EXPORT_FOOTER = """
bpy.ops.export_scene.gltf(filepath=OUT_PATH, export_format='GLB', export_yup=False)
"""


TASKS: List[TaskSpec] = [
    # --- Category A: Hard-Surface Booleans ---
    TaskSpec(
        id="flange_plate",
        category="boolean",
        title="Mounting Flange Plate",
        prompt=(
            "Model a solid rectangular mounting flange plate centered at the origin (0, 0, 0) in Z-up coordinates: "
            "dimensions X=4.0, Y=2.0, Z=0.4 (spanning X in [-2, 2], Y in [-1, 1], Z in [-0.2, 0.2]). "
            "Cut a centered circular through-hole along Z of radius 0.5 at (0, 0), and 4 circular bolt through-holes "
            "along Z of radius 0.25 at (1.4, 0.5), (-1.4, 0.5), (1.4, -0.5), and (-1.4, -0.5). "
            "Result must be a single watertight manifold mesh."
        ),
        expected_bbox=((-2.0, -1.0, -0.2), (2.0, 1.0, 0.2)),
        ref_bpy="""
base = box("flange", 4.0, 2.0, 0.4, (0, 0, 0))
bool_op(base, cyl("bore", 0.5, 1.0, 32, (0, 0, 0)), 'DIFFERENCE')
for x in (-1.4, 1.4):
    for y in (-0.5, 0.5):
        bool_op(base, cyl("bolt", 0.25, 1.0, 24, (x, y, 0)), 'DIFFERENCE')
""",
    ),
    TaskSpec(
        id="l_bracket",
        category="boolean",
        title="Drilled L-Bracket",
        prompt=(
            "Model a solid L-shaped bracket occupying X in [-1.0, 1.0], Y in [-0.5, 0.5], Z in [-1.0, 1.0] (Z-up): "
            "it consists of a horizontal bottom plate spanning X in [-1.0, 1.0], Y in [-0.5, 0.5], Z in [-1.0, -0.6] (thickness 0.4) "
            "unioned cleanly with a vertical back wall spanning X in [-1.0, -0.6], Y in [-0.5, 0.5], Z in [-1.0, 1.0] (thickness 0.4). "
            "Cut two circular through-holes of radius 0.25: one vertically (along Z) through the horizontal plate at (0.4, 0.0), "
            "and one horizontally (along X) through the vertical wall at (y=0.0, z=0.4). Single watertight manifold mesh."
        ),
        expected_bbox=((-1.0, -0.5, -1.0), (1.0, 0.5, 1.0)),
        ref_bpy="""
horiz = box("horiz", 2.0, 1.0, 0.4, (0.0, 0.0, -0.8))
vert = box("vert", 0.4, 1.0, 2.0, (-0.8, 0.0, 0.0))
base = bool_op(horiz, vert, 'UNION')
bool_op(base, cyl("h1", 0.25, 1.0, 24, (0.4, 0.0, -0.8)), 'DIFFERENCE')
bool_op(base, cyl("h2", 0.25, 1.0, 24, (-0.8, 0.0, 0.4), rot=(0, math.pi/2, 0)), 'DIFFERENCE')
""",
    ),
    TaskSpec(
        id="pipe_tee",
        category="boolean",
        title="Hollow Pipe T-Junction",
        prompt=(
            "Model a hollow T-junction pipe in Z-up coordinates: "
            "a horizontal main pipe along X spanning X in [-2.0, 2.0] centered on Y=0, Z=0 with outer radius 0.6 and inner bore radius 0.4; "
            "joined cleanly with a vertical branch pipe along +Z spanning Z in [0.0, 2.0] centered on X=0, Y=0 with outer radius 0.5 and inner bore radius 0.32. "
            "All three pipe ends are open and connect internally through the hollow interior. Single watertight manifold mesh."
        ),
        expected_bbox=((-2.0, -0.6, -0.6), (2.0, 0.6, 2.0)),
        ref_bpy="""
outer_h = cyl("oh", 0.6, 4.0, 32, (0, 0, 0), rot=(0, math.pi/2, 0))
outer_v = cyl("ov", 0.5, 2.0, 32, (0, 0, 1.0))
body = bool_op(outer_h, outer_v, 'UNION')
inner_h = cyl("ih", 0.4, 4.4, 32, (0, 0, 0), rot=(0, math.pi/2, 0))
inner_v = cyl("iv", 0.32, 2.4, 32, (0, 0, 1.2))
bool_op(body, inner_h, 'DIFFERENCE')
bool_op(body, inner_v, 'DIFFERENCE')
""",
    ),
    TaskSpec(
        id="slotted_block",
        category="boolean",
        title="Slotted Guide Block",
        prompt=(
            "Model a solid rectangular guide block centered at the origin (Z-up) with dimensions X=3.0, Y=2.0, Z=1.2 "
            "(X in [-1.5, 1.5], Y in [-1.0, 1.0], Z in [-0.6, 0.6]). "
            "Cut a straight channel slot running along the full X length on the top face, centered on Y=0, width 1.0 (Y in [-0.5, 0.5]) "
            "and depth 0.5 from the top (Z in [0.1, 0.6]). "
            "Also cut a circular through-hole along the Y axis of radius 0.25 centered at X=0.0, Z=-0.2. Single watertight manifold mesh."
        ),
        expected_bbox=((-1.5, -1.0, -0.6), (1.5, 1.0, 0.6)),
        ref_bpy="""
base = box("block", 3.0, 2.0, 1.2, (0, 0, 0))
slot = box("slot", 3.4, 1.0, 0.7, (0, 0, 0.45))
bool_op(base, slot, 'DIFFERENCE')
hole = cyl("hole", 0.25, 2.6, 24, (0, 0, -0.2), rot=(math.pi/2, 0, 0))
bool_op(base, hole, 'DIFFERENCE')
""",
    ),

    # --- Category B: Multi-Part / Parametric Assemblies & Precision Features ---
    TaskSpec(
        id="stepped_shaft",
        category="assembly",
        title="Stepped Precision Axle",
        prompt=(
            "Model a concentric stepped cylindrical shaft aligned with the Z axis, resting on Z=0 (total height 3.6, centered at X=0, Y=0): "
            "5 stacked cylindrical sections unioned into one manifold solid: "
            "1) bottom flange: radius 0.9, Z in [0.0, 0.4]; "
            "2) lower shaft: radius 0.5, Z in [0.4, 1.6]; "
            "3) middle collar: radius 0.75, Z in [1.6, 2.2]; "
            "4) upper shaft: radius 0.4, Z in [2.2, 3.2]; "
            "5) top cap: radius 0.25, Z in [3.2, 3.6]. Single watertight manifold mesh."
        ),
        expected_bbox=((-0.9, -0.9, 0.0), (0.9, 0.9, 3.6)),
        ref_bpy="""
s1 = cyl("s1", 0.9, 0.4, 32, (0, 0, 0.2))
s2 = cyl("s2", 0.5, 1.24, 32, (0, 0, 1.0))
s3 = cyl("s3", 0.75, 0.64, 32, (0, 0, 1.9))
s4 = cyl("s4", 0.4, 1.04, 32, (0, 0, 2.7))
s5 = cyl("s5", 0.25, 0.44, 32, (0, 0, 3.4))
# Use exact spans with tiny overlap only inside larger cylinder so outer heights stay exact
bpy.data.objects.remove(s2, do_unlink=True); bpy.data.objects.remove(s3, do_unlink=True)
bpy.data.objects.remove(s4, do_unlink=True); bpy.data.objects.remove(s5, do_unlink=True)
base = s1
for r, z0, z1 in [(0.5, 0.4, 1.6), (0.75, 1.6, 2.2), (0.4, 2.2, 3.2), (0.25, 3.2, 3.6)]:
    bool_op(base, cyl("sec", r, z1 - z0, 32, (0, 0, 0.5 * (z0 + z1))), 'UNION')
""",
    ),
    TaskSpec(
        id="table_frame",
        category="assembly",
        title="Four-Legged Table",
        prompt=(
            "Model a minimalist four-legged table standing on Z=0, centered in X and Y (Z-up): "
            "a rectangular tabletop of size X=2.4, Y=1.4, Z=0.12 occupying Z in [0.88, 1.00] (X in [-1.2, 1.2], Y in [-0.7, 0.7]); "
            "and 4 square legs of cross-section 0.12 x 0.12 spanning Z in [0.0, 0.88], inset 0.10 from the outer edges "
            "(so leg centers are at X = ±1.04, Y = ±0.54). Union all parts cleanly into a single watertight manifold mesh."
        ),
        expected_bbox=((-1.2, -0.7, 0.0), (1.2, 0.7, 1.0)),
        ref_bpy="""
top = box("top", 2.4, 1.4, 0.12, (0, 0, 0.94))
for sx in (-1.04, 1.04):
    for sy in (-0.54, 0.54):
        bool_op(top, box("leg", 0.12, 0.12, 0.88, (sx, sy, 0.44)), 'UNION')
""",
    ),
    TaskSpec(
        id="beveled_enclosure",
        category="assembly",
        title="Beveled Electronics Enclosure",
        prompt=(
            "Model an open-top rectangular electronics enclosure resting on Z=0, centered in X and Y (Z-up): "
            "outer dimensions X=2.4, Y=1.6, Z=1.0 (X in [-1.2, 1.2], Y in [-0.8, 0.8], Z in [0.0, 1.0]). "
            "Bevel the 4 vertical outer corner edges with width 0.15 and 3 segments. "
            "Hollow out the interior from the top leaving a wall and floor thickness of 0.15 (inner cavity spanning Z in [0.15, 1.0]). "
            "Single watertight manifold mesh."
        ),
        expected_bbox=((-1.2, -0.8, 0.0), (1.2, 0.8, 1.0)),
        ref_bpy="""
bm = bmesh.new()
bmesh.ops.create_cube(bm, size=1.0, matrix=Matrix.Translation((0, 0, 0.5)) @ Matrix.Diagonal((2.4, 1.6, 1.0, 1.0)))
vedges = [e for e in bm.edges if abs(e.verts[0].co.z - e.verts[1].co.z) > 0.5]
bmesh.ops.bevel(bm, geom=vedges, offset=0.15, offset_type='OFFSET', segments=3, profile=0.5, affect='EDGES')
outer = make_obj("outer", bm)
bm2 = bmesh.new()
bmesh.ops.create_cube(bm2, size=1.0, matrix=Matrix.Translation((0, 0, 0.65)) @ Matrix.Diagonal((2.1, 1.3, 1.0, 1.0)))
vedges2 = [e for e in bm2.edges if abs(e.verts[0].co.z - e.verts[1].co.z) > 0.5]
bmesh.ops.bevel(bm2, geom=vedges2, offset=0.08, offset_type='OFFSET', segments=3, profile=0.5, affect='EDGES')
inner = make_obj("inner", bm2)
bool_op(outer, inner, 'DIFFERENCE')
""",
    ),
    TaskSpec(
        id="hex_nut",
        category="assembly",
        title="Chamfered Hex Nut",
        prompt=(
            "Model a hexagonal nut centered at the origin (0, 0, 0) in Z-up coordinates: "
            "a regular 6-sided prism with circumradius R=1.0 (vertices at angles k*60° around Z so X spans [-1.0, 1.0], Y spans [-0.866, 0.866]) "
            "and height 0.8 (Z in [-0.4, 0.4]), with its 6 top outer rim edges and 6 bottom outer rim edges beveled/chamfered by width 0.1 (1 segment). "
            "Cut a centered circular through-bore along Z of radius 0.5. Single watertight manifold mesh."
        ),
        expected_bbox=((-1.0, -0.866, -0.4), (1.0, 0.866, 0.4)),
        ref_bpy="""
bm = bmesh.new()
bmesh.ops.create_cone(bm, cap_ends=True, cap_tris=False, segments=6, radius1=1.0, radius2=1.0, depth=0.8, matrix=Matrix.Rotation(math.pi/6, 4, 'Z'))
rim_edges = [e for e in bm.edges if abs(e.verts[0].co.z - e.verts[1].co.z) < 1e-4]
bmesh.ops.bevel(bm, geom=rim_edges, offset=0.1, offset_type='OFFSET', segments=1, profile=0.5, affect='EDGES')
nut = make_obj("nut", bm)
bool_op(nut, cyl("bore", 0.5, 1.2, 32, (0, 0, 0)), 'DIFFERENCE')
""",
    ),

    # --- Category C: Subdivision / Organic & Rotational Forms ---
    TaskSpec(
        id="subdiv_mug",
        category="subdiv",
        title="Coffee Mug with Handle",
        prompt=(
            "Model a coffee mug standing on Z=0, centered at X=0, Y=0 (Z-up): "
            "a hollow cylindrical cup body with outer radius 0.6, inner radius 0.48, height 1.4 (Z in [0.0, 1.4]), "
            "and solid floor thickness 0.14 (cavity Z in [0.14, 1.4]). "
            "Attach a C-shaped handle on the +X side spanning Z in [0.35, 1.05] and extending outward to X=1.05 "
            "(handle tube radius ~0.08). Smooth cleanly so the result is a single watertight manifold mesh."
        ),
        expected_bbox=((-0.6, -0.6, 0.0), (1.05, 0.6, 1.4)),
        ref_bpy="""
outer = cyl("cup", 0.6, 1.4, 32, (0, 0, 0.7))
# Build smooth C-handle on +X from z=0.35 to z=1.05 extending to x=1.05
bm = bmesh.new()
n_arc = 16
n_ring = 12
r_tube = 0.08
# Elliptical half-loop centered at (0.55, 0, 0.70), rx=0.42 (reaches 0.55+0.42+0.08=1.05), rz=0.35
rings = []
for i in range(n_arc + 1):
    t = -math.pi/2 + math.pi * (i / n_arc)
    cx = 0.55 + 0.42 * math.cos(t)
    cz = 0.70 + 0.35 * math.sin(t)
    tx = -0.42 * math.sin(t)
    tz = 0.35 * math.cos(t)
    tangent = Vector((tx, 0.0, tz)).normalized()
    normal = Vector((tz, 0.0, -tx)).normalized()
    binormal = Vector((0.0, 1.0, 0.0))
    ring = []
    for j in range(n_ring):
        a = 2 * math.pi * j / n_ring
        pt = Vector((cx, 0.0, cz)) + normal * (r_tube * math.cos(a)) + binormal * (r_tube * math.sin(a))
        ring.append(bm.verts.new(pt))
    rings.append(ring)
for i in range(n_arc):
    for j in range(n_ring):
        jn = (j + 1) % n_ring
        bm.faces.new((rings[i][j], rings[i+1][j], rings[i+1][jn], rings[i][jn]))
bm.faces.new(list(reversed(rings[0])))
bm.faces.new(rings[-1])
bmesh.ops.recalc_face_normals(bm, faces=bm.faces[:])
handle = make_obj("handle", bm)
body = bool_op(outer, handle, 'UNION')
inner = cyl("cavity", 0.48, 1.4, 32, (0, 0, 0.14 + 0.7))
bool_op(body, inner, 'DIFFERENCE')
""",
    ),
    TaskSpec(
        id="pedestal_bowl",
        category="subdiv",
        title="Smooth Pedestal Bowl",
        prompt=(
            "Model a rotationally symmetric smooth pedestal bowl standing on Z=0, centered at X=0, Y=0 (Z-up), total height 1.3: "
            "circular foot (radius 0.55, Z in [0.0, 0.15]), slender cylindrical stem (radius 0.22, Z in [0.15, 0.60]), "
            "and a flared hollow bowl spanning Z in [0.60, 1.30] tapering from outer radius 0.35 at Z=0.60 to outer radius 1.0 at Z=1.30, "
            "with wall thickness ~0.10 (inner cavity bottom at Z=0.70). Single watertight manifold mesh."
        ),
        expected_bbox=((-1.0, -1.0, 0.0), (1.0, 1.0, 1.3)),
        ref_bpy="""
# Build revolved profile cleanly
profile = [
    (0.0, 0.0),
    (0.55, 0.0),
    (0.55, 0.15),
    (0.22, 0.15),
    (0.22, 0.60),
    (0.35, 0.60),
    (1.00, 1.30),
    (0.90, 1.30),
    (0.30, 0.70),
    (0.0, 0.70),
]
bm = bmesh.new()
segs = 48
rings = []
v_bot = bm.verts.new((0, 0, 0.0))
v_in_bot = bm.verts.new((0, 0, 0.70))
pts = profile[1:-1]
for r, z in pts:
    ring = [bm.verts.new((r * math.cos(2*math.pi*j/segs), r * math.sin(2*math.pi*j/segs), z)) for j in range(segs)]
    rings.append(ring)
for j in range(segs):
    jn = (j + 1) % segs
    bm.faces.new((v_bot, rings[0][jn], rings[0][j]))
for i in range(len(rings) - 1):
    for j in range(segs):
        jn = (j + 1) % segs
        bm.faces.new((rings[i][j], rings[i][jn], rings[i+1][jn], rings[i+1][j]))
for j in range(segs):
    jn = (j + 1) % segs
    bm.faces.new((v_in_bot, rings[-1][j], rings[-1][jn]))
bmesh.ops.recalc_face_normals(bm, faces=bm.faces[:])
obj = make_obj("bowl", bm)
""",
    ),
    TaskSpec(
        id="arched_bridge",
        category="subdiv",
        title="Arched Bridge Block",
        prompt=(
            "Model a symmetric arched bridge block resting on Z=0, centered in X and Y (Z-up): "
            "outer block dimensions X=4.0, Y=1.2, Z=1.6 (X in [-2.0, 2.0], Y in [-0.6, 0.6], Z in [0.0, 1.6]). "
            "Cut a semicircular arch tunnel through the full Y depth, centered at X=0.0, Z=0.0 with radius 1.1. "
            "Bevel the 4 top outer perimeter edges at Z=1.6 with width 0.1 and 2 segments. Single watertight manifold mesh."
        ),
        expected_bbox=((-2.0, -0.6, 0.0), (2.0, 0.6, 1.6)),
        ref_bpy="""
bm = bmesh.new()
bmesh.ops.create_cube(bm, size=1.0, matrix=Matrix.Translation((0, 0, 0.8)) @ Matrix.Diagonal((4.0, 1.2, 1.6, 1.0)))
top_edges = [e for e in bm.edges if e.verts[0].co.z > 1.5 and e.verts[1].co.z > 1.5]
bmesh.ops.bevel(bm, geom=top_edges, offset=0.1, offset_type='OFFSET', segments=2, profile=0.5, affect='EDGES')
blk = make_obj("bridge", bm)
arch = cyl("arch", 1.1, 2.0, 48, (0, 0, 0), rot=(math.pi/2, 0, 0))
bool_op(blk, arch, 'DIFFERENCE')
""",
    ),
    TaskSpec(
        id="chess_pawn",
        category="subdiv",
        title="Stylized Chess Pawn",
        prompt=(
            "Model a rotationally symmetric stylized chess pawn standing on Z=0, centered at X=0, Y=0 (Z-up), total height 2.40: "
            "1) circular base: radius 0.65, Z in [0.0, 0.20]; "
            "2) tapered skirt: frustum from radius 0.55 at Z=0.20 to radius 0.28 at Z=0.70; "
            "3) slender waist: frustum/cylinder from radius 0.28 at Z=0.70 to radius 0.20 at Z=1.40; "
            "4) collar ring: cylinder radius 0.36, Z in [1.40, 1.55]; "
            "5) neck: cylinder radius 0.20, Z in [1.55, 1.70]; "
            "6) spherical head: radius 0.38 centered at (0, 0, 2.02) (so top reaches Z=2.40). "
            "Single watertight manifold mesh."
        ),
        expected_bbox=((-0.65, -0.65, 0.0), (0.65, 0.65, 2.4)),
        ref_bpy="""
# Single continuous revolved profile including the spherical head arc (r=0.38 centered at z=2.02)
# Intersection of neck r=0.20 with sphere (r^2 + (z-2.02)^2 = 0.38^2) is at z = 2.02 - sqrt(0.38^2 - 0.20^2) = 1.6969
z_join = 2.02 - math.sqrt(0.38**2 - 0.20**2)
profile = [
    (0.65, 0.00),
    (0.65, 0.20),
    (0.55, 0.20),
    (0.28, 0.70),
    (0.20, 1.40),
    (0.36, 1.40),
    (0.36, 1.55),
    (0.20, 1.55),
    (0.20, z_join),
]
a0 = math.asin((z_join - 2.02) / 0.38)
for k in range(1, 14):
    a = a0 + (math.pi / 2 - a0) * (k / 14.0)
    profile.append((0.38 * math.cos(a), 2.02 + 0.38 * math.sin(a)))
bm = bmesh.new()
segs = 32
v_bot = bm.verts.new((0, 0, 0.0))
v_top = bm.verts.new((0, 0, 2.40))
rings = []
for r, z in profile:
    rings.append([bm.verts.new((r * math.cos(2*math.pi*j/segs), r * math.sin(2*math.pi*j/segs), z)) for j in range(segs)])
for j in range(segs):
    jn = (j + 1) % segs
    bm.faces.new((v_bot, rings[0][jn], rings[0][j]))
for i in range(len(rings) - 1):
    for j in range(segs):
        jn = (j + 1) % segs
        bm.faces.new((rings[i][j], rings[i][jn], rings[i+1][jn], rings[i+1][j]))
for j in range(segs):
    jn = (j + 1) % segs
    bm.faces.new((v_top, rings[-1][j], rings[-1][jn]))
bmesh.ops.recalc_face_normals(bm, faces=bm.faces[:])
base = make_obj("chess_pawn", bm)
""",
    ),
]

TASK_BY_ID: Dict[str, TaskSpec] = {t.id: t for t in TASKS}


def generate_reference_meshes() -> None:
    REF_DIR.mkdir(parents=True, exist_ok=True)
    for t in TASKS:
        out = REF_DIR / f"{t.id}.glb"
        script = f"OUT_PATH = {str(out)!r}\n" + HEADER_BPY + t.ref_bpy + EXPORT_FOOTER
        with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:
            f.write(script)
            sp = f.name
        try:
            r = subprocess.run(
                [BLENDER_BIN, "--background", "--factory-startup", "--python", sp],
                capture_output=True,
                text=True,
                timeout=30,
            )
            if r.returncode != 0 or not out.exists():
                raise RuntimeError(f"Failed ref {t.id}: {r.stderr[-600:]}")
            print(f"[REF OK] {t.id:20s} -> {out.stat().st_size:7d} bytes")
        finally:
            os.unlink(sp)


if __name__ == "__main__":
    generate_reference_meshes()