Skip to content

Commit 5a8977f

Browse files
committed
add function definitions
+ try to switch unity project when exporting to a new fbx (just like on import)
1 parent 42b3b6a commit 5a8977f

File tree

1 file changed

+51
-22
lines changed
  • Assets/com.unity.formats.fbx/Editor/Integrations/Autodesk/maya/scripts

1 file changed

+51
-22
lines changed

Assets/com.unity.formats.fbx/Editor/Integrations/Autodesk/maya/scripts/unityCommands.mel

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ proc string checkNamespaceNeedsUpdate(string $unitySet, string $unityFbxNamespac
141141

142142
// =======================
143143

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)
144150
proc string[] getExportSetAttributes(string $exportPath, int $exportAnimOnly){
145151
global int $UnityFbxFilePathIndex;
146152
global int $UnityFbxFileNameIndex;
@@ -175,11 +181,15 @@ proc string[] getExportSetAttributes(string $exportPath, int $exportAnimOnly){
175181
return $exportAttributes;
176182
}
177183

184+
// Get export set name with format "{$fileNameWithoutExt}_UnityExportSet"
178185
proc string getNewExportSetName(string $fileNameWithoutExt){
179186
global string $UnityExportSetNameFormat;
180187
return `format -stringArg $fileNameWithoutExt $UnityExportSetNameFormat`;
181188
}
182189

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.
183193
proc string getTargetNamespaceName(string $fileNameWithoutExt){
184194
string $origNamespace = `namespaceInfo -cur -an`;
185195
string $targetNamespace = ":" + $fileNameWithoutExt;
@@ -190,7 +200,9 @@ proc string getTargetNamespaceName(string $fileNameWithoutExt){
190200
return $targetNamespace;
191201
}
192202

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.
194206
proc string getSetNamespace(string $unityExportSet){
195207
global string $UnityFbxNamespaceAttr;
196208

@@ -206,12 +218,15 @@ proc string getSetNamespace(string $unityExportSet){
206218
$setNamespace = checkNamespaceNeedsUpdate($unityExportSet, $setNamespace, getObjectNamespace($unitySetContents[0]));
207219
}
208220

221+
// check if the namespace exists
209222
if($setNamespace != "" && `namespace -exists $setNamespace`){
210223
return $setNamespace;
211224
}
212225
return "";
213226
}
214227

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.
215230
proc int getOrCreateExportSet(string $unityExportSet, string $origNamespace){
216231
if (setExists($unityExportSet)){
217232
return false;
@@ -236,6 +251,12 @@ proc int getOrCreateExportSet(string $unityExportSet, string $origNamespace){
236251
return true;
237252
}
238253

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)
239260
proc setExportSetAttributes(
240261
string $unityExportSet, int $isAnimFile, int $setCreated, string $exportAttrs[], string $targetNamespace
241262
){
@@ -279,6 +300,29 @@ proc setExportSetAttributes(
279300
if($fileNameWithoutExt != ""){
280301
storeAttribute($unityExportSet, $UnityFbxNamespaceAttr, $targetNamespace);
281302
}
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+
}
282326
}
283327

284328
// =======================
@@ -374,8 +418,6 @@ proc importFile(string $filePathStr){
374418
if (size($newItems) > 0){
375419
sets -include $unityExportSet $newItems;
376420
}
377-
// lock set so it doesn't get deleted when empty
378-
lockNode -lock true $unityExportSet;
379421
}
380422
}
381423

@@ -424,23 +466,8 @@ global proc unityImport(){
424466
importFile($filePathStr);
425467
}
426468

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]);
444471
}
445472

446473
// returns the intersection of two string arrays
@@ -599,15 +626,16 @@ proc setupNewExportSet(string $exportPath, int $exportAnimOnly, string $selected
599626
if (size($selectedObjects) > 0){
600627
sets -include $unityExportSet $selectedObjects;
601628
}
602-
// lock set so it doesn't get deleted when empty
603-
lockNode -lock true $unityExportSet;
604629
}
605630

606631
// add to the targetNamespace
607632
// iterate over all selected objects and rename
608633
for($object in $selectedObjects){
609634
rename $object ($targetNamespace + ":" + $object);
610635
}
636+
637+
// switch project if file exported to a different Unity project
638+
switchUnityProject($exportPath);
611639
}
612640

613641
proc unityExport(int $exportType){
@@ -666,6 +694,7 @@ proc unityExport(int $exportType){
666694
// if selection doesn't belong to a set, export to a new file
667695
if(size($setsToExport) <= 0){
668696
$unityProject = `optionVar -q "UnityProject"`;
697+
$unityProject = $unityProject + "/Assets";
669698
$exportPath = `fileDialog2 -ds 2 -cap "FBX Export Selection" -dir $unityProject -ff "*.fbx" -fm 0`;
670699
if(size($exportPath)<=0){
671700
return;

0 commit comments

Comments
 (0)