2
$\begingroup$

I have a UI panel for my addon, with a couple of buttons that should all invoke a custom operator class which append objects from another blend file.

My questions is how do I append specific object based on what specific button is pressed without making a new class (with an execute function attached) for each button.

I have a decent amount of scripting experience but classes and Blender's addon making are super mysterious to me. I know how to pass variable into functions, which is a technique I use when scripting, but have no idea how to do this within classes.

That's my ui_panel.py, which currently has one button but ultimately should have multiple buttons that append different objects from a blend file which store all my assets:

class UI_PT_Panel(bpy.types.Panel): bl_idname = "UI_PT_Panel" bl_label = "My Panel" bl_category = "My Addon" bl_space_type = "VIEW_3D" bl_region_type = "UI" def draw(self, context): layout = self.layout row = layout.row() row.operator('wm.appending_basic_objects', text = "Append object") 

And this is my append_operator.py, which should have a variable handling which object is appended based on which button is pressed in the UI (or something equivalent to that):

class Append_OT_Operator(bpy.types.Operator): bl_idname = "wm.appending_basic_objects" bl_label = "Append the basic objects" def execute(self, context): path = (r'C:/Users/') blendfile = "Assets" + ".blend" bpy.ops.wm.append( filepath = blendfile, directory = path + '/' + blendfile + "/Object/", filename = 'my_object') return {'FINISHED'} 

Finally I have an init.py which only register and unregister classes (still have no idea what this mean but I know it's necessary).

$\endgroup$
2

1 Answer 1

5
$\begingroup$

You can just add a property to your operator class.

This is done by inserting a line between the class declaration and the class methods. Both bl_idname and bl_label are properties, these are inherited from bpy.types.Operator.

So lets say you want to add a property to an operator that prints monkey names. The property will be called my_monkey:

class Monkey_OT_Operator(bpy.types.Operator): bl_idname = "wm.print_mokey_name" bl_label = "Print the name of my monkey" # 2.7x way to add a string property to hold the monkey name # my_monkey = bpy.props.StringProperty() # 2.8x way to add a string property my_monkey : bpy.props.StringProperty(name="My monkey name") def execute(self, context): print(self.my_monkey) return {'FINISHED'} 

Okay so this should seem pretty simple, we've added a "slot" into the monkey operator class that holds a string.

Now to use this in your panel layout you can just set the property as you would for any normal class, for this you need a reference to your operator:

 row = layout.row() op = row.operator('wm.print_mokey_name', text = "Print Monkey") op.my_monkey = 'Suzanne' 

Now when you print the button, it should print Suzanne.

Of course, you are not limited to string properties, you will find a more comprehensive use of properties in the Blender API documentation - Properties

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.