5
$\begingroup$

In my add-on I execute a modal Blender operator, namely

bpy.ops.transform.translate('INVOKE_DEFAULT')

After a user finished moving a Blender object, I'd like to execute some code.

Is there a trick how that could be done?

The question could be probably reformulated in the following way: is there a trick to get notification when a modal operator finished its job?

$\endgroup$
7
$\begingroup$

One way to do this is to create a macro operator, with the transform operator being the first component, and a second custom operator for running your finalizing code, once the modal operator is finished.

Unfortunately the macro API is barely documented, so here is a minimum example script:

import bpy
from bpy.types import Operator, Macro


# Our finalizing operator, shall run after transform
class Finalize(Operator):
    bl_idname = "test.finalize"
    bl_label = "Finalize"

    def execute(self, context):
        print("DONE!")
        return {'FINISHED'}


# Macro operator to concatenate transform and our finalization
class Test(Macro):
    bl_idname = "TEST_OT_Test"
    bl_label = "Test"


# Note that we have to register classes first before populating
# the Macro operator    
bpy.utils.register_class(Finalize)
bpy.utils.register_class(Test)


# The important bit: populate the macro operator with a sequence
# of existing other operators
Test.define("transform.translate")
Test.define("test.finalize")

You can run this script and then test it in the viewport via the spacebar menu.

One more note: if the transform operator is cancelled, so is the macro operator, i.e. in case of cancelling the second operator will never run. If that is necessary for some reason you could make a wrapper around the existing transform operator, which then implements own behavior in the cancel(self, context) function.

$\endgroup$
  • $\begingroup$ I didn't get how to create a wrapper to deal with cancelling. Could you please extend your answer with the related code snippet? $\endgroup$ – vvoovv Oct 9 '15 at 7:57
  • $\begingroup$ Ah sorry, i thought i'd recall doing this in the past, but it's not so easy because of the difficulty of extending existing operators. Could still be possible somehow, but not sure if doing this from python as opposed to C hacking is the same. $\endgroup$ – lukas_t Oct 11 '15 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.