1
$\begingroup$

I seem to get two different behaviours from the Blender interface and from external scripts when trying to invoke my addon's operator function object.myaddonoperator()

import bpy

class MyAddonOperator(bpy.types.Operator):
    bl_idname = "object.myaddonoperator"
    bl_label = "My Addon"
    bl_options = {'REGISTER'}

    @classmethod
    def poll(self, context):
        return context.mode == 'OBJECT'

    def invoke(self, context, event):
        do_stuff()

        return {'FINISHED'}

From the console within Blender, calling bpy.ops.object.myaddonoperator('INVOKE_DEFAULT') works just fine, but when using an external script with blender -b -P my_script.py, calling the same results in wm_operator_invoke: invalid operator call 'OBJECT_OT_myaddonoperator'.

What am I doing wrong?

$\endgroup$

2 Answers 2

3
$\begingroup$

To use the plugin in the way intended, execute() needs to be used instead of invoke(). The execute method can still use the invoke code by calling it with an event of None:

class MyAddonOperator(bpy.types.Operator):
    bl_idname = "object.myaddonoperator"
    bl_label = "My Addon"
    bl_options = {'REGISTER'}

    @classmethod
    def poll(self, context):
        return context.mode == 'OBJECT'

    def execute(self, context):
        return self.invoke(context, None)    

    def invoke(self, context, event):
        do_stuff()

        return {'FINISHED'}

Now the addon can be called with bpy.ops.object.myaddonoperator('EXEC_DEFAULT')

$\endgroup$
0
$\begingroup$

@Chris Watts Doesn't seem to be valid in 2022 (or am I wrong?). Created this small addon and calling bpy.ops.object.myaddonoperator('EXEC_DEFAULT') returns an error:

AttributeError: Calling operator "bpy.ops.object.myaddonoperator" error, could not be found

bl_info = {
    "name": "Call Operator from console",
    "category": "Object",
}

import bpy

class ABC_OT_MyAddonOperator(bpy.types.Operator):
    bl_idname = "object.myaddonoperator"
    bl_label = "My Addon"
    bl_options = {'REGISTER'}

    @classmethod
    def poll(self, context):
        return context.mode == 'OBJECT'

    def execute(self, context):
        return self.invoke(context, None)    

    def invoke(self, context, event):
        print('aaaaaaaaaaaaaaaaaaaaaaa')
        return {'FINISHED'}

def register():
    bpy.utils.register_class(ABC_OT_MyAddonOperator)

def unregister():
    bpy.utils.unregister_class(ABC_OT_MyAddonOperator)

if __name__ == "__main__":
    register()

P.S. not sure if this should have been an alternate post or a comment below your code sample.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .