Skip to content

Commit 9949309

Browse files
committed
replace namespace text box with checkbox in export set attributes
- also a checkbox to strip namespaces in export set creation dialog
1 parent ce9d0ab commit 9949309

File tree

1 file changed

+58
-90
lines changed

1 file changed

+58
-90
lines changed

Integrations/Autodesk/maya/scripts/unityCommands.mel

Lines changed: 58 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ global string $UnityFbxFilePathAttr = "unityFbxModelFilePath";
22
global string $UnityFbxFileNameAttr = "unityFbxModelFileName";
33
global string $UnityFbxAnimFilePathAttr = "unityFbxAnimFilePath";
44
global string $UnityFbxAnimFileNameAttr = "unityFbxAnimFileName";
5-
global string $UnityFbxNamespaceAttr = "unityFbxNamespace";
5+
global string $UnityFbxNamespaceAttr = "unityFbxStripAllNamespaces";
66
global string $UnityExportSetNameFormat = "^1s_UnityExportSet";
77

88
global int $UnityFbxFilePathIndex = 0;
@@ -57,9 +57,15 @@ proc string getAttribute(string $node, string $attr){
5757
return "";
5858
}
5959

60+
proc storeBoolAttribute(string $node, string $attr, int $attrValue){
61+
if (!attributeExists($attr, $node)){
62+
addAttr -shortName $attr -storable true -attributeType bool $node;
63+
}
64+
setAttr ($node+"."+$attr) $attrValue;
65+
}
6066

61-
proc storeAttribute(string $node, string $attr, string $attrValue){
62-
$attrType="string";
67+
proc storeStringAttribute(string $node, string $attr, string $attrValue){
68+
$attrType = "string";
6369
if (!attributeExists($attr, $node)){
6470
addAttr -shortName $attr -storable true -dataType $attrType $node;
6571
}
@@ -121,24 +127,6 @@ proc string getNewNamespace(string $n1, string $n2){
121127
return $newNamespace;
122128
}
123129

124-
proc string checkNamespaceNeedsUpdate(string $unitySet, string $unityFbxNamespace, string $objectNamespace){
125-
global string $UnityFbxNamespaceAttr;
126-
127-
string $newNamespace = getNewNamespace($unityFbxNamespace, $objectNamespace);
128-
if($unityFbxNamespace != $newNamespace){
129-
// popup asking to change namespace value to new namespace
130-
if(showConfirmDialog($unitySet,
131-
"Set namespace has been modified from "+$unityFbxNamespace+" to "+$newNamespace+", update export set namespace attribute?",
132-
"Yes", "No"
133-
)){
134-
storeAttribute($unitySet, $UnityFbxNamespaceAttr, $newNamespace);
135-
136-
return $newNamespace;
137-
}
138-
}
139-
return $unityFbxNamespace;
140-
}
141-
142130
// =======================
143131

144132
// Determine the export attributes to be used by the export set for the given export path.
@@ -200,31 +188,6 @@ proc string getTargetNamespaceName(string $fileNameWithoutExt){
200188
return $targetNamespace;
201189
}
202190

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.
206-
proc string getSetNamespace(string $unityExportSet){
207-
global string $UnityFbxNamespaceAttr;
208-
209-
if (!setExists($unityExportSet)){
210-
return "";
211-
}
212-
213-
string $unitySetContents[] = `listConnections $unityExportSet`;
214-
215-
// get namespace from set
216-
$setNamespace = getAttribute($unityExportSet, $UnityFbxNamespaceAttr);
217-
if(size($unitySetContents) > 0){
218-
$setNamespace = checkNamespaceNeedsUpdate($unityExportSet, $setNamespace, getObjectNamespace($unitySetContents[0]));
219-
}
220-
221-
// check if the namespace exists
222-
if($setNamespace != "" && `namespace -exists $setNamespace`){
223-
return $setNamespace;
224-
}
225-
return "";
226-
}
227-
228191
// Get or create the export set in the root namespace.
229192
// Return true if a set has been created, and false if it already exists.
230193
proc int getOrCreateExportSet(string $unityExportSet, string $origNamespace){
@@ -258,8 +221,10 @@ proc int getOrCreateExportSet(string $unityExportSet, string $origNamespace){
258221
// - export animation file name
259222
// - target namespace name (namespace that the contents of set belong to)
260223
proc setExportSetAttributes(
261-
string $unityExportSet, int $isAnimFile, int $setCreated, string $exportAttrs[], string $targetNamespace
224+
string $unityExportSet, int $isAnimFile, int $setCreated,
225+
string $exportAttrs[], int $stripNamespaces
262226
){
227+
263228
global string $UnityFbxFilePathAttr;
264229
global string $UnityFbxFileNameAttr;
265230
global string $UnityFbxAnimFilePathAttr;
@@ -282,24 +247,22 @@ proc setExportSetAttributes(
282247
lockNode -lock false $unityExportSet;
283248

284249
if ((!$isAnimFile || ($isAnimFile && $setCreated)) && $exportDir != ""){
285-
storeAttribute($unityExportSet, $UnityFbxFilePathAttr, $exportDir);
250+
storeStringAttribute($unityExportSet, $UnityFbxFilePathAttr, $exportDir);
286251
}
287252

288253
if ((!$isAnimFile || ($isAnimFile && $setCreated)) && $exportFileName != ""){
289-
storeAttribute($unityExportSet,$UnityFbxFileNameAttr,$exportFileName);
254+
storeStringAttribute($unityExportSet,$UnityFbxFileNameAttr,$exportFileName);
290255
}
291256

292257
if($exportAnimDir != ""){
293-
storeAttribute($unityExportSet,$UnityFbxAnimFilePathAttr,$exportAnimDir);
258+
storeStringAttribute($unityExportSet,$UnityFbxAnimFilePathAttr,$exportAnimDir);
294259
}
295260

296261
if($exportAnimFileName != ""){
297-
storeAttribute($unityExportSet,$UnityFbxAnimFileNameAttr,$exportAnimFileName);
298-
}
299-
300-
if($fileNameWithoutExt != ""){
301-
storeAttribute($unityExportSet, $UnityFbxNamespaceAttr, $targetNamespace);
262+
storeStringAttribute($unityExportSet,$UnityFbxAnimFileNameAttr,$exportAnimFileName);
302263
}
264+
265+
storeBoolAttribute($unityExportSet, $UnityFbxNamespaceAttr, $stripNamespaces);
303266

304267
// lock set so it doesn't get deleted when empty
305268
lockNode -lock true $unityExportSet;
@@ -333,7 +296,6 @@ proc importFile(string $filePathStr){
333296
global string $UnityFbxFileNameAttr;
334297
global string $UnityFbxAnimFilePathAttr;
335298
global string $UnityFbxAnimFileNameAttr;
336-
global string $UnityFbxNamespaceAttr;
337299

338300
global int $UnityFbxFilePathIndex;
339301
global int $UnityFbxFileNameIndex;
@@ -360,25 +322,18 @@ proc importFile(string $filePathStr){
360322
string $origNamespace = `namespaceInfo -cur -an`;
361323
string $targetNamespace = getTargetNamespaceName($fileNameWithoutExt);
362324

363-
$setNamespace = getSetNamespace($unityExportSet);
364-
$setNamespaceExists = ($setNamespace != "");
365-
if($setNamespaceExists){
366-
$targetNamespace = $setNamespace;
325+
// warn if namespace already exists
326+
if(`namespace -exists $targetNamespace`){
327+
if(!showConfirmDialog("Warning: " + $fileName,
328+
$targetNamespace + " namespace already exists, the imported objects will be added to the existing namespace and export set.",
329+
"Continue", "Cancel"
330+
)){
331+
// cancelled, don't import this fbx
332+
return;
333+
}
367334
}
368335
else{
369-
// warn if namespace already exists
370-
if(`namespace -exists $targetNamespace`){
371-
if(!showConfirmDialog("Warning: " + $fileName,
372-
$targetNamespace + " namespace already exists, the imported objects will be added to the existing namespace and export set.",
373-
"Continue", "Cancel"
374-
)){
375-
// cancelled, don't import this fbx
376-
return;
377-
}
378-
}
379-
else{
380-
namespace -add $targetNamespace;
381-
}
336+
namespace -add $targetNamespace;
382337
}
383338

384339
// Gather everything that is in the scene
@@ -392,9 +347,8 @@ proc importFile(string $filePathStr){
392347

393348
if(!$isAnimFile){
394349
// reset attribute values, in case import fails
395-
storeAttribute($unityExportSet, $UnityFbxFilePathAttr, "");
396-
storeAttribute($unityExportSet, $UnityFbxFileNameAttr, "");
397-
storeAttribute($unityExportSet, $UnityFbxNamespaceAttr, "");
350+
storeStringAttribute($unityExportSet, $UnityFbxFilePathAttr, "");
351+
storeStringAttribute($unityExportSet, $UnityFbxFileNameAttr, "");
398352
}
399353

400354
if(`namespaceInfo -cur -an` != $targetNamespace){
@@ -406,7 +360,7 @@ proc importFile(string $filePathStr){
406360
namespace -set $origNamespace;
407361
}
408362

409-
setExportSetAttributes($unityExportSet, $isAnimFile, $setCreated, $exportAttrs, $targetNamespace);
363+
setExportSetAttributes($unityExportSet, $isAnimFile, $setCreated, $exportAttrs, /*strip namespaces*/ true);
410364

411365
if (setExists($unityExportSet) == true){
412366
// figure out what has been added after import
@@ -494,11 +448,7 @@ proc exportSet(string $unitySet, int $exportAnim){
494448
global string $UnityFbxNamespaceAttr;
495449

496450
string $unitySetContents[] = `listConnections $unitySet`;
497-
string $unityFbxNamespace = getAttribute($unitySet, $UnityFbxNamespaceAttr);
498-
499-
if(size($unitySetContents) > 0){
500-
$unityFbxNamespace = checkNamespaceNeedsUpdate($unitySet, $unityFbxNamespace, getObjectNamespace($unitySetContents[0]));
501-
}
451+
int $stripNamespaces = getAttribute($unitySet, $UnityFbxNamespaceAttr);
502452

503453
string $animatedObjectSet = "";
504454
if($exportAnim){
@@ -534,8 +484,8 @@ proc exportSet(string $unitySet, int $exportAnim){
534484
$strCmd = "";
535485
if ($unityFbxFilePath != "" && $unityFbxFileName != ""){
536486
// export selected, relative to given namespace
537-
string $exportFormat = "file -force -options \"\" -typ \"FBX export\" -relativeNamespace \"^1s\" -es \"^2s/^3s\"";
538-
$strCmd = `format -s $unityFbxNamespace -s $unityFbxFilePath -s $unityFbxFileName $exportFormat`;
487+
string $exportFormat = "file -force -options \"\" -typ \"FBX export\" -es \"^2s/^3s\"";
488+
$strCmd = `format -s $unityFbxFilePath -s $unityFbxFileName $exportFormat`;
539489
eval $strCmd;
540490
}
541491

@@ -584,6 +534,7 @@ proc setupNewExportSet(
584534
string $modelFilename,
585535
string $animPath,
586536
string $animFilename,
537+
int $stripNamespaces,
587538
string $selectedObjects[]){
588539

589540
// make sure all necessary variables are set
@@ -656,7 +607,7 @@ proc setupNewExportSet(
656607
$exportAttrs[$UnityFbxAnimFileNameIndex] = $animFilename;
657608
$exportAttrs[$UnityFileNameWithoutExtIndex] = $exportFileNameWithoutExt;
658609

659-
setExportSetAttributes($unityExportSet, /*isAnimOnly*/ false, $setCreated, $exportAttrs, $namespace);
610+
setExportSetAttributes($unityExportSet, /*isAnimOnly*/ false, $setCreated, $exportAttrs, $stripNamespaces);
660611

661612
if (setExists($unityExportSet) == true){
662613
// clear contents of set
@@ -693,7 +644,8 @@ global proc unityOnCreateExportSet(
693644
string $modelPathField,
694645
string $modelFileField,
695646
string $animPathField,
696-
string $animFileField){
647+
string $animFileField,
648+
string $stripNamespaceCheckbox){
697649

698650
$origSelection = `ls -sl`;
699651
if(size($origSelection) <= 0){
@@ -737,13 +689,16 @@ global proc unityOnCreateExportSet(
737689
error ("Unity FBX Export Set Creation: Missing filepath for export.");
738690
return;
739691
}
692+
693+
int $stripNamespaces = `checkBox -q -value $stripNamespaceCheckbox`;
740694

741695
setupNewExportSet(
742696
$namespace,
743697
$modelPath,
744698
$modelFilename,
745699
$animPath,
746700
$animFilename,
701+
$stripNamespaces,
747702
$origSelection);
748703

749704
deleteUI -window $window;
@@ -792,6 +747,17 @@ proc string createTextFieldWithLabel(string $label, string $parent, int $labelSi
792747
return $textField;
793748
}
794749

750+
proc string createCheckboxWithLabelLeft(string $label, string $parent, int $labelSize, int $fieldSize){
751+
rowLayout
752+
-numberOfColumns 2
753+
-columnWidth2 $labelSize $fieldSize
754+
-columnAlign2 "left" "left"
755+
-p $parent;
756+
757+
text -label $label;
758+
return `checkBox -value true -label " "`;
759+
}
760+
795761
// Find the most common namespace among the selected objects. If there are multiple (because of nested namespaces),
796762
// prefer the most specific. For example if you have:
797763
//
@@ -866,7 +832,7 @@ proc createExportSetDialog(int $exportType){
866832
}
867833

868834
// open up a dialog for choosing the export set options
869-
string $window = `window -title "Unity FBX Export Options" -iconName "Short Name" -widthHeight 475 250`;
835+
string $window = `window -title "Unity FBX Export Options" -iconName "Short Name" -widthHeight 500 250`;
870836

871837
string $container = `formLayout -numberOfDivisions 100`;
872838

@@ -898,7 +864,7 @@ proc createExportSetDialog(int $exportType){
898864
}
899865
}
900866

901-
int $labelSize = 120;
867+
int $labelSize = 145;
902868
int $textFieldSize = 300;
903869

904870
// get namespace
@@ -920,15 +886,17 @@ proc createExportSetDialog(int $exportType){
920886
textField -e -text ($modelFilename + "@Take1.fbx") $animFileNameField;
921887
$animFilePath = " " + $animFilePathField + " " + $animFileNameField;
922888
}
889+
890+
string $stripNamespaceCheckbox = createCheckboxWithLabelLeft("Strip Namespaces on Export", $mainOptions, $labelSize, $textFieldSize);
923891

924-
int $buttonWidth = 158;
892+
int $buttonWidth = 166;
925893
string $buttons = `rowLayout
926894
-numberOfColumns 3
927895
-adjustableColumn3 1
928896
-columnWidth3 $buttonWidth $buttonWidth $buttonWidth
929897
-columnAlign3 "center" "center" "center" -p $container`;
930898

931-
string $createExportSetCommand = "unityOnCreateExportSet " + $window + " " + $namespaceField + $modelFilePath + $animFilePath;
899+
string $createExportSetCommand = "unityOnCreateExportSet " + $window + " " + $namespaceField + $modelFilePath + $animFilePath + " " + $stripNamespaceCheckbox;
932900

933901
button -label "Create Set and Export"
934902
-width $buttonWidth

0 commit comments

Comments
 (0)