Today, I will

[Unreal Python] How to find references to UI form python 본문

Unreal

[Unreal Python] How to find references to UI form python

Lv.Forest 2024. 3. 20. 11:33

학습 출처 : https://youtu.be/KLExEMFRk00?feature=shared

*본 포스팅은 위의 영상을 따라 학습한 뒤 기록한 것입니다.

LogPython: menus = unreal.ToolMenus.get()
LogPython: edit_menu = menus.find_menu("LevelEditor.MainMenu.Edit")
LogPython: Error: SyntaxError: invalid syntax (<string>, line 1)
LogPython: print(edit_menu)
LogPython: <Object '/Engine/Transient.ToolMenus_0:RegisteredMenu_56' (0x000006361E979740) Class 'ToolMenu'>

import unreal

def lists_menu(num=1000):
    menu_list = set()
    for i in range(num):
        obj = unreal.find_object(None, f"/Engine/Transient.ToolMenus_0:RegisteredMenu_{i}") #for backward compatibility
        if not obj:
            continue
        menu_name = str(obj.menu_name)
        if menu_name == "None":
            continue
        menu_list.add(menu_name)
    return list(menu_list)

print(lists_menu())


LogPython: def lists_menu(num=1000):
    menu_list = set()
    for i in range(num):
        obj = unreal.find_object(None, f"/Engine/Transient.ToolMenus_0:RegisteredMenu_{i}") #for backward compatibility
        if not obj:
            continue
        menu_name = str(obj.menu_name)
        if menu_name == "None":
            continue
        menu_list.add(menu_name)
    return list(menu_list)
print(lists_menu())


LogPython: ['ContentBrowser.AssetContextMenu.MetaSoundPatch', 'LevelEditor.ActorContextMenu.LevelSubMenu', 'ControlRigEditor.RigHierarchy.ContextMenu.Naming', 'ContentBrowser.AssetContextMenu.AimOffsetBlendSpace', 'LevelEditor.LevelEditorToolBar.LevelToolbarQuickSettings', 'LevelEditor.MainMenu', 'LevelEditor.LevelEditorToolBar.PlayToolBar', 'AssetEditor.AnimationBlueprintEditor.MainMenu.Tools', 'LevelEditor.MainMenu.Build.LightingInfo.LightingResolution', 'LevelEditor.StatusBar.ToolBar.CompileComboButton', 'MainFrame.MainMenu.Tools', 'LevelEditor.MainMenu.Help', 'MediaPlayer.AssetPickerAssetC
ontextMenu', 'LevelEditor.LevelEditorToolBar.OpenBlueprint', 'ContentBrowser.AssetContextMenu.Skeleton.CreateSkeletalMeshSubmenu', 'LevelEditor.LevelViewportToolBar.Options', 'ContentBrowser.FolderContextMenu', 'ContentBrowser.AssetContextMenu.LevelSequence', 'ControlRigEditor.RigHierarchy.DragDropMenu.Align', 'ContentBrowser.AssetContextMenu.AnimSequence', 'LevelEditor.MainMenu.Build.LightingQuality', 'LevelEditor.StatusBar.ToolBar', 'AssetEditor.SkeletalMeshEditor.ToolBar', 'ContentBrowser.AssetContextMenu.SkeletalMesh', 'ContentBrowser.AssetContextMenu.World', 'ContentBrowser.AssetContextMe
nu.BlendSpace', 'MainFrame.NomadMainMenu', 'MainFrame.MainMenu.Edit', 'LevelEditor.LevelEditorToolBar.AssetsToolBar', 'ContentBrowser.AssetContextMenu.AnimMontage', 'LevelEditor.LevelEditorSceneOutliner.ContextMenu', 'ContentBrowser.AssetContextMenu.SkeletalMesh.CreateSkeletalMeshSubmenu', 'MainFrame.MainMenu.Asset', 'ContentBrowser.AssetViewOptions', 'ContentBrowser.AssetContextMenu.SoundWave', 'LevelEditor.LevelEditorToolBar.SettingsToolbar', 'LevelEditor.LevelEditorToolBar.User', 'LevelEditor.LevelEditorToolBar.Cinematics', 'ControlRigEditor.RigHierarchy.ContextMenu', 'ContentBrowser.AssetC
ontextMenu.MetaSoundSource', 'MainFrame.MainMenu', 'MainFrame.MainMenu.Help', 'LevelEditor.LevelEditorToolBar.AddQuickMenu', 'LevelEditor.ActorContextMenu', 'AssetEditor.SkeletonEditor.ToolBar', 'LevelEditor.ActorContextMenu.AssetToolsSubMenu', 'LevelEditor.MainMenu.Tools', 'SourceControl.ChangelistContextMenu', 'ContentBrowser.AssetContextMenu.ImgMediaSource', 'ContentBrowser.AssetContextMenu', 'ContentBrowser.DragDropContextMenu', 'AssetEditor.AnimationEditor.ToolBar', 'LevelEditor.LevelEditorToolBar.ModesToolBar', 'ContentBrowser.AssetContextMenu.AssetActionsSubMenu', 'LevelEditor.InViewpor
tPanel', 'LevelEditor.LevelEditorSceneOutliner.ContextMenu.LevelSubMenu', 'MainFrame.MainMenu.Window', 'VrEditor.ToggleVrOptions', 'LevelEditor.MainMenu.Build.LightingInfo.LightingDensity', 'ContentBrowser.AssetContextMenu.PoseAsset', 'ControlRigEditor.RigHierarchy.DragDropMenu.Align.Translation', 'LevelEditor.MainMenu.Edit', 'ContentBrowser.AddNewContextMenu', 'LevelEditor.MainMenu.Build.LightingInfo', 'ControlRigEditor.RigHierarchy.ContextMenu.New', 'ContentBrowser.AssetContextMenu.StaticMesh', 'EditorSettingsViewer.LevelEditorPlaySettings', 'LevelEditor.MainMenu.Build', 'ContentBrowser.Asse
tContextMenu.BlendSpace1D', 'MainFrame.MainMenu.File', 'ContentBrowser.AssetContextMenu.AnimBlueprint', 'LevelEditor.LevelViewportToolBar.View', 'LevelEditor.MainMenu.Window', 'ContentBrowser.AssetViewOptions.PathViewFilters', 'LevelEditor.MainMenu.File', 'ContentBrowser.ToolBar', 'MainFrame.MainTabMenu.File', 'LevelEditor.MainMenu.Select']


아래와 같이 새로운 Menu Label을 추가해본다.

(보다싶이 Menu Name은 Menu Lable과 일치하지 않는다 : 디버그 오버레이를 갖는 것이 중요하기 때문에)

menus = unreal.ToolMenus.get()
main_menu = menus.find_menu("LevelEditor.MainMenu")
custom_menu = main_menu.add_sub_menu("Custom Menu", "Python Automation", "Menu Name", "Menu Label")
menus.refresh_all_widgets()

 

도구 메뉴와 세 개의 스크립트 클래스를 사용하여 준비한 다음 인터페이스에 추가해보도록 한다

@unreal.uclass() #데코레이터 필히 추가
class MyScriptObject(unreal.ToolMenuEntryScript):
    @unreal.ufunction(override=True) #데코레이터 필히 추가 : 상속된 메서드 재정의
    def execute(self, context):
        print("SCRIPT EXECUTED")

이제 확장할 메뉴를 찾은 다음 개체를 초기화해야 한다.
레벨 편집기 메인 메뉴 패드를 확장하여 하위 메뉴를 구체적으로 편집하므로 도구 메뉴를 사용하여 메뉴를 다시 한번 찾아,
편집 메뉴를 변수에 할당한 다음

edit_menu = menus.find_menu("LevelEditor.MainMenu.Edit")

 

Python 개체를 초기화하고 이를 호출하는 스크립트 개체라는 다른 변수에 할당할 수 있다.

 

script_object = MyScriptObject()

 

해당 메소드가 일부 매개변수를 가져오는 init 입력 메소드를 호출하려면
첫번째 소유자 이름과 메뉴
이름 편집 메뉴를 사용하고, 이름 매개변수에 액세스 한 다음 색션을 지정해야 하고 이를 수행하려면 디버그 오버레이가 필요하다.

 

script_object.init_entry(
    owner_name=edit_menu.menu_name,
    menu=edit_menu.menu_name,
    section="EditMain",
    name="Unreal Engine 5 Python Autonation Course",
    label="Unreal Engine 5 Python Autonation Course",
    tool_tip="Custom Script Entry"
)
script_object.register_menu_entry()

개체에 새 항목을 추가한 것을 볼 수 있듯이 등록 메뉴 항목을 호출할 수 있다.

클릭해보면,
지정한 로그가 뜬다!


다른 예제. 콘텐츠 브라우저 창에 우클릭했을 때 뜨는 창

역시 메뉴를 이름으로 찾아 변수에 할당해보도록 한다

asset_context_menu = menus.find_menu("ContentBrowser.AssetContextMenu.StaticMesh")

 

여기서부터는 위에서 했던 것과 같은 방식으로 스크립트 객체를 초기화

script_object2 = MyScriptObject()

script_object2.init_entry(
    owner_name=asset_context_menu.menu_name,
    menu=asset_context_menu.menu_name,
    section="GetAssetActions",
    name="Unreal Engine 5 Python Autonation Course",
    label="Unreal Engine 5 Python Autonation Course",
    tool_tip="Custom Script Entry"
)

script_object2.register_menu_entry()

 

그러면 추가된 것을 확인할 수 있다!