I have made a custom panel in Blender and I would like to have 3 buttons. Currently I've taken the render button and just copied and pasted that into my panel and changed the name of it just for a place holder. How can I make my own button, like how I would make my own checkbox etc. Also, it's currently one of these kinds of buttons:
-
1$\begingroup$ see this question and answer: blender.stackexchange.com/questions/43593/… $\endgroup$– zeffiiDec 27, 2015 at 8:16
1 Answer
Panels can't have their own properties defined inside the Panel's class (by design), this means you must register properties either as part of the Scene or as part of the Object class, or as part of a higher level PropertyGroup. A group keeps your properties organized and avoids potential naming conflicts between other add-ons.
Here's a small snippet that creates a Panel, and registers a property group with as many properties as you might need. The update function called by the enum is where you could execute code.
This is one way to do that, scavenge what you need from this:
import bpy
def update_after_enum(self, context):
print('self.my_enum ---->', self.my_enum)
class IgnitProperties(bpy.types.PropertyGroup):
my_enum = bpy.props.EnumProperty(
name = "My options",
description = "My enum description",
items = [
("Silver" , "Silver" , "Description..."),
("Gold", "Gold", "other description"),
("Space Grey", "Space Grey", "Some other description")
],
update=update_after_enum
)
# my_string = bpy.props.StringProperty()
# my_integer = bpy.props.IntProperty()
class IGLayoutDemoPanel(bpy.types.Panel):
"""Creates a Panel in the scene context of the properties editor"""
bl_label = "IG Layout Demo"
bl_idname = "SCENE_PT_layout"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"
def draw(self, context):
layout = self.layout
scene = context.scene
row = layout.row()
row.prop(scene.ignit_panel, "my_enum", expand=True)
def register():
bpy.utils.register_module(__name__)
bpy.types.Scene.ignit_panel = bpy.props.PointerProperty(type=IgnitProperties)
def unregister():
bpy.utils.unregister_module(__name__)
del bpy.types.Scene.ignit_panel
if __name__ == "__main__":
register()
If you want bigger buttons a different approach is needed.
-
$\begingroup$ Wonderful answer! Are there any guidelines for the bl_idname of the Panel? The official documentation about bl_idname there reads a bit like an unfinished sentence and didn't really answer it for me. $\endgroup$– RayFeb 17, 2016 at 17:58