2.5 calling same invoke for multiple buttons?

My script creates GUI elements that are similar to the “Custom properties” column in the Object attributes panel. Notice how each custom property has little buttons to the right that let you delete that property, etc., and that many of these can be created.

My script also dynamically creates many buttons where each operates on its corresponding data. For example, button number 41 causes something to happen to element 41 of some internal data.

My question is, do I have to define a separate Operator class to handle each button? Was that what the Custom properties code had to do to implement its GUI?

It would be nice to define one Operator class that handles all buttons, and its invoke function could somehow tell which button was clicked via an application context variable, for example.

As it is I have to create one Operator class for each button. I know how to simplify this by sub-classing and by running a loop that uses exec() to define each class, but its still a clunky way to do it.

For example, this would be nice:


# in the panel class:
    draw(self.context):
        MyUIlayout.operator("object.MyGenericOperator", text = "Click me!",appcontext="1")
        MyUIlayout.operator("object.MyGenericOperator", text = "Click me!",appcontext="2")

....
# and in the Operator class:
    def invoke(self, context, event) :
         print("You clicked button number %d" % (event.appcontext))

Maybe the event parameter already has this info? I can’t find docs for it.

Any help is appreciated.

I don’t think you can pass arguments to operators that way. One way or another, you have to have different operator classes. But they could be subclasses of a custom class of yours, to save duplication of code.