Menu(bpy_struct)

Basic Menu Example

Here is an example of a simple menu. Menus differ from panels in that they must reference from a header, panel or another menu.

Notice the ‘CATEGORY_MT_name’ in Menu.bl_idname, this is a naming convention for menus.

Note

Menu subclasses must be registered before referencing them from blender.

Note

Menus have their Layout.operator_context initialized as ‘EXEC_REGION_WIN’ rather than ‘INVOKE_DEFAULT’ (see Execution Context). If the operator context needs to initialize inputs from the Operator.invoke function, then this needs to be explicitly set.

import bpy


class BasicMenu(bpy.types.Menu):
    bl_idname = "OBJECT_MT_select_test"
    bl_label = "Select"

    def draw(self, context):
        layout = self.layout

        layout.operator("object.select_all", text="Select/Deselect All").action = 'TOGGLE'
        layout.operator("object.select_all", text="Inverse").action = 'INVERT'
        layout.operator("object.select_random", text="Random")


bpy.utils.register_class(BasicMenu)

# test call to display immediately.
bpy.ops.wm.call_menu(name="OBJECT_MT_select_test")

Extending Menus

When creating menus for addons you can’t reference menus in Blender’s default scripts. Instead, the addon can add menu items to existing menus.

The function menu_draw acts like Menu.draw.

import bpy


def menu_draw(self, context):
    self.layout.operator("wm.save_homefile")

bpy.types.INFO_MT_file.append(menu_draw)

Preset Menus

Preset menus are simply a convention that uses a menu sub-class to perform the common task of managing presets.

This example shows how you can add a preset menu.

We will use the camera in this example.

import bpy
from bpy.types import Operator, Menu
from bl_operators.presets import AddPresetBase


class OBJECT_MT_draw_presets(Menu):
    bl_label = "Object Draw Presets"
    preset_subdir = "object/draw"
    preset_operator = "script.execute_preset"
    draw = Menu.draw_preset


class AddPresetObjectDraw(AddPresetBase, Operator):
    '''Add a Object Draw Preset'''
    bl_idname = "camera.object_draw_preset_add"
    bl_label = "Add Object Draw Preset"
    preset_menu = "OBJECT_MT_draw_presets"

    # variable used for all preset values
    preset_defines = [
        "obj = bpy.context.object"
    ]

    # properties to store in the preset
    preset_values = [
        "obj.draw_type",
        "obj.show_bounds",
        "obj.draw_bounds_type",
        "obj.show_name",
        "obj.show_axis",
        "obj.show_wire",
    ]

    # where to store the preset
    preset_subdir = "object/draw"


# Draw into an existing panel
def panel_func(self, context):
    layout = self.layout

    row = layout.row(align=True)
    row.menu(OBJECT_MT_draw_presets.__name__, text=OBJECT_MT_draw_presets.bl_label)
    row.operator(AddPresetObjectDraw.bl_idname, text="", icon='ZOOMIN')
    row.operator(AddPresetObjectDraw.bl_idname, text="", icon='ZOOMOUT').remove_active = True


classes = (
    OBJECT_MT_draw_presets,
    AddPresetObjectDraw,
    )


def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.types.OBJECT_PT_display.prepend(panel_func)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    bpy.types.OBJECT_PT_display.remove(panel_func)


if __name__ == "__main__":
    register()

base class — bpy_struct

subclasses — GPENCIL_PIE_sculpt, GPENCIL_PIE_settings_palette, GPENCIL_PIE_tool_palette, GPENCIL_PIE_tools_more

class bpy.types.Menu(bpy_struct)

Editor menu containing buttons

bl_description
Type:string, default “”
bl_idname

If this is set, the menu gets a custom ID, otherwise it takes the name of the class used to define the menu (for example, if the class name is “OBJECT_MT_hello”, and bl_idname is not set by the script, then bl_idname = “OBJECT_MT_hello”)

Type:string, default “”, (never None)
bl_label

The menu label

Type:string, default “”, (never None)
bl_translation_context
Type:string, default “*”, (never None)
layout

Defines the structure of the menu in the UI

Type:UILayout, (readonly)
classmethod poll(context)

If this method returns a non-null output, then the menu can be drawn

Return type:boolean
draw(context)

Draw UI elements into the menu UI layout

draw_preset(context)

Define these on the subclass: - preset_operator (string) - preset_subdir (string)

Optionally: - preset_extensions (set of strings) - preset_operator_defaults (dict of keyword args)

path_menu(searchpaths, operator, props_default=None, filter_ext=None)

Inherited Properties

Inherited Functions