Skip to content

Unreal add menu

When using Python in Unreal, you can add a python command to the menu.

It’s a good practice to let the user launch your tool from the Menu.
If your command launches a window, avoid making a new menu group and place it under
Tools or Windows. If you have a collection of tools, consider making a submenu group instead of a new Menu in the main menu bar. e.g. `Tools/My Collection/The Tool

Extracted these functions from [[unimenu]], see source
You can just copy paste these in your tool to keep it small and contained, for more advanced menus you can use [[unimenu]]

def default_root_parent():
    parent_path = "LevelEditor.MainMenu"
    unreal_menus = unreal.ToolMenus.get()
    parent_menu = unreal_menus.find_menu(parent_path)
    return parent_menu

def add_cmd_to_menu(label=None, command=None, tooltip=None, icon=None):
    # name kwargs needs to be unique! if not set, it's autogenerated
    entry = unreal.ToolMenuEntry(
                type=unreal.MultiBlockType.MENU_ENTRY,
                insert_position=unreal.ToolMenuInsert("", unreal.ToolMenuInsertType.FIRST),
            )
    if label:
        entry.set_label(label)
    if command:
        entry.set_string_command(
            type=unreal.ToolMenuStringCommandType.PYTHON,
            string=command,
            custom_type=unreal.Name("_placeholder_"),
        )
    if tooltip:
        entry.set_tool_tip(tooltip)
    if icon:
        entry.set_icon(icon)

    default_root_parent().add_menu_entry("Scripts", entry)

to add to the edit menu

parent_path = menus.find_menu("LevelEditor.MainMenu.Edit")

this is similar to add button to unreal toolbar

used in Unreal python template Unreal python plugin template