Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

blender: cover the bpy helper, the verdict and the shell scripts; fix four bugs

dee78b5df18c7ee
diff --git a/store/agents/blender/toolchain/harness_blender.py b/store/agents/blender/toolchain/harness_blender.py
index 892e1da9f..ad5ce66ef 100644
--- a/store/agents/blender/toolchain/harness_blender.py
+++ b/store/agents/blender/toolchain/harness_blender.py
@@ -215,7 +215,9 @@ def turntable(path: str | Path = "out/turntable.mp4", seconds: float = 4.0, fps:
bpy.context.view_layer.update()
cam.parent = pivot
cam.matrix_parent_inverse = pivot.matrix_world.inverted()
- frames = int(seconds * fps)
+ # At least one frame, rounded not truncated: int(0.29 * 100) is 28, and 0 frames divided by zero in
+ # the progress handler below.
+ frames = max(1, round(seconds * fps))
s.frame_start, s.frame_end = 1, frames
s.render.fps = fps
# Linear keys, set through the preference new keys are born with: Blender 5's layered actions
@@ -314,7 +316,11 @@ def _stamp() -> list[tuple]:
targets: list[tuple] = []
def put(idblock, value: dict) -> None:
- targets.append((idblock, idblock.get("harness")))
+ before = idblock.get("harness")
+ # A group or an array comes back as a live view of the property about to be replaced, and
+ # reads as whatever took its memory afterwards: keep a copy.
+ copy = getattr(before, "to_dict", None) or getattr(before, "to_list", None)
+ targets.append((idblock, copy() if copy else before))
idblock["harness"] = value
put(s, {
@@ -362,14 +368,20 @@ def export_glb(path: str | Path = "out/model.glb", cameras: bool = True, lights:
_activity(f"Exporting {out.name}")
gltf = out.suffix.lower() == ".gltf"
tmp = out.with_name(f".{out.stem}.partial{out.suffix}")
+ # Since Blender 4.2 the exporter offers GLTF_EMBEDDED (one self-contained .gltf, what the pane
+ # loads) only while its add-on preference allows it; allow it for this export and put it back.
+ prefs = bpy.context.preferences.addons["io_scene_gltf2"].preferences
+ embedded = prefs.allow_embedded_format
targets = _stamp()
try:
+ prefs.allow_embedded_format = True
bpy.ops.export_scene.gltf(
filepath=str(tmp), export_format="GLTF_EMBEDDED" if gltf else "GLB",
use_selection=False, use_renderable=True, export_apply=True, export_yup=True,
export_extras=True, export_cameras=cameras, export_lights=lights,
export_hierarchy_full_collections=collections, export_animations=True)
finally:
+ prefs.allow_embedded_format = embedded
_unstamp(targets)
os.replace(tmp, out)
rel = _rel(out)
diff --git a/store/agents/blender/toolchain/test_harness_blender.py b/store/agents/blender/toolchain/test_harness_blender.py
new file mode 100644
index 000000000..c7ad4ad30
--- /dev/null
+++ b/store/agents/blender/toolchain/test_harness_blender.py
@@ -0,0 +1,420 @@
+"""harness_blender on real bpy, at postage-stamp sizes. Needs the venv's bpy:
+
+ "$BLENDER_PYTHON" -m unittest toolchain/test_harness_blender.py
+
+Every test starts from fresh() in its own scratch workspace; renders are 32×18 and turntables a few
+frames, so the whole file runs in seconds.
+"""
+import atexit, contextlib, importlib, io, json, os, shutil, struct, sys, tempfile, unittest
+from pathlib import Path
+from unittest import mock
+
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+try:
+ import bpy
+ from mathutils import Vector
+except ImportError: # the verdict tests run without bpy; these cannot
+ bpy = None
+
+hb = None
+IMPORT_FEED = None
+IMPORT_HOOK = None
+SIZE = (32, 18)
+
+
+def setUpModule():
+ """Import the helper the way a scene script does: from a workspace, as scenes/build.py."""
+ global hb, IMPORT_FEED, IMPORT_HOOK
+ if bpy is None:
+ return
+ tmp = tempfile.TemporaryDirectory()
+ ws = Path(tmp.name).resolve()
+ (ws / ".harness").mkdir()
+ (ws / "scenes").mkdir()
+ cwd, hook = os.getcwd(), sys.excepthook
+ os.chdir(ws)
+ try:
+ with mock.patch.dict(os.environ, {"HARNESS_WORKSPACE": str(ws)}), mock.patch.object(sys, "argv", [str(ws / "scenes" / "build.py")]):
+ sys.modules.pop("harness_blender", None)
+ hb = importlib.import_module("harness_blender")
+ IMPORT_FEED = json.loads((ws / ".harness" / "build.json").read_text())
+ IMPORT_HOOK = sys.excepthook
+ finally:
+ os.chdir(cwd)
+ sys.excepthook = hook
+ atexit.unregister(hb._finish)
+ tmp.cleanup()
+
+
+def glb_json(path: Path) -> dict:
+ data = path.read_bytes()
+ length = struct.unpack("<I", data[12:16])[0]
+ return json.loads(data[20:20 + length])
+
+
[email protected](bpy is None, "bpy is not importable here; run with the venv's python")
+class Base(unittest.TestCase):
+ def setUp(self):
+ tmp = tempfile.TemporaryDirectory()
+ self.addCleanup(tmp.cleanup)
+ self.ws = Path(tmp.name).resolve()
+ (self.ws / ".harness").mkdir()
+ cwd = os.getcwd()
+ os.chdir(self.ws)
+ self.addCleanup(os.chdir, cwd)
+ patcher = mock.patch.object(hb, "_WS", self.ws)
+ patcher.start()
+ self.addCleanup(patcher.stop)
+ hb._feed.clear()
+ hb._exported.clear()
+ hb._feed_written = 0.0
+ self.addCleanup(hb._feed.clear)
+ self.addCleanup(hb._exported.clear)
+ hb.fresh()
+
+ def feed(self) -> dict:
+ return json.loads((self.ws / ".harness" / "build.json").read_text())
+
+ def cube(self, name="Cube", size=10.0, location=(0, 0, 0)):
+ bpy.ops.mesh.primitive_cube_add(size=size, location=location)
+ obj = bpy.context.active_object
+ obj.name = name
+ return obj
+
+
+class Feed(Base):
+ def test_importing_announces_the_script(self):
+ self.assertEqual({k: IMPORT_FEED[k] for k in ("state", "step", "progress", "script", "pid", "exported", "error")},
+ {"state": "building", "step": "Running", "progress": None, "script": "scenes/build.py", "pid": os.getpid(), "exported": None, "error": None})
+ self.assertIs(IMPORT_HOOK, hb._excepthook)
+
+ def test_fresh_says_modelling(self):
+ self.assertEqual(self.feed()["step"], "Modelling")
+
+ def test_no_harness_folder_means_no_feed(self):
+ hb._feed.clear()
+ shutil.rmtree(self.ws / ".harness")
+ hb._activity("Exporting")
+ self.assertEqual(hb._feed, {})
+ self.assertFalse((self.ws / ".harness").exists())
+
+ def test_only_a_python_script_is_named(self):
+ for argv in (["python"], [""], []):
+ with self.subTest(argv=argv), mock.patch.object(sys, "argv", argv):
+ hb._feed.clear()
+ hb._activity("x")
+ self.assertIsNone(self.feed()["script"])
+
+ def test_progress_is_clamped_and_extras_ride_along(self):
+ hb._activity("a", progress=1.7, exported="out/a.glb")
+ self.assertEqual((self.feed()["progress"], self.feed()["exported"]), (1.0, "out/a.glb"))
+ hb._activity("b", progress=-0.3)
+ self.assertEqual(self.feed()["progress"], 0.0)
+ hb._activity(progress=0.12345)
+ self.assertEqual((self.feed()["step"], self.feed()["progress"]), ("b", 0.123))
+
+ def test_per_frame_progress_is_throttled(self):
+ hb._activity("first")
+ hb._activity("second", progress=0.5, force=False)
+ self.assertEqual(self.feed()["step"], "first")
+ self.assertEqual(hb._feed["step"], "second")
+ hb._feed_written = 0.0
+ hb._activity("third", force=False)
+ self.assertEqual(self.feed()["step"], "third")
+
+ def test_a_feed_that_cannot_be_written_never_fails_the_build(self):
+ (self.ws / ".harness" / "build.json").unlink()
+ (self.ws / ".harness" / "build.json").mkdir()
+ hb._activity("Exporting")
+ self.assertTrue((self.ws / ".harness" / "build.json").is_dir())
+
+ def test_an_uncaught_error_marks_the_build_failed(self):
+ prior = mock.Mock()
+ with mock.patch.object(hb, "_prior_excepthook", prior):
+ hb._activity("Exporting model.glb")
+ try:
+ raise ValueError("boom " + "x" * 300)
+ except ValueError as error:
+ hb._excepthook(ValueError, error, error.__traceback__)
+ prior.assert_called_once()
+ feed = self.feed()
+ self.assertEqual((feed["state"], feed["step"]), ("failed", "Exporting model.glb"))
+ self.assertEqual(len(feed["error"]), 200)
+ self.assertTrue(feed["error"].startswith("ValueError: boom xxx") and feed["error"].endswith("…"))
+ hb._feed.clear()
+ hb._excepthook(RuntimeError, RuntimeError("short"), None)
+ self.assertEqual({k: self.feed()[k] for k in ("state", "step", "error")}, {"state": "failed", "step": "Failed", "error": "RuntimeError: short"})
+
+ def test_the_end_of_a_script_is_done_unless_it_failed(self):
+ hb._feed.clear()
+ (self.ws / ".harness" / "build.json").unlink()
+ hb._finish()
+ self.assertFalse((self.ws / ".harness" / "build.json").exists(), "nothing ran, nothing to finish")
+ hb._activity("Report written")
+ hb._finish()
+ self.assertEqual((self.feed()["state"], self.feed()["step"]), ("done", "Done"))
+ hb._activity(state="failed")
+ hb._finish()
+ self.assertEqual(self.feed()["state"], "failed")
+
+ def test_a_path_outside_the_workspace_stays_absolute(self):
+ self.assertEqual(hb._rel(self.ws / "out" / "a.glb"), "out/a.glb")
+ self.assertEqual(hb._rel(Path("/Users/example/elsewhere.glb")), "/Users/example/elsewhere.glb")
+
+
+class Scene(Base):
+ def test_fresh_is_an_empty_scene_in_millimetres(self):
+ s = bpy.context.scene
+ self.assertEqual(len(s.objects), 0)
+ self.assertEqual((s.unit_settings.system, s.unit_settings.length_unit, s.render.engine), ("METRIC", "MILLIMETERS", "BLENDER_WORKBENCH"))
+ self.assertAlmostEqual(hb._mm(), 1.0)
+ self.assertIsNotNone(s.world)
+
+ def test_bounds_are_what_renders(self):
+ self.assertEqual(hb.bounds(), (Vector((0, 0, 0)), Vector((0, 0, 0))), "an empty scene is a point")
+ self.cube(location=(10, 0, 0))
+ cutter = self.cube("Cutter", size=100)
+ cutter.hide_render = True
+ self.assertEqual([o.name for o in hb.meshes()], ["Cube"])
+ lo, hi = hb.bounds()
+ self.assertEqual((tuple(round(v, 4) for v in lo), tuple(round(v, 4) for v in hi)), ((5, -5, -5), (15, 5, 5)))
+
+ def test_frame_all_adds_one_camera_and_one_key_light_looking_at_the_model(self):
+ self.cube(location=(10, 20, 5))
+ cam = hb.frame_all()
+ s = bpy.context.scene
+ self.assertIs(s.camera, cam)
+ bpy.context.view_layer.update()
+ forward = cam.matrix_world.to_quaternion() @ Vector((0, 0, -1))
+ towards = (Vector((10, 20, 5)) - cam.location).normalized()
+ self.assertGreater(forward.dot(towards), 0.9999)
+ distance = (cam.location - Vector((10, 20, 5))).length
+ self.assertLess(cam.data.clip_start, distance)
+ self.assertLess(distance, cam.data.clip_end)
+ self.assertIs(hb.frame_all(azimuth=90), cam, "a second call moves the same camera")
+ self.assertEqual([o.name for o in s.objects if o.type == "LIGHT"], ["Key"])
+
+ def test_a_scene_with_its_own_light_gets_no_key(self):
+ self.cube()
+ light = bpy.data.objects.new("Lamp", bpy.data.lights.new("Lamp", "POINT"))
+ bpy.context.scene.collection.objects.link(light)
+ hb.frame_all()
+ self.assertEqual([o.name for o in bpy.context.scene.objects if o.type == "LIGHT"], ["Lamp"])
+
+
+class Renders(Base):
+ def test_a_still_frames_the_scene_when_there_is_no_camera(self):
+ self.cube()
+ out = hb.render("out/preview.png", size=SIZE)
+ self.assertEqual(out, self.ws / "out" / "preview.png")
+ self.assertTrue(out.is_file())
+ self.assertIsNotNone(bpy.context.scene.camera)
+ self.assertEqual(self.feed()["step"], "Rendering preview.png (Workbench)")
+
+ def test_a_cycles_still_on_the_cpu(self):
+ self.cube()
+ hb.render("out/beauty.png", size=SIZE, engine="CYCLES", samples=1)
+ s = bpy.context.scene
+ self.assertEqual((s.render.engine, s.cycles.samples, s.cycles.device), ("CYCLES", 1, "CPU"))
+ self.assertEqual(self.feed()["step"], "Rendering beauty.png (Cycles)")
+ self.assertTrue((self.ws / "out" / "beauty.png").is_file())
+
+ def test_another_named_engine_keeps_cycles_settings_alone(self):
+ self.cube()
+ samples = bpy.context.scene.cycles.samples
+ hb.render("out/p.png", size=SIZE, engine="BLENDER_WORKBENCH", samples=3)
+ self.assertEqual(bpy.context.scene.cycles.samples, samples)
+
+ def scene_as_found(self):
+ self.cube()
+ cam = hb.frame_all()
+ s = bpy.context.scene
+ s.frame_start, s.frame_end, s.render.fps = 5, 90, 24
+ s.frame_set(7)
+ bpy.context.preferences.edit.keyframe_new_interpolation_type = "BEZIER"
+ return cam, cam.matrix_world.copy()
+
+ def assert_scene_put_back(self, cam, matrix):
+ s = bpy.context.scene
+ self.assertIsNone(cam.parent)
+ for a, b in zip(cam.matrix_world, matrix):
+ for x, y in zip(a, b):
+ self.assertAlmostEqual(x, y, places=4)
+ self.assertNotIn("Pivot", bpy.data.objects)
+ self.assertEqual((s.frame_start, s.frame_end, s.render.fps, s.frame_current), (5, 90, 24, 7))
+ self.assertEqual(bpy.context.preferences.edit.keyframe_new_interpolation_type, "BEZIER")
+
+ @unittest.skipIf(shutil.which("ffmpeg") is None, "no ffmpeg on PATH")
+ def test_a_turntable_is_an_mp4_and_the_scene_is_left_as_found(self):
+ cam, matrix = self.scene_as_found()
+ out = hb.turntable("out/turntable.mp4", seconds=0.3, fps=10, size=SIZE)
+ self.assertTrue(out.is_file() and out.stat().st_size > 0)
+ self.assertFalse((self.ws / "out" / "turntable-frames").exists(), "the frames are joined, then removed")
+ self.assert_scene_put_back(cam, matrix)
+ self.assertEqual(len(bpy.data.actions), 0, "the orbit's action goes with its pivot")
+ self.assertEqual(self.feed()["step"], "Encoding turntable")
+
+ def test_without_ffmpeg_the_frames_stay_and_say_so(self):
+ cam, matrix = self.scene_as_found()
+ frames = self.ws / "out" / "spin-frames"
+ frames.mkdir(parents=True)
+ (frames / "frame_0099.png").write_bytes(b"stale")
+ printed = io.StringIO()
+ with mock.patch.object(hb.shutil, "which", return_value=None), contextlib.redirect_stdout(printed):
+ out = hb.turntable("out/spin.mp4", seconds=0.2, fps=10, size=SIZE)
+ self.assertFalse(out.exists())
+ self.assertEqual(sorted(p.name for p in frames.iterdir()), ["frame_0001.png", "frame_0002.png"])
+ self.assertIn("warn no ffmpeg on PATH", printed.getvalue())
+ self.assert_scene_put_back(cam, matrix)
+
+ def test_a_turntable_shorter_than_a_frame_still_renders_one(self):
+ self.cube()
+ with mock.patch.object(hb.shutil, "which", return_value=None), mock.patch.object(hb, "_activity", wraps=hb._activity) as activity, contextlib.redirect_stdout(io.StringIO()):
+ hb.turntable("out/blink.mp4", seconds=0.04, fps=10, size=SIZE)
+ self.assertIn(mock.call("Rendering turntable 1/1", progress=1.0, force=False), activity.call_args_list,
+ "the progress handler ran (0 frames divided by zero in it)")
+ self.assertEqual([p.name for p in (self.ws / "out" / "blink-frames").iterdir()], ["frame_0001.png"])
+
+ def test_an_action_still_in_use_is_kept(self):
+ self.cube()
+
+ def keep(*_):
+ for action in bpy.data.actions:
+ action.use_fake_user = True
+
+ bpy.app.handlers.render_pre.append(keep)
+ try:
+ with mock.patch.object(hb.shutil, "which", return_value=None), contextlib.redirect_stdout(io.StringIO()):
+ hb.turntable("out/t.mp4", seconds=0.1, fps=10, size=SIZE)
+ finally:
+ bpy.app.handlers.render_pre.remove(keep)
+ self.assertNotIn("Pivot", bpy.data.objects)
+ self.assertEqual(len(bpy.data.actions), 1)
+
+
+class Exports(Base):
+ def build(self):
+ body = self.cube("Body")
+ clay = bpy.data.materials.new("Clay")
+ body.data.materials.append(clay)
+ body.data.materials.append(None)
+ bpy.data.materials.new("Spare") # no users: not in the extras
+ body.modifiers.new("Bevel", "BEVEL")
+ lid = self.cube("Lid", size=4, location=(0, 0, 8))
+ lid.parent = body
+ off = lid.modifiers.new("Hidden", "BEVEL")
+ off.show_render = False
+ parts = bpy.data.collections.new("Parts")
+ bpy.context.scene.collection.children.link(parts)
+ handle = self.cube("Handle", size=2, location=(8, 0, 0))
+ bpy.context.scene.collection.objects.unlink(handle)
+ parts.objects.link(handle)
+ bpy.data.collections.new("Loose") # not in the scene
+ marker = bpy.data.objects.new("Marker", None)
+ bpy.context.scene.collection.objects.link(marker)
+ hb.frame_all()
+ body["harness"] = {"mine": 1}
+ lid["harness"] = [1, 2]
+ handle["harness"] = "theirs"
+ return body
+
+ def test_a_glb_carries_the_facts_as_extras_and_the_scene_is_left_clean(self):
+ body = self.build()
+ out = hb.export_glb("out/model.glb")
+ self.assertEqual(sorted(p.name for p in out.parent.iterdir()), ["model.glb"], "no partial file left behind")
+ doc = glb_json(out)
+ scene = doc["scenes"][0]["extras"]["harness"]
+ self.assertEqual((scene["units"], scene["metres_per_unit"], scene["camera"]), ("millimeters", 0.001, "Camera"))
+ self.assertEqual(list(scene["materials"]), ["Clay"])
+ nodes = {n["name"]: (n.get("extras") or {}).get("harness") for n in doc["nodes"]}
+ self.assertEqual(nodes["Body"]["modifiers"], [{"name": "Bevel", "type": "BEVEL"}])
+ self.assertEqual((nodes["Body"]["materials"], nodes["Body"]["faces"]), (["Clay"], 26), "faces after the bevel: the evaluated mesh")
+ self.assertEqual(nodes["Body"]["dimensions_mm"], [10.0, 10.0, 10.0])
+ self.assertEqual(nodes["Lid"]["parent"], "Body")
+ self.assertNotIn("modifiers", nodes["Lid"])
+ self.assertEqual(nodes["Handle"]["collections"], ["Parts"])
+ self.assertEqual(nodes["Parts"], {"type": "COLLECTION"})
+ self.assertEqual(nodes["Camera"]["camera"]["scene_camera"], True)
+ self.assertEqual(nodes["Key"]["light"]["type"], "SUN")
+ self.assertEqual(nodes["Marker"]["type"], "EMPTY")
+ self.assertFalse({"light", "camera", "faces"} & set(nodes["Marker"]))
+ self.assertEqual(body["harness"].to_dict(), {"mine": 1}, "a property that was there is put back, not what reused its memory")
+ self.assertEqual((bpy.data.objects["Lid"]["harness"].to_list(), bpy.data.objects["Handle"]["harness"]), ([1, 2], "theirs"))
+ self.assertNotIn("harness", bpy.context.scene)
+ self.assertNotIn("harness", bpy.data.objects["Camera"])
+ self.assertEqual((self.feed()["step"], self.feed()["exported"]), ("Exported model.glb", "out/model.glb"))
+ hb.export_glb("out/model.glb")
+ self.assertEqual(hb._exported, ["out/model.glb"], "an export named twice is listed once")
+
+ def test_a_gltf_is_one_embedded_file_and_the_preference_is_put_back(self):
+ self.cube()
+ prefs = bpy.context.preferences.addons["io_scene_gltf2"].preferences
+ self.assertFalse(prefs.allow_embedded_format)
+ out = hb.export_glb("out/model.gltf", cameras=False, lights=False)
+ self.assertEqual(sorted(p.name for p in out.parent.iterdir()), ["model.gltf"])
+ doc = json.loads(out.read_text())
+ self.assertTrue(doc["buffers"][0]["uri"].startswith("data:"))
+ self.assertFalse(prefs.allow_embedded_format)
+
+ def test_a_scene_without_units_says_unit(self):
+ self.cube()
+ bpy.context.scene.unit_settings.system = "NONE"
+ doc = glb_json(hb.export_glb("out/model.glb"))
+ self.assertEqual(doc["scenes"][0]["extras"]["harness"]["units"], "unit")
+
+ def test_an_export_outside_the_workspace_is_named_absolutely(self):
+ self.cube()
+ with tempfile.TemporaryDirectory() as other:
+ out = hb.export_glb(Path(other) / "shared.glb")
+ self.assertEqual(hb._exported, [str(out)])
+
+ def test_unstamp_tolerates_a_missing_property_and_a_removed_object(self):
+ kept = self.cube("Kept")
+ gone = self.cube("Gone")
+ bpy.data.objects.remove(gone, do_unlink=True)
+ hb._unstamp([(kept, None), (gone, None)])
+ self.assertNotIn("harness", kept)
+
+ def test_stl(self):
+ self.cube()
+ out = hb.export_stl("out/model.stl")
+ self.assertGreater(out.stat().st_size, 84)
+ self.assertEqual(self.feed()["step"], "Exporting model.stl")
+
+
+class Report(Base):
+ def test_what_was_built_is_reported(self):
+ a = self.cube("A")
+ a.data.materials.append(bpy.data.materials.new("Clay"))
+ b = self.cube("B", location=(20, 0, 0))
+ parts = bpy.data.collections.new("Parts")
+ bpy.context.scene.collection.children.link(parts)
+ parts.objects.link(b)
+ self.cube("Cutter", size=100).hide_render = True
+ hb.frame_all()
+ hb.export_glb("out/lamp.glb")
+ hb.render("out/preview.png", size=SIZE)
+ printed = io.StringIO()
+ with contextlib.redirect_stdout(printed):
+ data = hb.report()
+ self.assertEqual(json.loads((self.ws / "out" / "report.json").read_text()), data)
+ self.assertEqual((data["objects"], data["vertices"], data["faces"]), (["A", "B"], 16, 12))
+ self.assertEqual(data["size_mm"], [30.0, 10.0, 10.0])
+ self.assertEqual((data["materials"], data["collections"], data["cameras"], data["lights"]), (["Clay"], ["Parts"], ["Camera"], ["Key"]))
+ self.assertEqual(data["files"], {"preview": "out/preview.png", "glb": "out/lamp.glb"})
+ self.assertEqual(data["blender"], bpy.app.version_string)
+ self.assertTrue(printed.getvalue().startswith("2 objects · 16 verts · 12 faces · [30.0, 10.0, 10.0] mm"))
+ self.assertEqual(self.feed()["step"], "Report written")
+
+ def test_one_object_nothing_exported(self):
+ self.cube()
+ printed = io.StringIO()
+ with contextlib.redirect_stdout(printed):
+ data = hb.report("elsewhere/report.json")
+ self.assertTrue((self.ws / "elsewhere" / "report.json").is_file())
+ self.assertEqual(data["files"], {}, "out/model.glb is only named when it exists")
+ self.assertTrue(printed.getvalue().startswith("1 object · 8 verts"))
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/store/agents/blender/toolchain/test_scripts.py b/store/agents/blender/toolchain/test_scripts.py
new file mode 100644
index 000000000..e2e1f688b
--- /dev/null
+++ b/store/agents/blender/toolchain/test_scripts.py
@@ -0,0 +1,184 @@
+"""setup.sh, doctor.sh and init-workspace.sh, run for real against a scratch install whose PATH holds
+only stub commands (and the few coreutils the scripts use), so every ok / warn / miss line is reached
+without a network, a Python 3.11 or a 300 MB bpy wheel:
+
+ python3 -m unittest toolchain/test_scripts.py
+"""
+import shutil, subprocess, tempfile, unittest
+from pathlib import Path
+
+PACKAGE = Path(__file__).resolve().parent.parent
+BASH = "/bin/bash"
+COREUTILS = ("dirname", "cat", "mkdir", "cp", "rm", "chmod", "grep")
+VERSION = (PACKAGE / "BPY_VERSION").read_text().strip()
+
+# The venv's python: answers the bpy import (IMPORT_BPY), its version string, pip (PIP_EXIT; the pip
+# self-upgrade always fails, which setup tolerates) and the heredoc render check (RENDER_EXIT).
+VENV_PYTHON = """case "$1" in
+ -c) case "$2" in *version_string*) echo "%s" ;; *) exit "${IMPORT_BPY:-0}" ;; esac ;;
+ -m) case "$*" in *--upgrade*) exit 1 ;; *) exit "${PIP_EXIT:-0}" ;; esac ;;
+ -) cat >/dev/null; [ "${RENDER_EXIT:-0}" = 0 ] || exit "$RENDER_EXIT"; echo "ok headless rendering works" ;;
+esac""" % VERSION
+
+
+class Sandbox:
+ """An install dir with the package's scripts, a bin/ of stubs as the whole PATH, and a log of
+ every stub call."""
+
+ def __init__(self, test: unittest.TestCase):
+ tmp = tempfile.TemporaryDirectory()
+ test.addCleanup(tmp.cleanup)
+ self.root = Path(tmp.name)
+ self.install = self.root / "install"
+ (self.install / "toolchain").mkdir(parents=True)
+ for script in PACKAGE.glob("toolchain/*.sh"):
+ shutil.copy(script, self.install / "toolchain" / script.name)
+ shutil.copy(PACKAGE / "BPY_VERSION", self.install / "BPY_VERSION")
+ self.bin = self.root / "bin"
+ self.bin.mkdir()
+ self.calls = self.root / "calls.log"
+ self.calls.touch()
+ for name in COREUTILS:
+ real = next(p for p in (Path("/bin") / name, Path("/usr/bin") / name) if p.exists())
+ (self.bin / name).symlink_to(real)
+
+ def stub(self, name: str, body: str = "", where: Path | None = None) -> Path:
+ path = (where or self.bin) / name
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(f'#!/bin/bash\necho "{name} $*" >> "$CALLS"\n{body}\n')
+ path.chmod(0o755)
+ return path
+
+ def interpreter(self, name: str, version: str) -> Path:
+ """A python on PATH: the 3.11 probe answers for `version`; `-m venv` makes a venv of stubs."""
+ venv_python = self.stub("python", VENV_PYTHON, where=self.root / "templates")
+ return self.stub(name, f"""case "$1" in
+ -c) [ "{version[:4]}" = 3.11 ] ;;
+ --version) echo "Python {version}" ;;
+ -m) mkdir -p "$3/bin" && cp "{venv_python}" "$3/bin/python" ;;
+esac""")
+
+ def venv(self) -> Path:
+ return self.stub("python", VENV_PYTHON, where=self.install / ".venv" / "bin")
+
+ def run(self, script: str, cwd: Path | None = None, **env: str) -> subprocess.CompletedProcess:
+ return subprocess.run([BASH, str(self.install / "toolchain" / script)], cwd=cwd or self.install,
+ env={"PATH": str(self.bin), "CALLS": str(self.calls), **env},
+ capture_output=True, text=True, timeout=60)
+
+ def logged(self) -> list[str]:
+ return self.calls.read_text().splitlines()
+
+
+class Doctor(unittest.TestCase):
+ def test_ready_with_ffmpeg(self):
+ box = Sandbox(self)
+ box.venv()
+ box.stub("ffmpeg")
+ r = box.run("doctor.sh")
+ self.assertEqual((r.returncode, r.stdout.splitlines()), (0, [f"ok blender {VERSION}", "ok ffmpeg (turntables)"]), r.stderr)
+
+ def test_no_ffmpeg_is_only_a_warning(self):
+ box = Sandbox(self)
+ box.venv()
+ r = box.run("doctor.sh")
+ self.assertEqual(r.returncode, 0)
+ self.assertEqual(r.stdout.splitlines()[-1], "warn ffmpeg not on PATH — turntables stay as frames (brew install ffmpeg)")
+
+ def test_no_venv_is_a_miss_and_stops(self):
+ box = Sandbox(self)
+ box.stub("ffmpeg")
+ r = box.run("doctor.sh")
+ self.assertEqual((r.returncode, r.stdout.splitlines()), (1, ["miss .venv with bpy — run toolchain/setup.sh"]))
+
+ def test_a_venv_without_bpy_is_a_miss(self):
+ box = Sandbox(self)
+ box.venv()
+ r = box.run("doctor.sh", IMPORT_BPY="1")
+ self.assertEqual((r.returncode, r.stdout.splitlines()), (1, ["miss .venv with bpy — run toolchain/setup.sh"]))
+
+
+class Setup(unittest.TestCase):
+ def test_installs_bpy_into_a_venv_and_checks_rendering(self):
+ box = Sandbox(self)
+ box.interpreter("python3.11", "3.11.9")
+ r = box.run("setup.sh")
+ self.assertEqual(r.returncode, 0, r.stderr)
+ self.assertEqual(r.stdout.splitlines(), [
+ "ok Python 3.11.9",
+ f" installing bpy {VERSION} (Blender as a module, ~300 MB, a few minutes the first time)",
+ f"ok blender {VERSION}",
+ " render check (Workbench, headless)",
+ "ok headless rendering works",
+ ])
+ log = box.logged()
+ self.assertIn("python3.11 -m venv .venv", log)
+ self.assertIn(f"python -m pip install --quiet bpy=={VERSION} numpy", log)
+ self.assertEqual(log[-1], "python -")
+
+ def test_python3_serves_when_it_is_3_11(self):
+ box = Sandbox(self)
+ box.interpreter("python3", "3.11.2")
+ r = box.run("setup.sh")
+ self.assertEqual((r.returncode, r.stdout.splitlines()[0]), (0, "ok Python 3.11.2"))
+ self.assertIn("python3 -m venv .venv", box.logged())
+
+ def test_a_python3_11_that_is_another_version_is_passed_over(self):
+ box = Sandbox(self)
+ box.interpreter("python3.11", "3.12.0")
+ box.interpreter("python3", "3.11.4")
+ r = box.run("setup.sh")
+ self.assertEqual((r.returncode, r.stdout.splitlines()[0]), (0, "ok Python 3.11.4"))
+
+ def test_no_python_3_11_is_a_miss(self):
+ box = Sandbox(self)
+ box.interpreter("python3", "3.12.1")
+ r = box.run("setup.sh")
+ self.assertEqual(r.returncode, 1)
+ self.assertEqual(r.stdout.strip(), f"miss python 3.11 exactly — the bpy {VERSION} wheel is built for it (brew install [email protected])")
+
+ def test_an_existing_venv_is_reused(self):
+ box = Sandbox(self)
+ box.interpreter("python3.11", "3.11.9")
+ box.venv()
+ r = box.run("setup.sh")
+ self.assertEqual(r.returncode, 0, r.stderr)
+ self.assertNotIn("python3.11 -m venv .venv", box.logged())
+
+ def test_a_failed_install_stops_setup(self):
+ box = Sandbox(self)
+ box.interpreter("python3.11", "3.11.9")
+ r = box.run("setup.sh", PIP_EXIT="1")
+ self.assertNotEqual(r.returncode, 0)
+ self.assertNotIn(f"ok blender {VERSION}", r.stdout)
+
+ def test_a_render_check_that_fails_fails_setup(self):
+ box = Sandbox(self)
+ box.interpreter("python3.11", "3.11.9")
+ r = box.run("setup.sh", RENDER_EXIT="3")
+ self.assertEqual(r.returncode, 3)
+ self.assertEqual(r.stdout.splitlines()[-1], " render check (Workbench, headless)")
+
+
+class InitWorkspace(unittest.TestCase):
+ def test_builds_the_starter_and_seeds_the_verdict(self):
+ box = Sandbox(self)
+ ws = box.root / "ws"
+ ws.mkdir()
+ # Both fail: a starter that cannot build (no bpy yet) must not stop the workspace being made.
+ box.stub("python", 'echo "PYTHONPATH=${PYTHONPATH:-}" >> "$CALLS"; exit 1', where=box.install / ".venv" / "bin")
+ r = box.run("init-workspace.sh", cwd=ws, HARNESS_DSH_DIR=str(box.install))
+ self.assertEqual(r.returncode, 0, r.stderr)
+ self.assertTrue((ws / ".harness").is_dir() and (ws / "out").is_dir())
+ self.assertEqual(box.logged(), ["python scenes/hello.py", f"PYTHONPATH={box.install}/toolchain",
+ f"python {box.install}/toolchain/verdict.py", "PYTHONPATH="])
+
+ def test_needs_the_install_dir(self):
+ box = Sandbox(self)
+ r = box.run("init-workspace.sh", cwd=box.root)
+ self.assertNotEqual(r.returncode, 0)
+ self.assertIn("HARNESS_DSH_DIR", r.stderr)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/store/agents/blender/toolchain/test_verdict.py b/store/agents/blender/toolchain/test_verdict.py
index 02aad8e9c..58cf3dd5a 100644
--- a/store/agents/blender/toolchain/test_verdict.py
+++ b/store/agents/blender/toolchain/test_verdict.py
@@ -1,8 +1,14 @@
-import sys, unittest
+import contextlib, io, json, os, runpy, sys, tempfile, time, unittest
from pathlib import Path
+from unittest import mock
sys.path.insert(0, str(Path(__file__).parent))
+import verdict
from verdict import judge
+HERE = Path(__file__).resolve().parent
+BIG = b"x" * 600 # over the 500 bytes that make a file count
+
+
class Judge(unittest.TestCase):
def test_modelled_exported_rendered(self):
v = judge(True, {"objects": ["Mug", "Handle"], "faces": 12400, "size_mm": [90, 90, 100]}, True, True, True)
@@ -11,6 +17,7 @@ class Judge(unittest.TestCase):
def test_empty_scene_fails_model(self):
v = judge(True, {"objects": [], "faces": 0}, False, False, False)
self.assertFalse(v["ready"]); self.assertEqual(v["phases"][0]["state"], "failed")
+ self.assertEqual(v["findings"], [{"severity": "error", "kind": "model", "message": "the scene has no geometry"}])
def test_preview_only(self):
v = judge(True, {"objects": ["a"], "faces": 12, "size_mm": [1, 1, 1]}, False, True, False)
self.assertTrue(v["ready"]); self.assertIsNone(v["artifact"]); self.assertEqual(v["phases"][1]["state"], "active")
@@ -21,19 +28,101 @@ class Judge(unittest.TestCase):
def test_modelled_without_export_warns(self):
v = judge(True, {"objects": ["a"], "faces": 12, "size_mm": [1, 1, 1]}, False, True, True)
self.assertIsNone(v["artifact"]); self.assertEqual([f["kind"] for f in v["findings"]], ["export"])
+ def test_a_heavy_mesh_warns_and_a_report_without_size_says_none(self):
+ v = judge(True, {"objects": ["a"], "faces": 2_500_000}, True, False, True)
+ self.assertTrue(v["ready"])
+ self.assertEqual([(f["severity"], f["kind"]) for f in v["findings"]], [("warning", "model")])
+ self.assertEqual(v["summary"], "1 object · 2,500,000 faces · glb")
+ def test_nothing_yet(self):
+ v = judge(False, None, False, False, False)
+ self.assertEqual([p["state"] for p in v["phases"]], ["active", "pending", "pending"])
+ self.assertEqual((v["summary"], v["findings"], v["ready"]), ("no model yet", [], False))
+ def test_a_script_that_has_not_run(self):
+ v = judge(True, None, False, False, False)
+ self.assertEqual(v["phases"][0]["state"], "active")
+
class Artifact(unittest.TestCase):
def test_report_then_model_then_newest(self):
- import tempfile, os, time, verdict
- with tempfile.TemporaryDirectory() as d:
+ with tempfile.TemporaryDirectory() as d, mock.patch.object(verdict, "WS", Path(d)):
ws = Path(d); (ws / "out").mkdir()
- verdict.WS = ws
ok = lambda p: bool(p) and (ws / p).is_file() and (ws / p).stat().st_size > 500
self.assertIsNone(verdict.glb_path(None, ok))
- (ws / "out/a.glb").write_bytes(b"x" * 600); time.sleep(0.02); (ws / "out/b.glb").write_bytes(b"x" * 600)
+ (ws / "out/a.glb").write_bytes(BIG); time.sleep(0.02); (ws / "out/b.glb").write_bytes(BIG)
self.assertEqual(verdict.glb_path(None, ok), "out/b.glb")
- (ws / "out/model.glb").write_bytes(b"x" * 600)
+ (ws / "out/model.glb").write_bytes(BIG)
self.assertEqual(verdict.glb_path(None, ok), "out/model.glb")
self.assertEqual(verdict.glb_path({"files": {"glb": "out/a.glb"}}, ok), "out/a.glb")
+ self.assertEqual(verdict.glb_path({"files": {"glb": "out/gone.glb"}}, ok), "out/model.glb", "a named export that is not there")
+
+ def test_partial_tiny_and_other_files_are_never_the_export(self):
+ with tempfile.TemporaryDirectory() as d, mock.patch.object(verdict, "WS", Path(d)):
+ ws = Path(d); (ws / "out" / "parts").mkdir(parents=True)
+ ok = lambda p: bool(p) and (ws / p).is_file() and (ws / p).stat().st_size > 500
+ (ws / "out/parts/lid.gltf").write_bytes(BIG); time.sleep(0.02)
+ (ws / "out/.lid.partial.glb").write_bytes(BIG) # an export being written
+ (ws / "out/stub.glb").write_bytes(b"x") # not a model yet
+ (ws / "out/turntable.mp4").write_bytes(BIG)
+ self.assertEqual(verdict.glb_path({"files": {"glb": 7}}, ok), "out/parts/lid.gltf")
+ with tempfile.TemporaryDirectory() as d, mock.patch.object(verdict, "WS", Path(d)):
+ self.assertIsNone(verdict.glb_path(None, lambda p: p != "out/model.glb"), "no out/ folder to search")
+
+
+class Main(unittest.TestCase):
+ """verdict.py on a workspace: what it reads, what it writes, what it prints and returns."""
+
+ def run_in(self, files: dict) -> tuple[int, str, dict]:
+ with tempfile.TemporaryDirectory() as d, mock.patch.object(verdict, "WS", Path(d)):
+ ws = Path(d)
+ for rel, body in files.items():
+ path = ws / rel
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_bytes(body if isinstance(body, bytes) else (body if isinstance(body, str) else json.dumps(body)).encode())
+ printed = io.StringIO()
+ with contextlib.redirect_stdout(printed):
+ code = verdict.main(["verdict.py"])
+ return code, printed.getvalue(), json.loads((ws / ".harness" / "verdict.json").read_text())
+
+ def test_a_modelled_exported_rendered_workspace(self):
+ code, printed, v = self.run_in({
+ "scenes/lamp.py": "", "out/report.json": {"objects": ["Lamp"], "faces": 800, "size_mm": [120, 120, 300], "files": {"glb": "out/lamp.glb"}},
+ "out/lamp.glb": BIG, "out/preview.png": BIG, "out/turntable.mp4": BIG})
+ self.assertEqual(code, 0)
+ self.assertEqual((v["artifact"], v["summary"]), ("out/lamp.glb", "1 object · 800 faces · 120×120×300 mm · glb"))
+ self.assertEqual(printed, "ready · 1 object · 800 faces · 120×120×300 mm · glb\n")
+
+ def test_a_render_that_is_too_small_is_not_a_render(self):
+ code, printed, v = self.run_in({"scenes/a.py": "", "out/report.json": {"objects": ["a"], "faces": 6}, "out/preview.png": b"x"})
+ self.assertEqual(code, 1)
+ self.assertEqual([p["state"] for p in v["phases"]], ["done", "active", "active"])
+ self.assertIn(" warning no glTF export", printed)
+
+ def test_a_scene_without_geometry_prints_its_error(self):
+ code, printed, v = self.run_in({"scenes/a.py": "", "out/report.json": {"objects": [], "faces": 0}})
+ self.assertEqual(code, 1)
+ self.assertEqual(printed.splitlines(), ["not ready · no model yet", " error the scene has no geometry"])
+
+ def test_a_half_written_or_foreign_report_is_no_report(self):
+ for body in ("{\"objects\": [", "[1, 2]", "\"done\"", "null"):
+ with self.subTest(report=body):
+ code, _, v = self.run_in({"scenes/a.py": "", "out/report.json": body, "out/model.glb": BIG})
+ self.assertEqual(code, 1)
+ self.assertEqual((v["summary"], v["findings"]), ("no model yet", []))
+ self.assertEqual(v["artifact"], "out/model.glb", "the last export still shows")
+
+ def test_an_empty_workspace(self):
+ code, _, v = self.run_in({})
+ self.assertEqual((code, v["summary"], v["phases"][0]["state"]), (1, "no model yet", "active"))
+
+ def test_run_as_a_script(self):
+ with tempfile.TemporaryDirectory() as d:
+ (Path(d) / "scenes").mkdir()
+ (Path(d) / "scenes" / "a.py").write_text("")
+ with mock.patch.dict(os.environ, {"HARNESS_WORKSPACE": d}), mock.patch.object(sys, "argv", ["verdict.py"]), contextlib.redirect_stdout(io.StringIO()):
+ with self.assertRaises(SystemExit) as exit_:
+ runpy.run_path(str(HERE / "verdict.py"), run_name="__main__")
+ self.assertEqual(exit_.exception.code, 1)
+ self.assertEqual(json.loads((Path(d) / ".harness" / "verdict.json").read_text())["summary"], "no model yet")
+
if __name__ == "__main__": unittest.main()
diff --git a/store/agents/blender/toolchain/verdict.py b/store/agents/blender/toolchain/verdict.py
index a3155a95e..2c5829b26 100755
--- a/store/agents/blender/toolchain/verdict.py
+++ b/store/agents/blender/toolchain/verdict.py
@@ -67,6 +67,8 @@ def main(argv: list[str]) -> int:
try:
report = json.loads(rp.read_text())
except ValueError:
+ pass
+ if not isinstance(report, dict): # half-written, or JSON that is not an object: no report
report = None
ok = lambda p: bool(p) and (WS / p).is_file() and (WS / p).stat().st_size > 500
verdict = judge(bool(has_script), report, bool(glb_path(report, ok)), ok("out/preview.png"), ok("out/turntable.mp4"), glb_path(report, ok) or "out/model.glb")