Add a custom dropdown menu to the Unreal Level Editor Toolbar


Tue Nov 08 2022 14:06:11 GMT+0000 (UTC)

Saved by @MaVCArt #python

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
    # name
    # label
    'My Python Menu',
    # tooltip
    'This is a custom python menu - you can do cool stuff with it!',
    # command_type
    # 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
    # parent - we leave this blank in this case
    # menu type - we just want a normal menu
    # warn_if_already_registered - if False, this will just not do anything if it already exists.

# -- now we can create a menu entry. This can be done in a variety of ways.
menu_entry = unreal.ToolMenuEntry() = '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