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()
@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