2
$\begingroup$

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?

$\endgroup$
8
  • $\begingroup$ You may wanna look into Macros, but there is no real reliable way to achieve this. blender.stackexchange.com/questions/3032/… $\endgroup$
    – CodeManX
    Jul 17, 2014 at 21:43
  • $\begingroup$ So the idea would be to call the macro instead of directly invoking transform.translate? A bit inconvenient for the user to have to remember, but I see how that could work. $\endgroup$
    – supergra
    Jul 17, 2014 at 21:50
  • $\begingroup$ Keep in mind that in macro calling a modal op and then a regular op, the regular op will be executed before the modal op. Waiting for a modal op to finished is not officially supported. But you may be able to circumvent it with another modal op, that needs to be invoked and wait for the event that ends the other modal op (yes, this is really hacky not and never fully reliable). $\endgroup$
    – CodeManX
    Jul 17, 2014 at 21:55
  • $\begingroup$ Oh weird. In that case, I'll hold out. I wonder if there's room for post_operator and pre_operator Handlers? (I know they don't exist, just wondering if it would be useful addition). $\endgroup$
    – supergra
    Jul 17, 2014 at 22:02
  • $\begingroup$ window_manager.operators keeps a log of the recent operators. Not sure if it is useful and what it does. $\endgroup$ Jul 17, 2014 at 22:41

0

You must log in to answer this question.

Browse other questions tagged .