3
$\begingroup$

I already learned that you can't and don't need to overload in Python, but I didn't know how to call it otherwise.

(all pseudo code)

I have a button:

myBox = layout.box()
myBox.operator(my_Button.bl_idname, text = "Move it") 

And want to add a non-obligatory parameter to the execute method:

class my_Button(bpy.types.Operator):
   bl_idname = "object.my_button"
   def execute(self, context, calledByButton=True):   

So I can call the method by using the button, or somewhere in the script:

bpy.ops.object.my_button(False)

But I get an error on registering it (strangly for the next class)

ValueError: expected Operator, my_NextButton class "execute" function to have 2 args , found 3

How would I do that correctly?

$\endgroup$
4
$\begingroup$

You do this correctly like this:

import bpy

class MyButton(bpy.types.Operator):
    bl_idname = "object.my_button"
    bl_label = "My Button"
    flag = bpy.props.BoolProperty(default=True)

    def execute(self, context):
        if self.flag:
            print('Called by button')
        else:
            print('Not called by button')

 bpy.utils.register_class(MyButton)

 #usage:
 myBox.operator("object.my_button").flag = True
 bpy.ops.object.my_button(flag=False)
| improve this answer | |
$\endgroup$
  • $\begingroup$ While this seemed correct at first, I don't seem to be able to do this: bpy.ops.object.my_button.flag = True, because the class isn't initialized and of course I'd run bpy.ops.object.my_button() afterwards to have the flag set at runtime. $\endgroup$ – bortran Jan 28 '15 at 14:47
  • $\begingroup$ You need to call bpy.ops.object.my_button(flag=False), I will add that. Also the class should have bl_label $\endgroup$ – Jaroslav Jerryno Novotny Jan 28 '15 at 15:18

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.