How to Create a Custom UI Panel in Blender Python
Creating a custom user interface panel in Blender using Python allows
you to streamline workflows, build custom toolsets, and easily access
frequently used operations. This guide walks you through the
fundamentals of using Blender's bpy module to define a
panel class, assign it to a specific workspace region, design its
interface layout with buttons and properties, and register it within
Blender's ecosystem.
1. Open the Scripting Workspace
- Open Blender.
- Select the Scripting tab from the top workspace menu.
- In the Text Editor window, click New to create a new Python script file.
2. Understand the Panel Class Structure
To create a UI panel, you must define a class that inherits from
bpy.types.Panel. This class requires several predefined
attributes that instruct Blender where and how to display the panel:
bl_label: The title displayed at the top of the panel.bl_idname: A unique identifier for your panel class (conventionally formatted asCATEGORY_PT_name).bl_space_type: The editor window where the panel appears (e.g.,'VIEW_3D').bl_region_type: The specific area inside the editor (e.g.,'UI'for the sidebar).bl_category: The tab name in the sidebar where your panel will live.
3. The draw Method
The draw(self, context) method defines the contents of
the panel. You use the self.layout object to add UI
elements such as rows, columns, labels, operators (buttons), and
property fields.
4. Complete Code Example
Paste the following code into Blender's Text Editor:
import bpy
class VIEW3D_PT_custom_panel(bpy.types.Panel):
# Panel metadata
bl_label = "My Custom Tools"
bl_idname = "VIEW3D_PT_custom_panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Custom Tab'
def draw(self, context):
layout = self.layout
scene = context.scene
# Add a text label
layout.label(text="Object Operations:")
# Add a button (operator)
row = layout.row()
row.operator("mesh.primitive_cube_add", text="Add Cube", icon='CUBE')
# Add another button
row = layout.row()
row.operator("object.shade_smooth", text="Shade Smooth")
# Display a scene property
layout.separator()
layout.label(text="Transform:")
if context.object:
layout.prop(context.object, "location")
else:
layout.label(text="No object selected", icon='ERROR')
# Registration functions
def register():
bpy.utils.register_class(VIEW3D_PT_custom_panel)
def unregister():
bpy.utils.unregister_class(VIEW3D_PT_custom_panel)
if __name__ == "__main__":
register()5. Run and View the Panel
- Click the Run Script button (the play icon) in the top-right of the Text Editor, or press Alt + P.
- Navigate to the 3D Viewport.
- Press N on your keyboard to open the Sidebar.
- Locate the tab named Custom Tab.
- Your custom panel containing the label, buttons, and transform fields will be visible and interactive.