Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unreal context menus #48

Merged
merged 22 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
52369f8
Added Context arg to the main node,
JLHayde Jul 17, 2023
c984a98
Merge branch 'hannesdelbeke:main' into unreal_context_menus
JLHayde Jul 17, 2023
a381a0e
updated sample text
JLHayde Jul 17, 2023
8829417
Removed context menu argument
JLHayde Jul 17, 2023
3f51e98
isolated context logic to just the unreal app
JLHayde Jul 17, 2023
74e01e7
updated string format
JLHayde Jul 17, 2023
cd5dc91
Set Section names on items and seperators
JLHayde Jul 17, 2023
a63c225
Set Section names on items and seperators
JLHayde Jul 17, 2023
d13314c
Merge remote-tracking branch 'origin/unreal_context_menus' into unrea…
JLHayde Jul 17, 2023
4084a61
did not mean to change that
JLHayde Jul 17, 2023
3e90aa1
updating sample
JLHayde Jul 20, 2023
707078a
Removed root_menu kwarg in favour of parent_path
JLHayde Jul 20, 2023
46730e4
Merge branch 'main' of github.com:JLHayde/unimenu into unreal_context…
JLHayde Jul 20, 2023
d66954f
section change
JLHayde Jul 24, 2023
5b6267c
Merge branch 'main' of github.com:JLHayde/unimenu into unreal_context…
JLHayde Jul 24, 2023
ceb18aa
reset test
JLHayde Jul 24, 2023
3c66b01
reset test
JLHayde Jul 24, 2023
33beb2a
kwargs can now override args for the unreal menus, items and separato…
JLHayde Jul 24, 2023
9134535
Merge branch 'main' of github.com:JLHayde/unimenu into unreal_context…
JLHayde Jul 24, 2023
a83e726
resized image
JLHayde Jul 24, 2023
152af6f
removed random line
JLHayde Jul 24, 2023
5bde8f3
adjusted the command to run from the samples package
JLHayde Jul 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added samples/menu_screen_unreal_context.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions samples/unreal_context_menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'''
Adds Context menus to assets in the browser.
to specify an asset context use the asset type as parent_path.

full list of ui names here https://dev.epicgames.com/community/snippets/exo/unreal-engine-editor-ui-menu-names

and the kwarg context_menu on the root noode

'''

import unreal
import unimenu


def test_function():
selected = unreal.EditorUtilityLibrary.get_selected_assets()
unreal.AssetEditorSubsystem().open_editor_for_assets(selected)


config = {
'label': 'StaticMesh Tools',
'kwargs': {
'root_menu': "ContentBrowser.AssetContextMenu.StaticMesh",
"menu_section": "GetAssetActions"
},

'items':

[
{
'command': 'test_function()',
'label': 'Open Selected Asset',
"icon": "MessageLog.TabIcon",
"tooltip": "tooltip",

},
{
'label': 'label',
'separator': True,
"icon": "BlueprintEditor.AddNewFunction",
"tooltip": "tooltip separator",

},
{
'command': 'print("hello 2")',
'label': 'tool2',
'icon': "BlueprintEditor.AddNewEventGraph"

},
{
'label': 'Tools',

'items':
[
{
'command': 'print("hello 1")',
'label': 'tool1',
'icon': "MessageLog.TabIcon"
},
{
'command': 'print("hello 2")',
'label': 'tool2',
'icon': "MessageLog.TabIcon"
}
]
}
]
}

# test teardown
node = unimenu.load(config)
app_node = node.setup(backlink=False)
48 changes: 37 additions & 11 deletions unimenu/apps/unreal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ class MenuNodeUnreal(MenuNodeAbstract):

@property
def _default_root_parent(self):
# Default menu root
parent_path = "LevelEditor.MainMenu"
hannesdelbeke marked this conversation as resolved.
Show resolved Hide resolved

# Optional root
if self.kwargs.get('root_menu'):
# full list of ui names here https://dev.epicgames.com/community/snippets/exo/unreal-engine-editor-ui-menu-names
parent_path = f"{self.kwargs['root_menu']}"

# Extend the root
if self.parent_path:
parent_path = f"LevelEditor.MainMenu.{self.parent_path}" # todo make this more flexible
else:
parent_path = "LevelEditor.MainMenu"
parent_path = f"{parent_path}.{self.parent_path}"

unreal_menus = unreal.ToolMenus.get()
parent_menu = unreal_menus.find_menu(parent_path)
return parent_menu
Expand All @@ -22,16 +30,28 @@ def setup(self, parent_app_node=None, backlink=True):
unreal_menus.refresh_all_widgets()

def _setup_sub_menu(self, parent_app_node=None) -> unreal.ToolMenu:
# Default Section
target_section_name = "PythonTools"

if self.kwargs.get('menu_section'):
hannesdelbeke marked this conversation as resolved.
Show resolved Hide resolved
# change target section
target_section_name = self.kwargs['menu_section']

return parent_app_node.add_sub_menu(
owner=parent_app_node.menu_name,
section_name="PythonTools",
section_name=target_section_name,
name=self.id, # todo check if needs to be unique like in add_to_menu
label=self.label, # todo add label support
tool_tip=self.tooltip
)

def _setup_menu_item(self, parent_app_node=None) -> unreal.ToolMenuEntry:
"""add a menu item to the script menu"""
target_section_name = "Scripts"
if self.kwargs.get('menu_section'):
# change target section
target_section_name = self.kwargs['menu_section']

entry = unreal.ToolMenuEntry(
name=self.id, # this needs to be unique! if not set, it's autogenerated
type=unreal.MultiBlockType.MENU_ENTRY,
Expand All @@ -48,18 +68,24 @@ def _setup_menu_item(self, parent_app_node=None) -> unreal.ToolMenuEntry:
if self.tooltip:
entry.set_tool_tip(self.tooltip)
if self.icon:
entry.set_icon(self.icon) # naive implementation todo improve
# it is not possible to register custom icons with Python, can only use the built-in icons
# Icon's are here ...\Engine\Content\Editor\Slate\Icons - look for the icon name here
# style_names can be found here ...\Engine\Source\Editor\EditorStyle\Private\SlateEditorStyle.cpp - look for the name here
# e.g. Set( "BlueprintEditor.AddNewMacroDeclaration", new IMAGE_BRUSH( "Icons/icon_Blueprint_AddMacro_40px", Icon40x40) );
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ <-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
entry.set_icon("EditorStyle", style_name=self.icon)

parent_app_node.add_menu_entry("Scripts", entry) # always returns None
parent_app_node.add_menu_entry(target_section_name, entry) # always returns None
return entry

def _setup_separator(self, parent_app_node=None):
# todo not working yet
"""add a separator to the script menu"""
# see https://docs.unrealengine.com/4.27/en-US/PythonAPI/class/ToolMenu.html
# todo what is diff with dynamic section?
return parent_app_node.add_section(section_name=self.label + "_section", label=self.label + "_label")
target_section_name = self.label + "_section"
if self.kwargs.get('menu_section'):
# change target section
target_section_name = self.kwargs['menu_section']
return parent_app_node.add_section(section_name=target_section_name, label=self.label)

def teardown(self):
"""remove from menu"""
raise NotImplementedError("not yet implemented")
raise NotImplementedError("not yet implemented")