@@ -141,6 +141,12 @@ proc string checkNamespaceNeedsUpdate(string $unitySet, string $unityFbxNamespac
141
141
142
142
// =======================
143
143
144
+ // Determine the export attributes to be used by the export set for the given export path.
145
+ // If animation only, check for {model}@{animation}.fbx naming convention and set the name to be used for the export set and namespace (filename without ext)
146
+ // to be {model} so that if {model}.fbx has already been imported, the animation is applied.
147
+ //
148
+ // returns an array of export attributes:
149
+ // export file path, file name, animation file path, animation file name, and filename without ext (for the export set and namespace names)
144
150
proc string[] getExportSetAttributes(string $exportPath, int $exportAnimOnly){
145
151
global int $UnityFbxFilePathIndex;
146
152
global int $UnityFbxFileNameIndex;
@@ -175,11 +181,15 @@ proc string[] getExportSetAttributes(string $exportPath, int $exportAnimOnly){
175
181
return $exportAttributes;
176
182
}
177
183
184
+ // Get export set name with format "{$fileNameWithoutExt}_UnityExportSet"
178
185
proc string getNewExportSetName(string $fileNameWithoutExt){
179
186
global string $UnityExportSetNameFormat;
180
187
return `format -stringArg $fileNameWithoutExt $UnityExportSetNameFormat`;
181
188
}
182
189
190
+ // Get the name of the namespace to add contents of fbx into.
191
+ // Namespace name is {currentNamespace}:{$fileNameWithoutExt} or :{$fileNameWithoutExt}
192
+ // if current namespace is root namespace.
183
193
proc string getTargetNamespaceName(string $fileNameWithoutExt){
184
194
string $origNamespace = `namespaceInfo -cur -an`;
185
195
string $targetNamespace = ":" + $fileNameWithoutExt;
@@ -190,7 +200,9 @@ proc string getTargetNamespaceName(string $fileNameWithoutExt){
190
200
return $targetNamespace;
191
201
}
192
202
193
-
203
+ // Get the name of the namespace containing the contents of the given export set
204
+ // or "" if none.
205
+ // The name of the namespace is stored as an attribute on the set.
194
206
proc string getSetNamespace(string $unityExportSet){
195
207
global string $UnityFbxNamespaceAttr;
196
208
@@ -206,12 +218,15 @@ proc string getSetNamespace(string $unityExportSet){
206
218
$setNamespace = checkNamespaceNeedsUpdate($unityExportSet, $setNamespace, getObjectNamespace($unitySetContents[0]));
207
219
}
208
220
221
+ // check if the namespace exists
209
222
if($setNamespace != "" && `namespace -exists $setNamespace`){
210
223
return $setNamespace;
211
224
}
212
225
return "";
213
226
}
214
227
228
+ // Get or create the export set in the root namespace.
229
+ // Return true if a set has been created, and false if it already exists.
215
230
proc int getOrCreateExportSet(string $unityExportSet, string $origNamespace){
216
231
if (setExists($unityExportSet)){
217
232
return false;
@@ -236,6 +251,12 @@ proc int getOrCreateExportSet(string $unityExportSet, string $origNamespace){
236
251
return true;
237
252
}
238
253
254
+ // Add or update the following five attributes of the given export set, to be used for exporting:
255
+ // - export directory
256
+ // - export file name
257
+ // - export animation directory
258
+ // - export animation file name
259
+ // - target namespace name (namespace that the contents of set belong to)
239
260
proc setExportSetAttributes(
240
261
string $unityExportSet, int $isAnimFile, int $setCreated, string $exportAttrs[], string $targetNamespace
241
262
){
@@ -279,6 +300,29 @@ proc setExportSetAttributes(
279
300
if($fileNameWithoutExt != ""){
280
301
storeAttribute($unityExportSet, $UnityFbxNamespaceAttr, $targetNamespace);
281
302
}
303
+
304
+ // lock set so it doesn't get deleted when empty
305
+ lockNode -lock true $unityExportSet;
306
+ }
307
+
308
+ proc switchUnityProject(string $newProjectPath){
309
+ $currentDir = dirname($newProjectPath);
310
+ // Change Unity project if fbx is from a different Unity project.
311
+ // Get the project based on the folder structure (i.e. folder above Assets)
312
+ $head = dirname($currentDir);
313
+ $tail = basename($currentDir, "");
314
+ // Check that we are not at the root directory.
315
+ // dirname($head) returns the last directory name in the path,
316
+ // or head if head is the root directory.
317
+ while ($head != "" && dirname($head) != $head){
318
+ if (`strcmp $tail "Assets"` == 0){
319
+ // this is a valid Unity project, so set it
320
+ optionVar -sv "UnityProject" $head;
321
+ break;
322
+ }
323
+ $head = dirname($head);
324
+ $tail = basename($head, "");
325
+ }
282
326
}
283
327
284
328
// =======================
@@ -374,8 +418,6 @@ proc importFile(string $filePathStr){
374
418
if (size($newItems) > 0){
375
419
sets -include $unityExportSet $newItems;
376
420
}
377
- // lock set so it doesn't get deleted when empty
378
- lockNode -lock true $unityExportSet;
379
421
}
380
422
}
381
423
@@ -424,23 +466,8 @@ global proc unityImport(){
424
466
importFile($filePathStr);
425
467
}
426
468
427
- $currentDir = dirname($filePaths[0]);
428
- // Change Unity project if fbx is from a different Unity project.
429
- // Get the project based on the folder structure (i.e. folder above Assets)
430
- $head = dirname($currentDir);
431
- $tail = basename($currentDir, "");
432
- // Check that we are not at the root directory.
433
- // dirname($head) returns the last directory name in the path,
434
- // or head if head is the root directory.
435
- while ($head != "" && dirname($head) != $head){
436
- if (`strcmp $tail "Assets"` == 0){
437
- // this is a valid Unity project, so set it
438
- optionVar -sv "UnityProject" $head;
439
- break;
440
- }
441
- $head = dirname($head);
442
- $tail = basename($head, "");
443
- }
469
+ // switch project if file imported from a different Unity project
470
+ switchUnityProject($filePaths[0]);
444
471
}
445
472
446
473
// returns the intersection of two string arrays
@@ -599,15 +626,16 @@ proc setupNewExportSet(string $exportPath, int $exportAnimOnly, string $selected
599
626
if (size($selectedObjects) > 0){
600
627
sets -include $unityExportSet $selectedObjects;
601
628
}
602
- // lock set so it doesn't get deleted when empty
603
- lockNode -lock true $unityExportSet;
604
629
}
605
630
606
631
// add to the targetNamespace
607
632
// iterate over all selected objects and rename
608
633
for($object in $selectedObjects){
609
634
rename $object ($targetNamespace + ":" + $object);
610
635
}
636
+
637
+ // switch project if file exported to a different Unity project
638
+ switchUnityProject($exportPath);
611
639
}
612
640
613
641
proc unityExport(int $exportType){
@@ -666,6 +694,7 @@ proc unityExport(int $exportType){
666
694
// if selection doesn't belong to a set, export to a new file
667
695
if(size($setsToExport) <= 0){
668
696
$unityProject = `optionVar -q "UnityProject"`;
697
+ $unityProject = $unityProject + "/Assets";
669
698
$exportPath = `fileDialog2 -ds 2 -cap "FBX Export Selection" -dir $unityProject -ff "*.fbx" -fm 0`;
670
699
if(size($exportPath)<=0){
671
700
return;
0 commit comments