-
Notifications
You must be signed in to change notification settings - Fork 3
/
unreal_utils.py
135 lines (109 loc) · 3.65 KB
/
unreal_utils.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Nguyen Phi Hung @ 2020
from unreal_global import *
import hashlib
import unreal
class AssetRegistryPostLoad:
_callbacks = {}
@classmethod
def register_callback(cls, func, *args, **kws):
cls._callbacks[hashlib.md5(str((func, args, kws)).encode()).hexdigest()] = (
func,
args,
kws,
)
@classmethod
def unregister_callback(cls, func, *args, **kws):
try:
del cls._callbacks[hashlib.md5(str((func, args, kws)).encode()).hexdigest()]
except KeyError:
unreal.log_error(
"No callback name {} with arguments: {} and keywords {} to unregister".format(
func.__name__, args, kws
)
)
@classmethod
def run_callbacks(cls):
for func_name, func_param in cls._callbacks.items():
func, args, kws = func_param
unreal.log_warning(
"execute {} with param {} keywords: {}".format(func.__name__, args, kws)
)
try:
func(*args, **kws)
except Exception as why:
unreal.log_error("failed to run with error:\n{}".format(why))
def get_outer_package():
return unreal.find_object(None, "/Engine/Transient")
def create_unreal_asset(
asset_name, package_path, factory, asset_class, force=False, save=True
):
"""
INPUT:
asset_name: str
Exp: "MyAwesomeBPActorClass"
package_path: str
Exp: "/Game/MyContentFolder"
package_path: unreal.Factory:
Exp: unreal.BlueprintFactory()
asset_class: unreal.Object
Exp: unreal.Actor
force: bool
Force remove old and create new one
save: bool
Save asset after creation
OUPUT:
unreal.Object
"""
asset_path = "{}/{}".format(package_path, asset_name)
if AssetLibrary.does_asset_exist(asset_path):
if force:
unreal.log_warning("{} exists. Skip creating".format(asset_name))
return
else:
unreal.log_warning("{} exists. Remove existing asset.".format(asset_name))
AssetLibrary.delete_asset(asset_path)
factory.set_editor_property("ParentClass", asset_class)
new_asset = AssetTools.create_asset(asset_name, package_path, None, factory)
if save:
AssetLibrary.save_loaded_asset(new_asset)
return new_asset
def create_levelsequence_asset(asset_name, package_path, force=False, save=True):
create_unreal_asset(
asset_name,
package_path,
unreal.LevelSequenceFactoryNew(),
unreal.LevelSequence,
force,
save,
)
def create_blueprint_asset(
asset_name, package_path, asset_parent_class, force=False, save=True
):
create_unreal_asset(
asset_name,
package_path,
unreal.BlueprintFactory(),
asset_parent_class,
force,
save,
)
def create_editor_utility_blueprint(
asset_name, asset_parent_class, force=False, save=False
):
create_unreal_asset(
f"{asset_name}",
"/Engine/",
unreal.EditorUtilityBlueprintFactory(),
asset_parent_class,
force,
save,
)
def register_editor_utility_blueprint(asset_name, asset_parent_class):
AssetRegistryPostLoad.register_callback(
create_editor_utility_blueprint, asset_name, asset_parent_class
)
# def new_object(object_class):
# unreal.load_object(unreal.new_object(object_class))
# def register_editor_utility_blueprint(asset_name, asset_parent_class):
# AssetRegistryPostLoad.register_callback(new_object, asset_parent_class)