Preview:
import unreal

# -- first let's acquire a global instance we'll need - ToolMenus
tool_menus = unreal.ToolMenus.get()

# -- now we need a handle on the main level editor toolbar instance.
# -- we can use ToolMenus' "find_menu" function for this.
toolbar = tool_menus.find_menu('LevelEditor.LevelEditorToolbar')

# -- now let's create a new menu entry
entry = unreal.ToolMenuEntryExtensions.init_menu_entry(
    # owner
    toolbar.menu_name,
    # name
    'MyPythonMenu',
    # label
    'My Python Menu',
    # tooltip
    'This is a custom python menu - you can do cool stuff with it!',
    # command_type
    unreal.ToolMenuStringCommandType.COMMAND,
    # custom command type - we don't need to use this
    '',
    # command string - normally this would be a python command, but we don't need this.
    ''
)

# -- now that we've created the entry, we can tell unreal it's supposed to be a combo button
entry.type = unreal.MultiBlockType.TOOL_BAR_COMBO_BUTTON

# -- and now we can add the button to the toolbar
toolbar.add_menu_entry('PythonExtensions', entry)

# -- now we tell unreal that the button we just created is ACTUALLY a menu.
# -- this makes it so that when pressed, unreal does not invoke the button's "actions",
# -- but instead creates a context menu we can populate.
# -- calling this "register_menu" function helpfully also returns a handle on the menu object itself.
# -- Note how we have to register the button by its Name property.
sub_menu = tool_menus.register_menu(
    # the menu name -- this MUST MATCH the name of the combo button we gave
    'LevelEditor.LevelEditorToolBar.MyPythonMenu',
    # parent - we leave this blank in this case
    '',
    # menu type - we just want a normal menu
    unreal.MultiBoxType.MENU,
    # warn_if_already_registered - if False, this will just not do anything if it already exists.
    False
)

# -- now we can create a menu entry. This can be done in a variety of ways.
menu_entry = unreal.ToolMenuEntry()
menu_entry.name = 'MyCustomOption'
menu_entry.set_label('My Custom Option')
menu_entry.type = unreal.MultiBlockType.MENU_ENTRY

sub_menu.add_menu_entry('My Custom Section', menu_entry)

# -- as a final step we need to tell unreal to refresh all menus
tool_menus.refresh_all_widgets()
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter