Adding menu entry in Python; script "works" but parameter meanings are unclear

Based on a previous forum post, and some web search, I now have a working script.
Here the post I used as example:

But there are multiple parameters in the script, whose meaning I do not understand. Here is the script:

import unreal

# Menu parameters
SRC_MENU = "LevelEditor.MainMenu"
SECTION_NAME = "PythonTools"
SUB_MENU_NAME = "Tools" 
SUB_MENU_LABEL = "PyTools"# Actual menu name in to toolbar

# Menu entry parameters
ENTRY_NAME = "Python.Tools"
ENTRY_LABEL = "YourMenuItemName" # Actual menu entry name
CUSTOM_TYPE = custom_type=unreal.Name("")
SCRIPT = "from foo import bar;bar.main()"

def main():
    menus = unreal.ToolMenus.get()
    # Find the 'Main' menu, this should not fail,
    # but if we're looking for a menu we're unsure about 'if not'
    # works as nullptr check,
    main_menu = menus.find_menu(SRC_MENU)
    if not main_menu:
        unreal.log_error("Failed to find the 'Main' menu. Something is wrong in the force!")
        unreal.log("Adding menu {} with label {} under section {} to menu {}".format(SUB_MENU_NAME, SUB_MENU_LABEL, SECTION_NAME, SRC_MENU))
        script_menu = main_menu.add_sub_menu(main_menu.get_name(), SECTION_NAME, SUB_MENU_NAME, SUB_MENU_LABEL)
        entry = unreal.ToolMenuEntry(
            # If you pass a type that is not supported Unreal will let you know,
            # this will tell unreal to insert this entry into the First spot of the menu
            insert_position=unreal.ToolMenuInsert("", unreal.ToolMenuInsertType.FIRST)
        # this is what gets executed on click
        entry.set_string_command(unreal.ToolMenuStringCommandType.PYTHON, CUSTOM_TYPE, string=(SCRIPT))
        # add our new entry to the new menu
        unreal.log("Adding menu entry {} under section {} to menu {}".format(ENTRY_NAME, ENTRY_SECTION_NAME, SUB_MENU_NAME))
        # refresh the UI

if __name__ == '__main__':

So, my question is, what is the purpose of those parameters, which I cannot “see” after running the script:


I would have at least expected, ENTRY_LABEL to be under a section called ENTRY_SECTION_NAME, but it’s not.

Some you can see, with setting Editor Preferences->General->Miscellaneous->Display UI Extension Points => True

Take a look at the code used to make the menu for setting Lighting Build Quality in Engine/Source/Editor/LevelEditor/Private/LevelEditorMenu.cpp

		static void RegisterLightingQualityMenu(const FName InBaseMenuName)
			UToolMenu* SubMenu = UToolMenus::Get()->RegisterMenu(UToolMenus::JoinMenuPaths(InBaseMenuName, "LightingQuality"));

				FToolMenuSection& Section = SubMenu->AddSection("LevelEditorBuildLightingQuality", LOCTEXT("LightingQualityHeading", "Quality Level"));

It creates a sub menu “LightingQuality”, then a section “LevelEditorBuildLightingQuality” and an entry for each command to set the quality which gets added to the section.

And you can see on this image section name, sub menu name etc are all there.

Most methods for extending and working with the menu will require that you provide these names and not the name of the label you see normally.

1 Like