Creating a Blender addon involves writing Python scripts that define how the addon behaves. The N-panel, or the sidebar in the 3D Viewport, can be customized with your own panels through an addon. Below is a basic example of a Blender addon that adds a panel to the N-panel.
import bpy
# Define the panel class
class SimpleCustomPanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Simple Custom Panel"
bl_idname = "OBJECT_PT_simple_custom_panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'My Addon'
def draw(self, context):
layout = self.layout
obj = context.object
# Add a label and a simple button to the panel
layout.label(text="Hello World")
layout.label(text="Active object is: " + obj.name)
layout.operator("mesh.primitive_cube_add")
# Define the operator class (optional if you want to add functionality)
class SimpleCustomOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"
def execute(self, context):
# Add your operator's functionality here
self.report({'INFO'}, "Simple Operator Executed")
return {'FINISHED'}
# Register classes to make them active
def register():
bpy.utils.register_class(SimpleCustomPanel)
bpy.utils.register_class(SimpleCustomOperator)
def unregister():
bpy.utils.unregister_class(SimpleCustomPanel)
bpy.utils.unregister_class(SimpleCustomOperator)
# This allows you to run the script directly from Blender's text editor
# to test the addon without installing it.
if __name__ == "__main__":
register()
Here's a step-by-step guide to what the code above does:
- We define a custom panel class (
SimpleCustomPanel) that inherits from bpy.types.Panel.
- In the panel class, we set various Blender UI properties like
bl_label, bl_idname, bl_space_type, bl_region_type, and bl_category.
- The
draw method of the panel class defines what the panel will contain. Here, we have added a label and an operator button that, when clicked, will add a cube to the scene.
- We define an operator class (
SimpleCustomOperator) that inherits from bpy.types.Operator.