This script is a simple menu, menus differ from panels in that they must reference from a header, panel or another menu.
Notice the ‘CATEGORY_MT_name’ Menu.bl_idname, this is a naming convention for menus.
Note
Menu subclasses must be registered before referencing them from blender.
Note
Menu’s have their Layout.operator_context initialized as ‘EXEC_REGION_WIN’ rather then ‘INVOKE_DEFAULT’, so 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")
This menu demonstrates some different functions.
import bpy
class SubMenu(bpy.types.Menu):
bl_idname = "OBJECT_MT_select_submenu"
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")
# access this operator as a submenu
layout.operator_menu_enum("object.select_by_type", "type", text="Select All by Type...")
layout.separator()
# expand each operator option into this menu
layout.operator_enum("object.lamp_add", "type")
layout.separator()
# use existing memu
layout.menu("VIEW3D_MT_transform")
bpy.utils.register_class(SubMenu)
# test call to display immediately.
bpy.ops.wm.call_menu(name="OBJECT_MT_select_submenu")
When creating menus for addons you can’t reference menus in blenders 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)
base class — bpy_struct
Editor menu containing buttons
Type : | string, default “” |
---|
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) |
---|
The menu label
Type : | string, default “”, (never None) |
---|
If this method returns a non-null output, then the menu can be drawn
Return type: | boolean |
---|
Draw UI elements into the menu UI layout
Define these on the subclass - preset_operator - preset_subdir
Inherited Properties
Inherited Functions
Enter search terms or a module, class or function name.