Insert Position

Controlling where a Menu Entry is inserted

Menus are added to the bottom of the Menu list when created by default. With a slight change, it is possible to control where our menus are inserted. This can be especially helpful when adding menu entries to existing menus.

The New __init__

We'll need to change our __init__ function in order to take advantage of the insert position:

@unreal.uclass()
class PythonMenuTool(unreal.ToolMenuEntryScript):
    name = "inserted_menu"
    label = "Inserted Menu Class"
    tool_tip = "tool tip!"

    def __init__(self, menu, section="", insert_policy=None):
        """Initialize our entry for the given menu_object's section"""
        super().__init__()

        # Initialize the entry data
        self.init_entry(
            owner_name="custom_owner",
            menu=menu.menu_name,
            section=section,
            name=self.name,
            label=self.label,
            tool_tip=self.tool_tip
        )

        # if an insert policy was provided
        if insert_policy:
            # Build the entry insert object
            entry = unreal.ToolMenuEntry(
                name=self.name,
                type=self.data.advanced.entry_type,
                owner=unreal.ToolMenuOwner("custom_owner"),
                insert_position=insert_policy,
                script_object=self
            )

            # insert policy method
            menu.add_menu_entry(section, entry)

        else:
            # default method - add at the bottom of the menu
            menu.add_menu_entry_object(self)

The big change here is that if an insert_policy is provided, we need to use a slightly different method to insert the menu in our desired location

Creating the Insert Position Object

The Insert Position class is a bit basic, we just need to tell it:

  • An existing Menu's name that we want this insert to be in reference of

  • The Insert Type, which is how we want our new menu inserted in relation to the given entry's name

insert_policy = unreal.ToolMenuInsert("ExistingMenu", unreal.ToolMenuInsertType.AFTER)

Run it!

In this example, we're adding our menu to the Editor's Edit dropdown menu AFTER the existing Paste menu option:

ToolMenus = unreal.ToolMenus.get()
edit_menu = ToolMenus.find_menu("LevelEditor.MainMenu.Edit")

policy = unreal.ToolMenuInsert("Paste", unreal.ToolMenuInsertType.AFTER)

PythonMenuTool(edit_menu, "EditMain", insert_policy = policy)

The results:

Last updated