I'm dynamically registering a variable number of panel classes, all sub-classed from a custom panel class that includes an operator. Thus all the new panel sub-classes contain a button that executes this common, ancestral operator code. When the button on one of these new sub-class panels is clicked I would like the operator code to be able to determine which of the panel/button instances invoked it. Is such a thing possible? My apologies if I've not described this very clearly. Basically I'm trying to recreate the same functionality as the 'X' button that sits in the header of modifiers sub-panels. In my example, the code behind the X button needs to know which panel class the clicked button belongs to in order to unregister the correct panel. Any advice is greatly welcomed and appreciated. Thank you!
1 Answer
You can define a property for the operator in order to identify something.
For instance:
class SimpleOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"
# >>>> Declare a property
some_value = IntProperty(default=0)
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
print(self.some_value)
return {'FINISHED'}
Then, in the panel you can assign the property value as you want:
class HelloWorldPanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Hello World Panel"
bl_idname = "OBJECT_PT_hello"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "object"
def draw(self, context):
layout = self.layout
row = layout.row()
op = row.operator("object.simple_operator")
# >>>> Assign its value
op.some_value = 1
op = row.operator("object.simple_operator")
# >>>> Assign its value
op.some_value = 2
Alternatively, you can assign the value in a subclass if your want to work with subclasses.
-
1$\begingroup$ That's perfect, thank you! And what a resounding forehead slap! I knew I was making it more complicated than it needed to be. Much appreciated! $\endgroup$ Dec 7, 2020 at 15:42
row.context_pointer_set(name="foo", data=self.__class__)
$\endgroup$