Unreal Python create a custom menu entry script object

PHOTO EMBED

Thu Nov 10 2022 23:12:37 GMT+0000 (Coordinated Universal Time)

Saved by @MaVCArt #python

import unreal
from .constants import tool_menus
from .menu_entry import CustomScriptMenuEntry


# ----------------------------------------------------------------------------------------------------------------------
def get_custom_menu():
    # -- first let's acquire a handle on the new menu we created in the last section.
    return tool_menus.find_menu('LevelEditor.LevelEditorToolbar.MyPythonMenu')


# ----------------------------------------------------------------------------------------------------------------------
def setup_menu():
    # -- 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.
    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
    )

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


# ----------------------------------------------------------------------------------------------------------------------
def setup_menu_entry():
    entry = CustomScriptMenuEntry()
    entry.name = 'CustomScriptMenuEntry'
    get_custom_menu().add_menu_entry_object(entry)
    tool_menus.refresh_all_widgets()
content_copyCOPY