Brokkr/docs/research/live-collab/probe_timer_undo.py
Meta Vens a975f99a90 Live collaboration target: invariants, probes, identity table
Target document for the brokkr-live campaign: the agent works in the same
live Blender scene the operator is looking at. Records the Blender 5.3
timer-undo and viewport-capture probes the invariants stand on, and the
identity, lifecycle and authority table for every kind the layer adds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 20:00:16 +01:00

146 lines
4.8 KiB
Python

"""Probe: do edits made from a bpy.app.timers callback land on Blender's undo stack?
Run in GUI Blender: blender --factory-startup --python probe_timer_undo.py -- <out.json> [cases, e.g. AB]
Each step is its own timer tick so the event loop runs between steps, as it does
for Brokkr's 1 s drain. Results are written to the JSON path, then Blender quits.
"""
import json
import sys
import traceback
import bmesh
import bpy
OUT = sys.argv[sys.argv.index("--") + 1]
log = {"blender": bpy.app.version_string, "background": bpy.app.background, "steps": []}
def win_ctx():
win = bpy.context.window_manager.windows[0]
area = next(a for a in win.screen.areas if a.type == "VIEW_3D")
return bpy.context.temp_override(window=win, area=area, region=area.regions[-1])
def cube():
return bpy.data.objects.get("Cube")
def state(tag):
c = cube()
s = {
"tag": tag,
"cube_x": round(c.location.x, 3) if c else None,
"objects": sorted(o.name for o in bpy.data.objects),
"mode": bpy.context.mode,
"mesh_v0_z": round(c.data.vertices[0].co.z, 3) if c else None,
}
if c and c.mode == "EDIT":
bm = bmesh.from_edit_mesh(c.data)
bm.verts.ensure_lookup_table()
s["edit_v0_z"] = round(bm.verts[0].co.z if bm.verts else 0, 3)
log["steps"].append(s)
return s
def push(msg):
with win_ctx():
r = bpy.ops.ed.undo_push(message=msg)
log["steps"].append({"tag": f"undo_push:{msg}", "result": list(r)})
def undo():
with win_ctx():
r = bpy.ops.ed.undo()
log["steps"].append({"tag": "undo", "result": list(r)})
def redo():
with win_ctx():
r = bpy.ops.ed.redo()
log["steps"].append({"tag": "redo", "result": list(r)})
# Case A: raw RNA write from a timer, no push. Does Ctrl+Z see it as its own step?
# Case B: RNA write then an explicit undo_push from the timer.
# Case C: an UNDO-flagged operator called from a timer, no explicit push.
# Case D: Edit Mode bmesh write from a timer then undo_push.
# Case E: two operators called with undo=False, then one explicit undo_push.
ONLY = sys.argv[sys.argv.index("--") + 2] if len(sys.argv) > sys.argv.index("--") + 2 else "ABCDE"
steps = [
lambda: (push("baseline"), state("A0 baseline")),
lambda: (setattr(cube().location, "x", 1.0), state("A1 raw write x=1, no push")),
lambda: (undo(), state("A2 after one undo")),
lambda: (push("rebase-B"), state("B0")),
lambda: (setattr(cube().location, "x", 2.0), push("agent: x=2"), state("B1 x=2 pushed")),
lambda: (setattr(cube().location, "x", 3.0), push("agent: x=3"), state("B2 x=3 pushed")),
lambda: (undo(), state("B3 after undo (expect x=2)")),
lambda: (undo(), state("B4 after second undo (expect x=0)")),
lambda: (redo(), state("B5 after redo (expect x=2)")),
lambda: (push("rebase-C"), state("C0")),
lambda: (_add_sphere(), state("C1 op add sphere, no push")),
lambda: (undo(), state("C2 after undo (sphere gone?)")),
lambda: (bpy.context.view_layer.objects.__setattr__("active", cube()), cube().select_set(True), push("rebase-D"), state("D0")),
lambda: (_edit_enter(), state("D1 edit mode")),
lambda: (_edit_write(), push("agent: edit v0"), state("D2 v0 moved, pushed")),
lambda: (undo(), state("D3 after undo (expect v0 back)")),
lambda: (undo(), state("D4 after second undo")),
lambda: (_edit_exit(), push("rebase-E"), state("E0")),
lambda: (_two_ops_one_step(), push("agent: two ops"), state("E1 two ops undo=False + one push")),
lambda: (undo(), state("E2 after one undo (expect both gone)")),
lambda: (undo(), state("E3 after second undo (expect no change)")),
]
def _add_sphere():
with win_ctx():
bpy.ops.mesh.primitive_uv_sphere_add(location=(5, 0, 0))
def _edit_exit():
if bpy.context.mode != "OBJECT":
with win_ctx():
bpy.ops.object.mode_set(mode="OBJECT")
def _two_ops_one_step():
with win_ctx():
bpy.ops.mesh.primitive_cube_add("EXEC_DEFAULT", False, location=(0, 4, 0))
bpy.ops.mesh.primitive_cone_add("EXEC_DEFAULT", False, location=(0, -4, 0))
def _edit_enter():
with win_ctx():
bpy.ops.object.mode_set(mode="EDIT")
def _edit_write():
c = cube()
bm = bmesh.from_edit_mesh(c.data)
bm.verts.ensure_lookup_table()
bm.verts[0].co.z += 5.0
bmesh.update_edit_mesh(c.data)
i = 0
_all = steps
steps = [f for f, c in zip(_all, "AAABBBBBBCCCDDDDDEEEE") if c in ONLY]
def tick():
global i
if i < len(steps):
try:
steps[i]()
except Exception:
log["steps"].append({"tag": f"step {i} error", "error": traceback.format_exc()})
i += 1
with open(OUT, "w") as f:
json.dump(log, f, indent=1)
return 0.3
with open(OUT, "w") as f:
json.dump(log, f, indent=1)
bpy.ops.wm.quit_blender()
return None
bpy.app.timers.register(tick, first_interval=2.0)