From d1b3b2ee5418a065ea645f4b3ee5c54300da0e97 Mon Sep 17 00:00:00 2001 From: Libby Date: Tue, 10 Nov 2026 14:31:06 +0000 Subject: [PATCH] blender: createObject refuses unknown objectType and non-cube meshType TEXT and CURVE used to fall through to an EMPTY with an accepted receipt. Co-Authored-By: Claude Sonnet 5.5 --- .../blender/brokkr_bridge/blender_target.py | 12 +++++- surfaces/blender/docs/install.md | 4 ++ surfaces/blender/tests/test_scene_actions.py | 41 ++++++++++++++++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/surfaces/blender/brokkr_bridge/blender_target.py b/surfaces/blender/brokkr_bridge/blender_target.py index 628c199..dcae328 100644 --- a/surfaces/blender/brokkr_bridge/blender_target.py +++ b/surfaces/blender/brokkr_bridge/blender_target.py @@ -34,6 +34,8 @@ SYNC_VAR_SCHEMA = "brokkr.sync.var.v0" SYNC_TIMELINE_BINDING_SCHEMA = "brokkr.sync.timeline_binding.v0" SYNC_RECEIPT_SCHEMA = "brokkr.sync.receipt.v0" +CREATABLE_OBJECT_TYPES = ("MESH", "LIGHT", "CAMERA", "EMPTY") + EVALUATED_POSITION_CAP = 256 EVALUATED_VERTEX_LIMIT = 1_000_000 EVALUATED_INSTANCE_SCAN_LIMIT = 100_000 # depsgraph items visited per capture; iterating one in Python costs ~5 us @@ -518,6 +520,14 @@ class BrokkrBlenderTarget: def _create_object(self, context: Any, command: dict[str, Any]) -> dict[str, Any]: object_type = command.get("objectType", "EMPTY") + if not isinstance(object_type, str) or object_type not in CREATABLE_OBJECT_TYPES: + shown = object_type if isinstance(object_type, str) else repr(object_type) + raise sa.ActionRefused( + f"objectType {shown!r} cannot be created; allowed: {', '.join(CREATABLE_OBJECT_TYPES)}.") + mesh_type = command.get("meshType") + if object_type == "MESH" and mesh_type is not None and ( + not isinstance(mesh_type, str) or mesh_type.upper() != "CUBE"): + raise sa.ActionRefused(f"meshType {mesh_type!r} is not available; a MESH is a CUBE.") name = command.get("name") or f"Brokkr {object_type.title()}" location = _vector(command.get("location"), 3, (0.0, 0.0, 0.0)) @@ -527,7 +537,7 @@ class BrokkrBlenderTarget: self.bpy.ops.object.light_add(type=command.get("lightType", "POINT"), location=location) elif object_type == "CAMERA": self.bpy.ops.object.camera_add(location=location) - else: + else: # EMPTY self.bpy.ops.object.empty_add(type=command.get("emptyDisplayType", "PLAIN_AXES"), location=location) obj = context.object diff --git a/surfaces/blender/docs/install.md b/surfaces/blender/docs/install.md index 6548af4..04d5a1e 100644 --- a/surfaces/blender/docs/install.md +++ b/surfaces/blender/docs/install.md @@ -116,6 +116,10 @@ Admitted command actions: Scene action fields (all other keys are ignored; a refused intent changes nothing and gets a `failed` receipt whose message names the rule): +- `createObject`: optional `objectType` (`MESH`, `LIGHT`, `CAMERA`, `EMPTY`; default `EMPTY`), + `name`, `location`. Any other `objectType` (for example `TEXT` or `CURVE`) is refused with + the allowed set named. A `MESH` is a cube (`size`); a `meshType` other than `CUBE` + (case-insensitive) is refused rather than ignored. - `importGltf`: `filepath`, `.gltf` or `.glb`, relative to the add-on preference `Import Directory` (absolute paths, `..` and symlinks that resolve outside it are refused). Empty `Import Directory` disables the action. Needs Blender's glTF add-on. diff --git a/surfaces/blender/tests/test_scene_actions.py b/surfaces/blender/tests/test_scene_actions.py index 6244188..aa4b498 100644 --- a/surfaces/blender/tests/test_scene_actions.py +++ b/surfaces/blender/tests/test_scene_actions.py @@ -86,7 +86,19 @@ class FakeBpy: worlds=NS(get=lambda name: None, new=self.new_world), ) self.path = NS(abspath=lambda p: p) - self.ops = NS(import_scene=NS(gltf=self.import_gltf), render=NS(render=self.render_still)) + self.created = [] + self.app = NS(background=False) + add = lambda kind: (lambda **kw: self.add_object(kind, kw)) + self.ops = NS(import_scene=NS(gltf=self.import_gltf), render=NS(render=self.render_still), + mesh=NS(primitive_cube_add=add("MESH")), object=NS(light_add=add("LIGHT"), + camera_add=add("CAMERA"), empty_add=add("EMPTY"))) + self.on_add = lambda obj: None + + def add_object(self, kind, kwargs): + self.created.append((kind, kwargs)) + obj = NS(name=kind.title(), type=kind) + self.data.objects[obj.name] = obj + self.on_add(obj) def new_world(self, name): background = NS(inputs={"Color": NS(default_value=None), "Strength": NS(default_value=None)}) @@ -117,7 +129,8 @@ class ActionTests(unittest.TestCase): self.bpy = FakeBpy() self.target = bt.BrokkrBlenderTarget(self.bpy) self.target.configure_action_dirs(str(self.root / "in"), str(self.root / "out")) - self.context = NS(scene=self.bpy.scene) + self.context = NS(scene=self.bpy.scene, object=None) + self.bpy.on_add = lambda obj: setattr(self.context, "object", obj) def run_action(self, **command): command.setdefault("commandId", "c1") @@ -238,6 +251,30 @@ class ActionTests(unittest.TestCase): self.target.configure_action_dirs(str(self.root / "in"), "") self.assertEqual(self.run_action(action="renderStill", outputPath="a.png")["status"], "failed") + # createObject + def test_create_object_makes_the_four_admitted_types(self): + for kind in ("MESH", "LIGHT", "CAMERA", "EMPTY"): + receipt = self.run_action(action="createObject", objectType=kind, name="N") + self.assertEqual(receipt["status"], "accepted", kind) + self.assertEqual(self.bpy.created[-1][0], kind) + self.assertEqual(self.run_action(action="createObject", objectType="MESH", meshType="cube")["status"], "accepted") + + def test_create_object_refuses_other_types_naming_the_type_and_the_allowed_set(self): + for kind in ("TEXT", "CURVE", "mesh", "", None, 3): + receipt = self.run_action(action="createObject", objectType=kind) + self.assertEqual(receipt["status"], "failed", kind) + for allowed in ("MESH", "LIGHT", "CAMERA", "EMPTY"): + self.assertIn(allowed, receipt["message"]) + self.assertIn(repr(kind) if not isinstance(kind, str) else kind, receipt["message"]) + self.assertEqual(self.bpy.created, []) + + def test_create_object_refuses_a_mesh_type_other_than_cube(self): + for mesh_type in ("CYLINDER", "", 1): + receipt = self.run_action(action="createObject", objectType="MESH", meshType=mesh_type) + self.assertEqual(receipt["status"], "failed", mesh_type) + self.assertIn("CUBE", receipt["message"]) + self.assertEqual(self.bpy.created, []) + # importGltf: names def test_import_refuses_drive_unc_extended_stream_and_device_names(self): for name in ("C:foo.glb", "C:\\x\\model.glb", "\\\\host\\share\\model.glb", "\\\\?\\C:\\model.glb",