Creating a Custom Menu with Python Scripting


View the Lesson

There are so many different tools, menus, and options in Blender that it is impossible to perfectly fit everyone’s ideal workflow. Some people like big menus, some people prefer hotkeys, and others still want only iconized toolbars. Blender will never be exactly like we each want it, but luckily this is where Python comes in!

Blender’s Python integration allows us to create our own menus, toolbar panels, and other things by writing simple scripts. We can then install these scripts as add-ons and adapt our workflow how we choose.

In this tutorial you’ll learn how to write a simple add-on that creates a custom menu. After writing the script you’ll be able to easily customize the menu by filling it with almost anything you like.

bl_info = {
        "name": "My Custom Menu",
        "category": "3D View",
        "author": "Jonathan Williamson"
        }        

import bpy

# Creates a menu for global 3D View
class customMenu(bpy.types.Menu):
    bl_label = "Custom Menu"
    bl_idname = "view3D.custom_menu"

    # Set the menu operators and draw functions
    def draw(self, context):
        layout = self.layout

        layout.operator("mesh.primitive_cube_add")
        layout.operator("object.duplicate_move")   

def register():
    bpy.utils.register_class(customMenu)
#    bpy.ops.wm.call_menu(name=customMenu.bl_idname)

def unregister():
    bpy.utils.unregister_class(customMenu)

if __name__ == "__main__":     register()

You can also find the code, along with all our other scripts on the CG Cookie Github Repository

View the Lesson

thanks man. I’ll have to try this out. I’ve been wanting to learn more of python with Blender.

By the way, is Python anything like Java? I have an intermediate knowledge of Java.

Wait something like the codeacademy exists? Get out. Might have to get back into coding. Thanks for the tutorial.

I would be greateful for more videotutorials about programming for blender. I don’t mean Python’s tutorials, beacuse they are easy to find, I mean the explanation of how it works in Blender. There are plenty of tutorials about modelling or basics, but programming is neglected.

Since google keeps making this thread show up when I’m hunting info,

Here’s an example of how to put a menu item in the 3D Viewport’s menu in blender 2.80 that can be toggled with a hotkey. I like alt+a.

bl_info = {
        'name': 'AimAtSelected',
        'author': 'bay raitt',
        'version': (0, 1),
        'blender': (2, 80, 0),
        'category': 'View',
        'location': 'View > Aim at selected',
        'wiki_url': ''}

import bpy


#store a location variable outside of any def so it can be accessed globally.
isPivotCursor = True

# define the code to run on click here
def main(context):

    global isPivotCursor
    
    if not isPivotCursor:
        bpy.ops.view3d.snap_cursor_to_center()
  
    if isPivotCursor:    
        context.scene.tool_settings.transform_pivot_point = 'CURSOR'
        bpy.ops.view3d.snap_cursor_to_selected()
        bpy.ops.view3d.view_center_cursor()
        
    isPivotCursor = not isPivotCursor
                
# menu button
class BR_OT_aim_at_selected(bpy.types.Operator):
    """Aim at Selected"""
    bl_idname = "view3d.aim_at_selected"
    bl_label = "Aim Selected"

    def execute(self, context):
        main(context)
        return {'FINISHED'}


# insert the button into blender's user interface:
def menu_draw(self, context):
    self.layout.operator(BR_OT_aim_at_selected.bl_idname)

def register():
    bpy.utils.register_class(BR_OT_aim_at_selected)
    bpy.types.VIEW3D_MT_view.prepend(menu_draw)  

def unregister():
    bpy.utils.unregister_class(BR_OT_aim_at_selected)
    bpy.types.VIEW3D_MT_view.remove(menu_draw)  

    if __name__ != "__main__":
        bpy.types.VIEW3D_MT_view.remove(menu_draw)

if __name__ == "__main__":
    register()