13
$\begingroup$

Basically, I want to have a single operator which can do multiple things, based on some attributes values.

class MyPanel(bpy.types.Panel):
    # (...)
    layout = self.layout
    row = layout.row()
    row.operator("my.button", text="Button text")

class MY_BUTTON_OT_Button(bpy.types.Operator):
    bl_idname = "my.button"
    bl_description = "Button description"
    bl_label = "Button"
    foo = bpy.props.IntProperty()
    bar = bpy.props.BoolProperty()

    if bar:
        # Do something with foo
    else:
        # Do something else :P

I know how to set one attribute, like so:

    row.operator("my.button", text="Button text").foo=5

But what about multiple arguments?
I tried several things that didn't worked, like (e.g.):

  • With setattr():
    row.operator("my.button", text="Button text").setattr(foo=5,bar=True)

  • With a custom method setVal() in the operator class:
    row.operator("my.button", text="Button text").setVal(foo=5,bar=True)

Maybe I must use a single EnumProperty? I hope to avoid this, so my question is:
Is it possible to pass multiple custom arguments to set attributes in an operator class? And if yes, how ?

Thanks for your help.

$\endgroup$
17
$\begingroup$

There may be other ways, but that's the easiest. Instead of using operator()'s return value (an OperatorProperties) right away, capture it in a variable, and assign them there:

props = row.operator("my.button", text="Button text")
props.foo = 5
props.bar = "Bar"
props.baz = True
$\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.