ue python代码段记录

获取世界(世界上下文对象)

关卡
world = unreal.get_editor_subsystem(unreal.UnrealEditorSubsystem).get_editor_world()

编辑器
world = unreal.get_editor_subsystem(unreal.UnrealEditorSubsystem)

生成actor到场景

unreal.EditorLevelLibrary.spawn_actor_from_class(actor, [0.000000, 0.000000, 0.000000])

get世界中的类

unreal.GameplayStatics.get_all_actors_of_class(世界上下文,actor)

添加内容浏览器右键

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import unreal

menus = unreal.ToolMenus.get()

# add contextbrowser menu
menu_name = "ContentBrowser.AssetContextMenu"
menu = menus.find_menu(menu_name)
entry = unreal.ToolMenuEntry(type=unreal.MultiBlockType.MENU_ENTRY)
entry.set_label("Convert Materials")
# NOTE 注册执行的命令
typ = unreal.ToolMenuStringCommandType.PYTHON
run_str = "print(11)"
entry.set_string_command(typ, "", run_str)

menu.add_section('CustomControl', label='CustomMenu')
menu.add_menu_entry('CustomControl',entry)

添加视口右键

1
2
3
4
5
6
7
8
9
10
viewport_menu_name = "LevelEditor.ActorContextMenu"
menu = menus.find_menu(viewport_menu_name)
entry = unreal.ToolMenuEntry(type=unreal.MultiBlockType.MENU_ENTRY)
entry.set_label("Convert Materials")
typ = unreal.ToolMenuStringCommandType.PYTHON
run_str = ""
entry.set_string_command(typ, "", run_str)

menu.add_section('CustomControl', label='ComstomMenu')
menu.add_menu_entry('CustomControl',entry)

添加菜单栏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
type_map = {'COMMAND': unreal.ToolMenuStringCommandType.COMMAND,
'PYTHON': unreal.ToolMenuStringCommandType.PYTHON}
# 分割线字典用来分割项目
menu_section_dict = {
"Assets":"AssetsManager",
"Animation": "Animation",
"Render":"Render",
"Help":"Help"
}
#项目配置
menu_entry_dict = {
"Assets_Tool" : {
"section": "Assets",
"label": "AssetsManager 2.0",
"type": "PYTHON",
"command": "from imp import reload;import UE4Tools;reload(UE4Tools);UE4Tools.showWindow()"
}
}
#添加菜单栏目选项
menus = unreal.ToolMenus.get()
main_menu = menus.find_menu('LevelEditor.MainMenu')
if not main_menu:
raise RuntimeError("Failed to find the 'Main' menu. Something is wrong in the force!")
script_menu = main_menu.add_sub_menu(main_menu.get_name(), 'Tools', 'OneMT_Tools', 'OneMT')
# 添加子项
for section, label in menu_section_dict.items():
script_menu.add_section(section, label)

for menu, data in menu_entry_dict.items():
entry = unreal.ToolMenuEntry(name=menu, type=unreal.MultiBlockType.MENU_ENTRY, insert_position=unreal.ToolMenuInsert('', unreal.ToolMenuInsertType.FIRST))
entry.set_label(data.get('label', 'untitle'))
command = data.get('command', '')
entry.set_string_command(type_map[data.get('type', 0)], '', string=command)
script_menu.add_menu_entry(data.get('section', ''), entry)
menus.refresh_all_widgets()