diff --git a/surfaces/blender/brokkr_bridge/blender_target.py b/surfaces/blender/brokkr_bridge/blender_target.py index a3156c0..3b9a81a 100644 --- a/surfaces/blender/brokkr_bridge/blender_target.py +++ b/surfaces/blender/brokkr_bridge/blender_target.py @@ -51,6 +51,9 @@ MUTATING_ACTIONS = ( "setObjectCustomProperty", "selectObject", "assignMaterial", "importGltf", "exportGltf", "setCamera", "setLight", "setWorld", "renderStill", ) +# While an object is in Edit Mode, undo steps are edit-mesh steps and an object-level write is not +# reverted by one undo (brokkr-live:ruling:object-intents-refused-in-edit-mode). These touch no object. +EDIT_MODE_SAFE_ACTIONS = ("setWorld", "exportGltf", "renderStill") REQUESTED_BY_SHAPE = re.compile(r"^[A-Za-z0-9._-]+/[A-Za-z0-9._-]+$") NOTE_LIMIT = 200 @@ -517,6 +520,9 @@ class BrokkrBlenderTarget: denial = "Intent has no requestedBy of the form '/'." elif action not in MUTATING_ACTIONS and action not in READ_ACTIONS and action not in na.NodeActions.ACTIONS: denial = f"Unsupported Blender command action: {action}" + elif action not in EDIT_MODE_SAFE_ACTIONS and self._edit_mode_object() is not None: + denial = (f"Object {self._edit_mode_object()!r} is in Edit Mode: object-level intents are refused " + "until she leaves it; nothing was changed.") if denial: if isinstance(command_id, str) and command_id: self._receipted.add(command_id) @@ -543,6 +549,12 @@ class BrokkrBlenderTarget: self._receipted.add(command_id) return receipt + def _edit_mode_object(self) -> str | None: + for obj in self.bpy.data.objects.values(): + if getattr(obj, "mode", "OBJECT") == "EDIT": + return obj.name + return None + def _push_undo(self, label: str) -> None: windows = self.bpy.context.window_manager.windows if windows: diff --git a/surfaces/blender/tests/live/agent_turn_undo.py b/surfaces/blender/tests/live/agent_turn_undo.py index c41ca89..75716ee 100644 --- a/surfaces/blender/tests/live/agent_turn_undo.py +++ b/surfaces/blender/tests/live/agent_turn_undo.py @@ -15,6 +15,7 @@ import traceback import types from pathlib import Path +import bmesh import bpy ADDON = Path(os.environ["BROKKR_ADDON"]).resolve() @@ -68,6 +69,11 @@ def names(): S = {} +def vz(): + bm = bmesh.from_edit_mesh(bpy.data.objects["Cube"].data); bm.verts.ensure_lookup_table() + return round(bm.verts[0].co.z, 3) + + def s_baseline(): push("driver baseline") S["objects0"] = names() @@ -114,25 +120,28 @@ def s_edit_enter(): bpy.ops.object.mode_set(mode="EDIT") S["loc0"] = loc("Cube") push("driver edit-mode baseline") + S["z0"] = vz() + bm = bmesh.from_edit_mesh(cube.data); bm.verts.ensure_lookup_table(); bm.verts[0].co.z += 5.0 + bmesh.update_edit_mesh(cube.data) + push("her edit-mesh edit") R["editMode"] = {"mode_before": bpy.context.mode, "loc_before": S["loc0"]} def s_edit_agent(): r = intent(action="setObjectTransform", objectName="Cube", location=[0, 0, 5]) - R["editMode"].update(mode_after_intent=bpy.context.mode, loc_after_intent=loc("Cube"), receipt=r["status"], undoLabel=r["undoLabel"]) - check("edit mode: agent transform applied", loc("Cube") == [0.0, 0.0, 5.0], loc("Cube")) + R["editMode"].update(mode_after_intent=bpy.context.mode, loc_after_intent=loc("Cube"), receipt=r["status"], + message=r["message"], undoLabel=r["undoLabel"]) + check("edit mode: object-level intent refused, scene and her mode unchanged", + r["status"] == "denied" and "Edit Mode" in r["message"] and r["undoLabel"] == "" + and loc("Cube") == S["loc0"] and bpy.context.mode == "EDIT_MESH", R["editMode"]) def s_edit_undo(): + """One undo must revert her edit-mesh step: a step pushed by the refused intent would have taken it.""" undo() - R["editMode"].update(mode_after_undo=bpy.context.mode, loc_after_undo=loc("Cube")) - check("edit mode: one undo reverts the agent transform", loc("Cube") == S["loc0"], R["editMode"]) - - -def s_edit_undo_again(): - """Diagnostic, not a check: where does the object-level write sit relative to Edit Mode steps?""" - undo() - R["editMode"].update(mode_after_second_undo=bpy.context.mode, loc_after_second_undo=loc("Cube")) + R["editMode"].update(mode_after_undo=bpy.context.mode, loc_after_undo=loc("Cube"), z_before=S["z0"] + 5.0, z_after_undo=vz()) + check("edit mode: undo stack unchanged by the refused intent (one undo reverts her edit-mesh step)", + vz() == S["z0"] and loc("Cube") == S["loc0"], R["editMode"]) def s_exit(): @@ -142,7 +151,7 @@ def s_exit(): steps = [s_baseline, s_create, s_transform, s_undo_transform, s_undo_create, s_denied_setup, s_denied_undo, - s_edit_enter, s_edit_agent, s_edit_undo, s_edit_undo_again, s_exit] + s_edit_enter, s_edit_agent, s_edit_undo, s_exit] i = 0 diff --git a/surfaces/blender/tests/live/agent_turn_undo.result.json b/surfaces/blender/tests/live/agent_turn_undo.result.json index 5ae93a7..455335f 100644 --- a/surfaces/blender/tests/live/agent_turn_undo.result.json +++ b/surfaces/blender/tests/live/agent_turn_undo.result.json @@ -62,17 +62,8 @@ ] }, { - "name": "edit mode: agent transform applied", + "name": "edit mode: object-level intent refused, scene and her mode unchanged", "ok": true, - "detail": [ - 0.0, - 0.0, - 5.0 - ] - }, - { - "name": "edit mode: one undo reverts the agent transform", - "ok": false, "detail": { "mode_before": "EDIT_MESH", "loc_before": [ @@ -84,22 +75,48 @@ "loc_after_intent": [ 0.0, 0.0, - 5.0 + 0.0 ], - "receipt": "accepted", - "undoLabel": "Brokkr nibu/hands: setObjectTransform 00000005", + "receipt": "denied", + "message": "Object 'Cube' is in Edit Mode: object-level intents are refused until she leaves it; nothing was changed.", + "undoLabel": "", "mode_after_undo": "EDIT_MESH", "loc_after_undo": [ 0.0, 0.0, - 5.0 + 0.0 ], - "mode_after_second_undo": "OBJECT", - "loc_after_second_undo": [ + "z_before": 6.0, + "z_after_undo": 1.0 + } + }, + { + "name": "edit mode: undo stack unchanged by the refused intent (one undo reverts her edit-mesh step)", + "ok": true, + "detail": { + "mode_before": "EDIT_MESH", + "loc_before": [ 0.0, 0.0, 0.0 - ] + ], + "mode_after_intent": "EDIT_MESH", + "loc_after_intent": [ + 0.0, + 0.0, + 0.0 + ], + "receipt": "denied", + "message": "Object 'Cube' is in Edit Mode: object-level intents are refused until she leaves it; nothing was changed.", + "undoLabel": "", + "mode_after_undo": "EDIT_MESH", + "loc_after_undo": [ + 0.0, + 0.0, + 0.0 + ], + "z_before": 6.0, + "z_after_undo": 1.0 } } ], @@ -130,12 +147,12 @@ }, { "intent": "setObjectTransform", - "status": "accepted", - "undoLabel": "Brokkr nibu/hands: setObjectTransform 00000005", - "message": "Blender object transform updated." + "status": "denied", + "undoLabel": "", + "message": "Object 'Cube' is in Edit Mode: object-level intents are refused until she leaves it; nothing was changed." } ], - "ok": false, + "ok": true, "editMode": { "mode_before": "EDIT_MESH", "loc_before": [ @@ -147,21 +164,18 @@ "loc_after_intent": [ 0.0, 0.0, - 5.0 + 0.0 ], - "receipt": "accepted", - "undoLabel": "Brokkr nibu/hands: setObjectTransform 00000005", + "receipt": "denied", + "message": "Object 'Cube' is in Edit Mode: object-level intents are refused until she leaves it; nothing was changed.", + "undoLabel": "", "mode_after_undo": "EDIT_MESH", "loc_after_undo": [ - 0.0, - 0.0, - 5.0 - ], - "mode_after_second_undo": "OBJECT", - "loc_after_second_undo": [ 0.0, 0.0, 0.0 - ] + ], + "z_before": 6.0, + "z_after_undo": 1.0 } } \ No newline at end of file diff --git a/surfaces/blender/tests/test_agent_turn.py b/surfaces/blender/tests/test_agent_turn.py index 353f709..db950a6 100644 --- a/surfaces/blender/tests/test_agent_turn.py +++ b/surfaces/blender/tests/test_agent_turn.py @@ -26,6 +26,19 @@ class AgentTurnTests(unittest.TestCase): self.assertEqual(receipt["status"], "denied") self.assertEqual(self.bpy.created, []) + def test_object_intent_is_refused_while_an_object_is_in_edit_mode(self): + self.bpy.mesh.mode = "EDIT" + receipt = self.run_action(action="setObjectTransform", objectName="Cube", location=[0, 0, 5]) + self.assertEqual(receipt["status"], "denied") + self.assertIn("Edit Mode", receipt["message"]) + self.assertEqual(receipt["undoLabel"], "") + self.assertEqual(self.bpy.undo.pushes, []) + self.assertEqual(self.bpy.created, []) + self.assertEqual(self.bpy.mesh.mode, "EDIT") # the agent never changes her mode + self.assertEqual(self.run_action(action="renderStill", outputPath="r.png", engine="CYCLES")["status"], "accepted") + self.bpy.mesh.mode = "OBJECT" + self.assertEqual(self.run_action(action="createObject", objectType="EMPTY")["status"], "accepted") + def test_unknown_action_is_denied(self): self.assertEqual(self.run_action(action="runPython")["status"], "denied") self.assertEqual(self.bpy.undo.pushes, [])