From 48c0aeee6d702c28da79fd275042d95455eb557e Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 27 Nov 2026 18:07:37 +0100 Subject: [PATCH] Live collab: depsgraph timing probe Records when depsgraph updates from an agent edit in a timer arrive and which ids they name, for the human-edit detection fork (Q3). Co-Authored-By: Claude Opus 5.5 --- .../live-collab/probe_depsgraph_timing.py | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 docs/research/live-collab/probe_depsgraph_timing.py diff --git a/docs/research/live-collab/probe_depsgraph_timing.py b/docs/research/live-collab/probe_depsgraph_timing.py new file mode 100644 index 0000000..d763e07 --- /dev/null +++ b/docs/research/live-collab/probe_depsgraph_timing.py @@ -0,0 +1,134 @@ +"""Probe: when do depsgraph updates caused by an agent edit in a timer arrive, and what do they name? + +blender --factory-startup --python probe_depsgraph_timing.py -- + +Each case runs in its own timer tick. AGENT is True only while the timer callback +runs the edit. depsgraph_update_post and undo_post log every call with AGENT's value, +the tick that was current, and the ids depsgraph.updates names. +""" +import json +import sys +import time +import traceback + +import bpy + +OUT = sys.argv[sys.argv.index("--") + 1] +log = {"blender": bpy.app.version_string, "events": []} +AGENT = False +TICK = "setup" +T0 = time.perf_counter() + + +def ev(kind, **kw): + log["events"].append({"kind": kind, "tick": TICK, "agent": AGENT, + "ms": round((time.perf_counter() - T0) * 1000, 1), **kw}) + + +@bpy.app.handlers.persistent +def on_depsgraph(scene, depsgraph): + ups = [] + for u in depsgraph.updates: + ups.append(f"{type(u.id).__name__}:{u.id.name}" + f"{'/T' if u.is_updated_transform else ''}{'/G' if u.is_updated_geometry else ''}") + ev("depsgraph", updates=ups) + + +@bpy.app.handlers.persistent +def on_undo(scene, *_): + ev("undo_post") + + +@bpy.app.handlers.persistent +def on_redo(scene, *_): + ev("redo_post") + + +def win_ctx(): + win = bpy.context.window_manager.windows[0] + area = next(a for a in win.screen.areas if a.type == "VIEW_3D") + region = next(r for r in area.regions if r.type == "WINDOW") + return bpy.context.temp_override(window=win, area=area, region=region) + + +def setup(): + # B follows A through a Copy Location constraint: an indirect dependent of A. + a = bpy.data.objects["Cube"] + with win_ctx(): + bpy.ops.mesh.primitive_cone_add("EXEC_DEFAULT", False, location=(0, 3, 0)) + b = bpy.context.active_object + b.name = "Follower" + c = b.constraints.new("COPY_LOCATION") + c.target = a + c.use_y = False + with win_ctx(): + bpy.ops.ed.undo_push(message="setup") + + +def agent(fn, flush): + global AGENT + AGENT = True + ev("turn_begin", flush=flush) + fn() + with win_ctx(): + bpy.ops.ed.undo_push(message=f"agent {TICK}") + if flush: + bpy.context.view_layer.update() + ev("turn_end") + AGENT = False + + +def move_a(x): + return lambda: setattr(bpy.data.objects["Cube"].location, "x", x) + + +def add_op(): + with win_ctx(): + bpy.ops.mesh.primitive_uv_sphere_add("EXEC_DEFAULT", False, location=(-4, 0, 0)) + + +def undo_once(): + with win_ctx(): + bpy.ops.ed.undo() + + +cases = [ + ("setup", setup), + ("idle1", lambda: None), + ("rna-noflush", lambda: agent(move_a(1.0), False)), + ("idle2", lambda: None), + ("rna-flush", lambda: agent(move_a(2.0), True)), + ("idle3", lambda: None), + ("op-noflush", lambda: agent(add_op, False)), + ("idle4", lambda: None), + ("op-flush", lambda: agent(add_op, True)), + ("idle5", lambda: None), + ("undo", undo_once), # stands in for a human Ctrl+Z (not an agent turn) + ("idle6", lambda: None), +] +i = 0 + + +def tick(): + global i, TICK + if i < len(cases): + TICK, fn = cases[i] + try: + fn() + except Exception: + ev("error", error=traceback.format_exc()) + TICK = f"after:{TICK}" + i += 1 + with open(OUT, "w") as f: + json.dump(log, f, indent=1) + return 0.5 + with open(OUT, "w") as f: + json.dump(log, f, indent=1) + bpy.ops.wm.quit_blender() + return None + + +bpy.app.handlers.depsgraph_update_post.append(on_depsgraph) +bpy.app.handlers.undo_post.append(on_undo) +bpy.app.handlers.redo_post.append(on_redo) +bpy.app.timers.register(tick, first_interval=2.0)