Skip to content

Instantly share code, notes, and snippets.

@rondreas
Created April 1, 2021 08:08
Show Gist options
  • Save rondreas/eeecb0fcf52f05e898e0e31b22658068 to your computer and use it in GitHub Desktop.
Save rondreas/eeecb0fcf52f05e898e0e31b22658068 to your computer and use it in GitHub Desktop.
Example of extending a menu in Unreal using Python
"""
Example of extending a menu in Unreal using Python
"""
import unreal
def main():
menus = unreal.ToolMenus.get()
# Find the 'edit' menu, this should not fail,
# but if we're looking for a menu we're unsure about 'if not'
# works as nullptr check,
edit = menus.find_menu("LevelEditor.MainMenu.Edit")
main_menu = menus.find_menu("LevelEditor.MainMenu")
if not edit:
print("Failed to find the 'Edit' menu")
entry = unreal.ToolMenuEntry(
name = "MyEntry",
type = unreal.MultiBlockType.MENU_ENTRY, # If you pass a type that is not supported Unreal will let you know,
insert_position = unreal.ToolMenuInsert("Delete", unreal.ToolMenuInsertType.AFTER) # this will tell unreal to insert this entry after the 'Delete' menu item, if found in section we define later,
)
entry.set_label("My Entry")
entry.set_tool_tip("This is a Tool Menu Entry made in Python!")
edit.add_menu_entry("EditMain", entry) # section name, ToolMenuInsert, Unreal will add the section name if it can't find it, otherwise call AddEntry for the found section,
my_menu = main_menu.add_sub_menu("My.Menu", "Python", "My Menu", "This is a submenu")
for name in ["Foo", "Bar", "Baz"]:
e = unreal.ToolMenuEntry(
name = name,
type = unreal.MultiBlockType.MENU_ENTRY, # If you pass a type that is not supported Unreal will let you know,
)
e.set_label(name)
my_menu.add_menu_entry("Items", e)
menus.refresh_all_widgets()
if __name__ == '__main__':
main()
@pineappleSpicy
Copy link

Thx, useful.

@wywarren
Copy link

Is there a way to get the list of all menu identifiers? I've enabled the Display UI Extension Points in Editor Preferences but it doesn't show the fully qualified menu identifier. For example I'm trying to get the identifier for the content browser context menu when you press the add button on content browser or right click a folder in the content browser.

@rondreas
Copy link
Author

@wywarren I think that menu is created each time you spawn the context menu, setting a breakpoint in ..Engine/Plugins/Editor/ContentBrowser/ContentBrowserAssetDataSource/Source/ContentBrowserAssetDataSource/Private/NewAssetContextMenu.cpp and looking where any of the section ContentBrowserNewBasicAsset gets added, the UToolMenu is temp with no name etc

contextbrowser

So pretty sure you need to edit the C++ to extent the context menu. Or somehow catch and add to the UToolMenu when it spawns.

@AnetteZellmoser
Copy link

hi,
im super new to python in unreal. can you give an example on how to run a python script when clicking on a custom menu entry?

@rondreas
Copy link
Author

rondreas commented Jan 3, 2023

@AnetteZellmoser was a while since I looked at it so hope it's not outdated. You could create a ToolMenuEntryScript like

import unreal

@unreal.uclass()
class MyEntryScript(unreal.ToolMenuEntryScript):
    @unreal.ufunction(override=True)
    def execute(self, context):
        print("Hello, World!")

Then in the gist above add it as an entry to a tool menu like

...
entry = unreal.ToolMenuEntry(
    name = "Hello",
    type = unreal.MultiBlockType.MENU_ENTRY,
    script_object = MyEntryScript()
)
...

@AnetteZellmoser
Copy link

thanks alot!
do you also know how to add a shortcut to a menu entry?

@AnetteZellmoser
Copy link

Sorry i have another question... so the first part would be a sperate .py ?

@rondreas
Copy link
Author

rondreas commented Jan 4, 2023

do you also know how to add a shortcut to a menu entry?

Off the top of my head, no sorry.

so the first part would be a sperate .py ?

If you wanted to sure, but there is nothing in python where you're forced to move classes into separate modules.

@AnetteZellmoser
Copy link

Thanks alot! you really helped me alot. I ran into another problem. When i add the script_object to the ToolMenuEntry the label and tool tip is not visible anymore

@rondreas
Copy link
Author

rondreas commented Jan 5, 2023

You will need to override and implement those methods, https://docs.unrealengine.com/4.26/en-US/PythonAPI/class/ToolMenuEntryScript.html

@AnetteZellmoser
Copy link

sorry i hope im not bothering you but could you give me an example on how to do that?

@rondreas
Copy link
Author

rondreas commented Jan 5, 2024

its not work with unreal 5.2 startup python file att all he cant find the Menus

You likely want extent menu when running on start up @amrSherifSrour, I made a post where I extend the menu on start up also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment