> For the complete documentation index, see [llms.txt](https://bkortbus.gitbook.io/unreal-python-recipe-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bkortbus.gitbook.io/unreal-python-recipe-book/editor-menus/python-menu-class/insert-position.md).

# 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:

```python
@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](https://dev.epicgames.com/documentation/en-us/unreal-engine/python-api/class/ToolMenuInsertType), which is how we want our new menu inserted in relation to the given entry's name

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

{% hint style="danger" %} <mark style="color:yellow;">Note</mark>: While the `section` is not required on the insert policy, we will need to make sure we're adding our Menu to the same section as the existing Menu Entry.&#x20;
{% endhint %}

## Run it!

In this example, we're adding our menu to the Editor's <mark style="color:green;">Edit</mark> dropdown menu AFTER the existing <mark style="color:green;">Paste</mark> menu option:

```python
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:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FiqVX96d6ZoJa3x1CmtsL%2Fimage.png?alt=media&amp;token=ea4320ae-15d6-4259-b781-3dc7e06a04f2" alt="" width="397"><figcaption></figcaption></figure>
