I want to write a Handler
that runs code only when the user stops translating an object, using bpy.ops.transform.translate
(the 'G' key). So while the user is sliding the mouse around, nothing should happen besides the object being translated. But when they exit the modal operator
(by left-clicking or pressing 'enter'), my code should run.
My approach:
@bpy.app.handlers.persistent
def detect_change(scene):
op = bpy.context.active_operator
opname = op.bl_idname.replace("_OT_", ".").lower() if op else ''
# On entering translate mode, start checking for exit
if opname == "transform.translate":
waiting = True # This will be some sort of persistent variable.
return
# If not in translate mode, then run code
elif waiting:
my_function()
waiting = False
# Add the handler
bpy.app.handlers.scene_update_post.append(detect_change)
The only problem is that after exiting the translate modal operator, the active_operator remains at transform.translate
. It doesn't switch until another operator is called.
Any way to either force the operator to update to None
or to detect exiting the modal operator?
transform.translate
? A bit inconvenient for the user to have to remember, but I see how that could work. $\endgroup$post_operator
andpre_operator
Handlers
? (I know they don't exist, just wondering if it would be useful addition). $\endgroup$