2
$\begingroup$

The Macro operator in the following code calls two other operators that log the calls of their invoke and execute functions. If the macro operator is called from the spacebar menu, the console output is what is expected:

TestOp1.invoke
TestOp1.execute
TestOp2.invoke
TestOp2.execute

However, if TestMacro is called from a python context (a script / addon / interpreter window), it returns {'FINISHED'} without calling the children operators TestOp1 and TestOp2.

What is the proper way to register a Macro operator ?

import bpy
from bpy.types import Macro, Operator
from bpy.utils import register_class, unregister_class

bl_info = {
    'name': "Test macros",
}


class TestOp1(Operator):
    bl_idname = 'wm.test_op1'
    bl_label = 'Test Op1'

    def execute(self, context):
        print('TestOp1.execute')
        return {'FINISHED'}

    def invoke(self, context, event):
        print('TestOp1.invoke')
        return self.execute(context)


class TestOp2(Operator):
    bl_idname = 'wm.test_op2'
    bl_label = 'Test Op2'

    def execute(self, context):
        print('TestOp2.execute\n')
        return {'FINISHED'}

    def invoke(self, context, event):
        print('TestOp2.invoke')
        return self.execute(context)


class TestMacro(Macro):
    bl_idname = 'wm.test_macro'
    bl_label = 'Test Macro'
    bl_options = {'REGISTER', 'UNDO'}


def register():
    register_class(TestOp1)
    register_class(TestOp2)
    register_class(TestMacro)

    TestMacro.define(TestOp1.bl_idname)
    TestMacro.define(TestOp2.bl_idname)


def unregister():
    unregister_class(TestOp1)
    unregister_class(TestOp2)
    unregister_class(TestMacro)


if __name__ == '__main__':
    register()

    # This doesnt call TestOp1 or TestOp2
    bpy.ops.wm.test_macro('INVOKE_DEFAULT')
$\endgroup$
2
$\begingroup$

The children operators need to be registered with their fully qualified internal name:

TestMacro.define('WM_OT_test_op1')
TestMacro.define('WM_OT_test_op2')
$\endgroup$
  • $\begingroup$ Good find... TestMacro.define(bpy.ops.wm.test_op1.idname()) $\endgroup$ – batFINGER Aug 6 '16 at 21:47
  • $\begingroup$ Doesn't work within the def register() function. At that point the Operators are not registered under bpy.ops yet: AttributeError: 'BPyOpsSubModOp' object has no attribute 'test_op1' $\endgroup$ – user29213 Aug 7 '16 at 21:23

Your Answer

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