2
$\begingroup$

I can add menus to Blender 2.8 with the python code below but how can I add a sub-menu using python to one of Blenders existing menus? I'm trying to add the rtMyMenu sub-menu under Blenders Add-Curve sub-menu (see red arrow).

What I currently have is:

-Add-
-rtMyMenu-  -rtMyOperator-

What I'm trying to get is:

-Add-
-Curve-
      -rtMyMenu-  -rtMyOperator-

-

import bpy

class OBJECT_OT_myoperator(bpy.types.Operator):
    bl_idname = 'object.myoperator'
    bl_label = 'rtMyOperator'
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        # ...affairs...
        return {'FINISHED'}

class OBJECT_MT_mymenu(bpy.types.Menu):
    bl_idname = 'object.mymenu'
    bl_label = 'rtMyMenu'

    def draw(self, context):
        layout = self.layout
        layout.operator(OBJECT_OT_myoperator.bl_idname)

def menu_func(self, context):
    self.layout.menu(OBJECT_MT_mymenu.bl_idname)

def register():
    bpy.utils.register_class(OBJECT_OT_myoperator)
    bpy.utils.register_class(OBJECT_MT_mymenu)
    bpy.types.VIEW3D_MT_add.append(menu_func)

def unregister():
    bpy.utils.unregister_class(OBJECT_OT_myooperator)
    bpy.utils.unregister_class(OBJECT_MT_mymenu)
    bpy.types.VIEW3D_MT_add.remove(menu_func)

if __name__ == "__main__":
    register()

I tried

bpy.types.VIEW3D_MT_add_curve.append(menu_func)         

and

bpy.types.VIEW3D_MT_add_curve.remove(menu_func)

But that doesn't work.

submenus

$\endgroup$
4
$\begingroup$

Minor issue: VIEW3D_MT_add_curve doesn't exist, you have to append to VIEW3D_MT_curve_add rather. Further reading: https://docs.blender.org/api/current/bpy.types.Menu.html

enter image description here

import bpy

class MY_MT_CustomMenu(bpy.types.Menu):
    bl_label = "First Menu"
    bl_idname = "OBJECT_MT_custom_menu"

    def draw(self, context):
        layout = self.layout
        layout.label(text="Hello First Menu!", icon='WORLD_DATA')

         # call the second custom menu
        layout.menu("OBJECT_MT_sub_menu", icon="COLLAPSEMENU")


class MY_MT_CustomSubMenu(bpy.types.Menu):
    bl_label = "Sub Menu"
    bl_idname = "OBJECT_MT_sub_menu"

    def draw(self, context):
        layout = self.layout
        layout.label(text="Hello Second Menu!", icon='WORLD_DATA')

        # call another predefined menu
        layout.operator("wm.call_menu", text="Unwrap").name = "VIEW3D_MT_uv_map" 


def draw_menu(self, context):
    self.layout.menu(MY_MT_CustomMenu.bl_idname)


def register():
    bpy.utils.register_class(MY_MT_CustomMenu)
    bpy.utils.register_class(MY_MT_CustomSubMenu)
    bpy.types.VIEW3D_MT_curve_add.append(draw_menu)

def unregister():
    bpy.types.VIEW3D_MT_curve_add.remove(draw_menu)
    bpy.utils.unregister_class(MY_MT_CustomSubMenu)
    bpy.utils.unregister_class(MY_MT_CustomMenu)    


if __name__ == "__main__":
    register()

Based on: How to create a custom UI? and Python: Extend “Select Similar” menu?

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks! That was a more clear example than I could find in the docs. $\endgroup$ – Jonathan L Mar 17 at 18:47
  • 1
    $\begingroup$ Yep, barely documented. For UI stuff, read How to create a custom UI?. Cheers @JonathanL $\endgroup$ – brockmann Mar 19 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.