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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237 | class SkeletalMeshFBXLoader(plugin.Loader):
"""Load Unreal SkeletalMesh from FBX."""
product_types = {"rig", "skeletalMesh"}
label = "Import FBX Skeletal Mesh"
representations = {"fbx"}
icon = "cube"
color = "orange"
loaded_asset_dir = "{folder[path]}/{product[name]}_{version[version]}"
loaded_asset_name = "{folder[name]}_{product[name]}_{version[version]}_{representation[name]}" # noqa
show_dialog = False
@classmethod
def apply_settings(cls, project_settings):
unreal_settings = project_settings["unreal"]["import_settings"]
super(SkeletalMeshFBXLoader, cls).apply_settings(project_settings)
cls.loaded_asset_dir = unreal_settings["loaded_asset_dir"]
cls.loaded_asset_name = unreal_settings["loaded_asset_name"]
cls.show_dialog = unreal_settings["show_dialog"]
@classmethod
def get_task(cls, filename, asset_dir, asset_name, replace):
task = unreal.AssetImportTask()
options = unreal.FbxImportUI()
task.set_editor_property('filename', filename)
task.set_editor_property('destination_path', asset_dir)
task.set_editor_property('destination_name', asset_name)
task.set_editor_property('replace_existing', replace)
task.set_editor_property('automated', not cls.show_dialog)
task.set_editor_property('save', True)
options.set_editor_property(
'automated_import_should_detect_type', False)
options.set_editor_property('import_as_skeletal', True)
options.set_editor_property('import_animations', False)
options.set_editor_property('import_mesh', True)
options.set_editor_property('import_materials', False)
options.set_editor_property('import_textures', False)
options.set_editor_property('skeleton', None)
options.set_editor_property('create_physics_asset', False)
options.set_editor_property(
'mesh_type_to_import',
unreal.FBXImportType.FBXIT_SKELETAL_MESH)
options.skeletal_mesh_import_data.set_editor_property(
'import_content_type',
unreal.FBXImportContentType.FBXICT_ALL)
options.skeletal_mesh_import_data.set_editor_property(
'normal_import_method',
unreal.FBXNormalImportMethod.FBXNIM_IMPORT_NORMALS)
task.options = options
return task
def import_and_containerize(
self, filepath, asset_dir, asset_name, container_name
):
task = None
if not unreal.EditorAssetLibrary.does_directory_exist(asset_dir):
unreal.EditorAssetLibrary.make_directory(asset_dir)
if not unreal.EditorAssetLibrary.does_asset_exist(
f"{asset_dir}/{asset_name}"):
task = self.get_task(filepath, asset_dir, asset_name, False)
unreal.AssetToolsHelpers.get_asset_tools().import_asset_tasks([task])
if not unreal.EditorAssetLibrary.does_asset_exist(
f"{asset_dir}/{container_name}"):
# Create Asset Container
create_container(container=container_name, path=asset_dir)
return asset_dir
def imprint(
self,
folder_path,
asset_dir,
container_name,
asset_name,
representation,
product_type,
project_name,
layout
):
data = {
"schema": "ayon:container-2.0",
"id": AYON_CONTAINER_ID,
"folder_path": folder_path,
"namespace": asset_dir,
"container_name": container_name,
"asset_name": asset_name,
"loader": str(self.__class__.__name__),
"representation": representation["id"],
"parent": representation["versionId"],
"product_type": product_type,
# TODO these should be probably removed
"asset": folder_path,
"family": product_type,
"project_name": project_name,
"layout": layout
}
imprint(f"{asset_dir}/{container_name}", data)
def load(self, context, name, namespace, options):
"""Load and containerise representation into Content Browser.
Args:
context (dict): application context
name (str): Product name
namespace (str): in Unreal this is basically path to container.
This is not passed here, so namespace is set
by `containerise()` because only then we know
real path.
data (dict): Those would be data to be imprinted.
Returns:
list(str): list of container content
"""
# Create directory for asset and Ayon container
folder_name = context["folder"]["name"]
product_type = context["product"]["productType"]
suffix = "_CON"
path = self.filepath_from_context(context)
asset_root, asset_name = format_asset_directory(
context, self.loaded_asset_dir, self.loaded_asset_name
)
tools = unreal.AssetToolsHelpers().get_asset_tools()
asset_dir, container_name = tools.create_unique_asset_name(
asset_root, suffix="")
container_name += suffix
should_use_layout = options.get("layout", False)
# Get existing asset dir if possible, otherwise import & containerize
if should_use_layout and (
existing_asset_dir := get_dir_from_existing_asset(
asset_dir, asset_name)
):
asset_dir = existing_asset_dir
else:
asset_dir = self.import_and_containerize(
path, asset_dir, asset_name, container_name
)
self.imprint(
folder_name,
asset_dir,
container_name,
asset_name,
context["representation"],
product_type,
context["project"]["name"],
should_use_layout
)
asset_content = unreal.EditorAssetLibrary.list_assets(
asset_dir, recursive=True, include_folder=True
)
for a in asset_content:
unreal.EditorAssetLibrary.save_asset(a)
return asset_content
def update(self, container, context):
folder_path = context["folder"]["path"]
product_type = context["product"]["productType"]
repre_entity = context["representation"]
# Create directory for asset and Ayon container
suffix = "_CON"
path = self.filepath_from_context(context)
asset_root, asset_name = format_asset_directory(
context, self.loaded_asset_dir, self.loaded_asset_name
)
tools = unreal.AssetToolsHelpers().get_asset_tools()
asset_dir, container_name = tools.create_unique_asset_name(
asset_root, suffix="")
container_name += suffix
should_use_layout = container.get("layout", False)
# Get existing asset dir if possible, otherwise import & containerize
if should_use_layout and (
existing_asset_dir := get_dir_from_existing_asset(
asset_dir, asset_name)
):
asset_dir = existing_asset_dir
else:
asset_dir = self.import_and_containerize(
path, asset_dir, asset_name, container_name
)
self.imprint(
folder_path,
asset_dir,
container_name,
asset_name,
repre_entity,
product_type,
context["project"]["name"],
should_use_layout
)
asset_content = unreal.EditorAssetLibrary.list_assets(
asset_dir, recursive=True, include_folder=False
)
for a in asset_content:
unreal.EditorAssetLibrary.save_asset(a)
def remove(self, container):
path = container["namespace"]
if unreal.EditorAssetLibrary.does_directory_exist(path):
unreal.EditorAssetLibrary.delete_directory(path)
|