Blender Naming Conventions to Write Clean and Consistent Scripts

Blender Naming Convention

Blender Python Coding Standards A Guide to Naming Conventions

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

  1. Use UPPERCASE letters.
  2. Use an underscore _ to separate words.
  3. Format: [SPACE OR SCREEN NAME in UPPERCASE] + _ + [PANEL(PT) or OPERATOR(OT) in UPPERCASE] + _ + [Your Operator or Panel name in CamelCase].

For Functions

  1. Function names should be written in snake_case (lowercase words separated by underscores).
    def record_macro_action(scene):
        pass
    • Avoid using camelCase or PascalCase for functions. These styles are typically reserved for class names.
  2. 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'}
  3. Panel Methods (draw): Fixed Names
    For custom panels (bpy.types.Panel), the draw method is fixed and must be named draw. 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")
  4. 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
  5. 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
  6. 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