Blender naming conventions
Blender tutorials
blender
Python scripting
Blender scripting
Blender scripting requires following specific naming conventions to maintain code clarity and consistency. Here’s a guide to help you keep your code in line with Blender’s best practices.
For Classes
- Use UPPERCASE letters.
- Use an underscore
_
to separate words. - Format:
[SPACE OR SCREEN NAME in UPPERCASE]
+_
+[PANEL(PT) or OPERATOR(OT) in UPPERCASE]
+_
+[Your Operator or Panel name in CamelCase]
.
For Functions
- Function names should be written in
snake_case
(lowercase words separated by underscores).def record_macro_action(scene): pass
- Avoid using
camelCase
orPascalCase
for functions. These styles are typically reserved for class names.
- Avoid using
- Operator Methods: Operator methods have specific names and purposes:
execute(self, context)
: Contains the main logic for the operator. It is called when the operator runs.invoke(self, context, event)
: Used when the operator is called interactively (e.g., from a button click).modal(self, context, event)
: Used for operators that run in a continuous loop, typically for interactive tools.draw(self, context)
: Defines how an operator's properties are displayed in the UI.
Example of an operator class:
class SimpleOperator(bpy.types.Operator): bl_idname = "object.simple_operator" bl_label = "Simple Operator" def execute(self, context): # Logic for the operator return {'FINISHED'}
- Panel Methods (
draw
): Fixed Names
For custom panels (bpy.types.Panel
), thedraw
method is fixed and must be nameddraw
. This method is responsible for adding UI elements (buttons, labels, etc.) to the panel.class VIEW3D_PT_MacroRecorder(bpy.types.Panel): bl_label = "Macro Recorder" bl_idname = "VIEW3D_PT_macro_recorder" bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_category = 'Macro Recorder' def draw(self, context): layout = self.layout layout.operator("wm.record_macro", text="Start/Stop Recording")
- Private Helper Functions: Prefix with an underscore.
If a function is meant to be a private helper (i.e., not intended for use outside the current module or class), prefix it with an underscore:def _get_selected_object(context): return context.active_object
- Function Names in Add-ons: Descriptive and Contextual.
Choose names that clearly describe the function's purpose. If the function is specific to a feature in the add-on, include that context in the name.def export_to_text_block(context): pass
- Event Handlers: Use Descriptive Names.
If you are defining functions to be used as handlers (e.g.,depsgraph_update_post
), use descriptive names to indicate their role:def record_macro(scene): pass