0
$\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
$\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$

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.