1
1
# This file is based on templates provided and copyrighted by Autodesk, Inc.
2
- # This file has been modified by Epic Games, Inc. and is subject to the license
2
+ # This file has been modified by Epic Games, Inc. and is subject to the license
3
3
# file included in this repository.
4
4
5
5
"""
6
6
Hook that loads defines all the available actions, broken down by publish type.
7
7
"""
8
8
9
- import pprint
10
9
import os
11
10
import sgtk
12
11
import unreal
@@ -68,7 +67,7 @@ def generate_actions(self, sg_publish_data, actions, ui_area):
68
67
"params" : None ,
69
68
"caption" : "Import into Content Browser" ,
70
69
"description" : "This will import the asset into the Unreal Editor Content Browser." })
71
-
70
+
72
71
return action_instances
73
72
74
73
def execute_multiple_actions (self , actions ):
@@ -135,24 +134,24 @@ def _import_to_content_browser(self, path, sg_publish_data):
135
134
:param path: Path to file.
136
135
:param sg_publish_data: Shotgun data dictionary with all the standard publish fields.
137
136
"""
138
-
137
+
139
138
unreal .log ("File to import: {}" .format (path ))
140
139
141
140
if not os .path .exists (path ):
142
141
raise Exception ("File not found on disk - '%s'" % path )
143
142
144
143
destination_path , destination_name = self ._get_destination_path_and_name (sg_publish_data )
145
-
144
+
146
145
asset_path = _unreal_import_fbx_asset (path , destination_path , destination_name )
147
-
146
+
148
147
if asset_path :
149
148
self ._set_asset_metadata (asset_path , sg_publish_data )
150
-
149
+
151
150
# Focus the Unreal Content Browser on the imported asset
152
151
asset_paths = []
153
152
asset_paths .append (asset_path )
154
153
unreal .EditorAssetLibrary .sync_browser_to_objects (asset_paths )
155
-
154
+
156
155
def _set_asset_metadata (self , asset_path , sg_publish_data ):
157
156
"""
158
157
Set needed metadata on the given asset
@@ -161,9 +160,9 @@ def _set_asset_metadata(self, asset_path, sg_publish_data):
161
160
162
161
if not asset :
163
162
return
164
-
163
+
165
164
engine = sgtk .platform .current_engine ()
166
-
165
+
167
166
# Add a metadata tag for "created_by"
168
167
if "created_by" in sg_publish_data :
169
168
createdby_dict = sg_publish_data ["created_by" ]
@@ -172,12 +171,12 @@ def _set_asset_metadata(self, asset_path, sg_publish_data):
172
171
name = createdby_dict ["name" ]
173
172
elif "id" in createdby_dict :
174
173
name = createdby_dict ["id" ]
175
-
174
+
176
175
tag = engine .get_metadata_tag ("created_by" )
177
176
unreal .EditorAssetLibrary .set_metadata_tag (asset , tag , name )
178
177
179
178
# Add a metadata tag for the Shotgun URL
180
- # Construct the PublishedFile URL from the publish data type and id since
179
+ # Construct the PublishedFile URL from the publish data type and id since
181
180
# the context of a PublishedFile is the Project context
182
181
shotgun_site = self .sgtk .shotgun_url
183
182
type = sg_publish_data ["type" ]
@@ -191,24 +190,24 @@ def _set_asset_metadata(self, asset_path, sg_publish_data):
191
190
entity_dict = sg_publish_data["entity"]
192
191
context = self.sgtk.context_from_entity_dictionary(entity_dict)
193
192
url = context.shotgun_url
194
-
193
+
195
194
if entity_dict["type"] == "Project":
196
- # As a last resort, construct the PublishedFile URL from the publish data type and id since
195
+ # As a last resort, construct the PublishedFile URL from the publish data type and id since
197
196
# the context of a PublishedFile is the Project context
198
197
shotgun_site = self.sgtk.shotgun_url
199
198
type = sg_publish_data["type"]
200
199
id = sg_publish_data["id"]
201
200
url = shotgun_site + "/detail/" + type + "/" + str(id)
202
201
"""
203
-
202
+
204
203
tag = engine .get_metadata_tag ("url" )
205
204
unreal .EditorAssetLibrary .set_metadata_tag (asset , tag , url )
206
205
207
206
unreal .EditorAssetLibrary .save_loaded_asset (asset )
208
-
207
+
209
208
##############################################################################################################
210
209
# helper methods which can be subclassed in custom hooks to fine tune the behaviour of things
211
-
210
+
212
211
def _get_destination_path_and_name (self , sg_publish_data ):
213
212
"""
214
213
Get the destination path and name from the publish data and the templates
@@ -247,14 +246,14 @@ def _get_destination_path_and_name(self, sg_publish_data):
247
246
248
247
# Add the name field from the publish data
249
248
fields ["name" ] = name
250
-
249
+
251
250
# Get destination path by applying fields to destination template
252
251
# Fall back to the root level if unsuccessful
253
252
try :
254
253
destination_path = destination_template .apply_fields (fields )
255
254
except Exception :
256
255
destination_path = "/Game/Assets/"
257
-
256
+
258
257
# Query the fields needed for the name template from the context
259
258
name_fields = context .as_template_fields (destination_name_template )
260
259
@@ -269,18 +268,21 @@ def _get_destination_path_and_name(self, sg_publish_data):
269
268
destination_name = _sanitize_name (sg_publish_data ["code" ])
270
269
271
270
return destination_path , destination_name
272
-
271
+
272
+
273
273
"""
274
274
Functions to import FBX into Unreal
275
275
"""
276
276
277
+
277
278
def _sanitize_name (name ):
278
279
# Remove the default Shotgun versioning number if found (of the form '.v001')
279
280
name_no_version = re .sub (r'.v[0-9]{3}' , '' , name )
280
-
281
+
281
282
# Replace any remaining '.' with '_' since they are not allowed in Unreal asset names
282
283
return name_no_version .replace ('.' , '_' )
283
284
285
+
284
286
def _unreal_import_fbx_asset (input_path , destination_path , destination_name ):
285
287
"""
286
288
Import an FBX into Unreal Content Browser
@@ -291,11 +293,11 @@ def _unreal_import_fbx_asset(input_path, destination_path, destination_name):
291
293
"""
292
294
tasks = []
293
295
tasks .append (_generate_fbx_import_task (input_path , destination_path , destination_name ))
294
-
296
+
295
297
unreal .AssetToolsHelpers .get_asset_tools ().import_asset_tasks (tasks )
296
298
297
299
first_imported_object = None
298
-
300
+
299
301
for task in tasks :
300
302
unreal .log ("Import Task for: {}" .format (task .filename ))
301
303
for object_path in task .imported_object_paths :
@@ -304,10 +306,19 @@ def _unreal_import_fbx_asset(input_path, destination_path, destination_name):
304
306
first_imported_object = object_path
305
307
306
308
return first_imported_object
307
-
308
- def _generate_fbx_import_task (filename , destination_path , destination_name = None , replace_existing = True ,
309
- automated = True , save = True , materials = True ,
310
- textures = True , as_skeletal = False ):
309
+
310
+
311
+ def _generate_fbx_import_task (
312
+ filename ,
313
+ destination_path ,
314
+ destination_name = None ,
315
+ replace_existing = True ,
316
+ automated = True ,
317
+ save = True ,
318
+ materials = True ,
319
+ textures = True ,
320
+ as_skeletal = False
321
+ ):
311
322
"""
312
323
Create and configure an Unreal AssetImportTask
313
324
@@ -318,11 +329,11 @@ def _generate_fbx_import_task(filename, destination_path, destination_name=None,
318
329
task = unreal .AssetImportTask ()
319
330
task .filename = filename
320
331
task .destination_path = destination_path
321
-
332
+
322
333
# By default, destination_name is the filename without the extension
323
334
if destination_name is not None :
324
335
task .destination_name = destination_name
325
-
336
+
326
337
task .replace_existing = replace_existing
327
338
task .automated = automated
328
339
task .save = save
0 commit comments