Skip to content

Commit 65afb17

Browse files
committed
add option to specify namespace when checkbox checked
1 parent 9949309 commit 65afb17

File tree

1 file changed

+110
-78
lines changed

1 file changed

+110
-78
lines changed

Integrations/Autodesk/maya/scripts/unityCommands.mel

Lines changed: 110 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ global string $UnityFbxFilePathAttr = "unityFbxModelFilePath";
22
global string $UnityFbxFileNameAttr = "unityFbxModelFileName";
33
global string $UnityFbxAnimFilePathAttr = "unityFbxAnimFilePath";
44
global string $UnityFbxAnimFileNameAttr = "unityFbxAnimFileName";
5-
global string $UnityFbxNamespaceAttr = "unityFbxStripAllNamespaces";
5+
global string $UnityFbxStripNamespaceAttr = "unityFbxStripNamespaces";
6+
global string $UnityFbxNamespaceAttr = "unityFbxNamespace";
67
global string $UnityExportSetNameFormat = "^1s_UnityExportSet";
78

89
global int $UnityFbxFilePathIndex = 0;
@@ -57,6 +58,13 @@ proc string getAttribute(string $node, string $attr){
5758
return "";
5859
}
5960

61+
proc int getBoolAttribute(string $node, string $attr){
62+
if (`attributeExists $attr $node`){
63+
return `getAttr ($node + "." + $attr)`;
64+
}
65+
return 0;
66+
}
67+
6068
proc storeBoolAttribute(string $node, string $attr, int $attrValue){
6169
if (!attributeExists($attr, $node)){
6270
addAttr -shortName $attr -storable true -attributeType bool $node;
@@ -107,26 +115,6 @@ proc string getObjectNamespace(string $objectName){
107115
return $lsResult[1];
108116
}
109117

110-
proc string getNewNamespace(string $n1, string $n2){
111-
string $n2Tokens[];
112-
int $n2NumTokens = `tokenize $n2 ":" $n2Tokens`;
113-
114-
string $newNamespace = ":";
115-
string $n1BaseName = `namespaceInfo -baseName $n1`;
116-
117-
for($i=$n2NumTokens-1; $i>=0; --$i){
118-
if($n2Tokens[$i] == $n1BaseName){
119-
break;
120-
}
121-
$n2Tokens[$i] = "";
122-
}
123-
$newNamespace = $newNamespace + stringArrayToString($n2Tokens, ":");
124-
if($newNamespace == ":"){
125-
return $n2;
126-
}
127-
return $newNamespace;
128-
}
129-
130118
// =======================
131119

132120
// Determine the export attributes to be used by the export set for the given export path.
@@ -214,6 +202,24 @@ proc int getOrCreateExportSet(string $unityExportSet, string $origNamespace){
214202
return true;
215203
}
216204

205+
global proc unityOnToggleStripNamespace(string $exportSetName)
206+
{
207+
global string $UnityFbxStripNamespaceAttr;
208+
global string $UnityFbxNamespaceAttr;
209+
210+
int $stripNamespaces = `getAttr ($exportSetName + "." + $UnityFbxStripNamespaceAttr)`;
211+
212+
$stripNamespaceAttrName = ($exportSetName + "." + $UnityFbxNamespaceAttr);
213+
214+
// temporarily unlock to be able to modify namespace attr
215+
lockNode -lock false $exportSetName;
216+
217+
setAttr -lock (!$stripNamespaces) $stripNamespaceAttrName;
218+
219+
// lock set so it doesn't get deleted when empty
220+
lockNode -lock true $exportSetName;
221+
}
222+
217223
// Add or update the following five attributes of the given export set, to be used for exporting:
218224
// - export directory
219225
// - export file name
@@ -229,6 +235,7 @@ proc setExportSetAttributes(
229235
global string $UnityFbxFileNameAttr;
230236
global string $UnityFbxAnimFilePathAttr;
231237
global string $UnityFbxAnimFileNameAttr;
238+
global string $UnityFbxStripNamespaceAttr;
232239
global string $UnityFbxNamespaceAttr;
233240

234241
global int $UnityFbxFilePathIndex;
@@ -262,8 +269,14 @@ proc setExportSetAttributes(
262269
storeStringAttribute($unityExportSet,$UnityFbxAnimFileNameAttr,$exportAnimFileName);
263270
}
264271

265-
storeBoolAttribute($unityExportSet, $UnityFbxNamespaceAttr, $stripNamespaces);
266-
272+
storeBoolAttribute($unityExportSet, $UnityFbxStripNamespaceAttr, $stripNamespaces);
273+
274+
storeStringAttribute($unityExportSet, $UnityFbxNamespaceAttr, "");
275+
276+
$stripNamespaceAttrName = ($unityExportSet + "." + $UnityFbxStripNamespaceAttr);
277+
setAttr -lock (!$stripNamespaces) $stripNamespaceAttrName;
278+
scriptJob -attributeChange $stripNamespaceAttrName ("unityOnToggleStripNamespace " + $unityExportSet) -protected;
279+
267280
// lock set so it doesn't get deleted when empty
268281
lockNode -lock true $unityExportSet;
269282
}
@@ -440,15 +453,81 @@ proc string[] getIntersection(string $set1[], string $set2[]){
440453
return $intersection;
441454
}
442455

456+
// Find the most common namespace among the selected objects. If there are multiple (because of nested namespaces),
457+
// prefer the most specific. For example if you have:
458+
//
459+
// test1:test2:sphere
460+
// test1:test3:cube
461+
// test1:test2:cylinder
462+
//
463+
// test1 will be returned because all objects have that namespace in common. However if you have:
464+
//
465+
// test1:test2:sphere
466+
// test1:test2:cube
467+
// test1:test2:cylinder
468+
//
469+
// Then it will return test1:test2.
470+
proc string getCommonNamespace(string $origSelection[]){
471+
// gather up all the unique namespaces
472+
string $selectedNamespaces[];
473+
for($i = 0; $i < size($origSelection); $i++){
474+
string $currNamespace = getObjectNamespace($origSelection[$i]);
475+
while ($currNamespace != ":" && !stringArrayContains($currNamespace, $selectedNamespaces)){
476+
$selectedNamespaces[size($selectedNamespaces)] = $currNamespace;
477+
$currNamespace = `namespaceInfo -p $currNamespace`;
478+
// make sure the namespace always starts with a colon
479+
if(!startsWith($currNamespace, ":")){
480+
$currNamespace = ":" + $currNamespace;
481+
}
482+
}
483+
}
484+
485+
// go through selection to find common namespace to set as default
486+
string $commonNamespace = ":";
487+
int $maxNamespaceCount = 0;
488+
for($i = 0; $i < size($selectedNamespaces); $i++){
489+
$currNamespace = $selectedNamespaces[$i];
490+
// get contents of namespace
491+
string $namespaceContents[] = `namespaceInfo -ls $currNamespace -r`;
492+
string $intersection[] = getIntersection($origSelection, $namespaceContents);
493+
int $intersectionSize = size($intersection);
494+
if($intersectionSize > $maxNamespaceCount ||
495+
// prefer more specific namespaces
496+
($maxNamespaceCount > 0 &&
497+
$intersectionSize == $maxNamespaceCount &&
498+
size($currNamespace) > size($commonNamespace)))
499+
{
500+
$commonNamespace = $currNamespace;
501+
$maxNamespaceCount = $intersectionSize;
502+
}
503+
}
504+
return $commonNamespace;
505+
}
506+
443507
proc exportSet(string $unitySet, int $exportAnim){
444508
global string $UnityFbxFilePathAttr;
445509
global string $UnityFbxFileNameAttr;
446510
global string $UnityFbxAnimFilePathAttr;
447511
global string $UnityFbxAnimFileNameAttr;
512+
global string $UnityFbxStripNamespaceAttr;
448513
global string $UnityFbxNamespaceAttr;
449514

450515
string $unitySetContents[] = `listConnections $unitySet`;
451-
int $stripNamespaces = getAttribute($unitySet, $UnityFbxNamespaceAttr);
516+
int $stripNamespaces = getBoolAttribute($unitySet, $UnityFbxStripNamespaceAttr);
517+
string $namespaceToStrip = getAttribute($unitySet, $UnityFbxNamespaceAttr);
518+
519+
// if there is no namespace manually set, find the common namespace
520+
if ($stripNamespaces){
521+
if ($namespaceToStrip == ""){
522+
$namespaceToStrip = getCommonNamespace($unitySetContents);
523+
}
524+
else {
525+
// make sure the namespace to strip is an absolute namespace
526+
if(!startsWith($namespaceToStrip, ":")){
527+
$namespaceToStrip = ":" + $namespaceToStrip;
528+
}
529+
}
530+
}
452531

453532
string $animatedObjectSet = "";
454533
if($exportAnim){
@@ -484,8 +563,12 @@ proc exportSet(string $unitySet, int $exportAnim){
484563
$strCmd = "";
485564
if ($unityFbxFilePath != "" && $unityFbxFileName != ""){
486565
// export selected, relative to given namespace
487-
string $exportFormat = "file -force -options \"\" -typ \"FBX export\" -es \"^2s/^3s\"";
488-
$strCmd = `format -s $unityFbxFilePath -s $unityFbxFileName $exportFormat`;
566+
string $exportFormat = "file -force -options \"\" -typ \"FBX export\" -relativeNamespace \"^1s\" -es \"^2s/^3s\"";
567+
string $relativeNamespace = ":";
568+
if ($stripNamespaces){
569+
$relativeNamespace = $namespaceToStrip;
570+
}
571+
$strCmd = `format -s $relativeNamespace -s $unityFbxFilePath -s $unityFbxFileName $exportFormat`;
489572
eval $strCmd;
490573
}
491574

@@ -758,57 +841,6 @@ proc string createCheckboxWithLabelLeft(string $label, string $parent, int $labe
758841
return `checkBox -value true -label " "`;
759842
}
760843

761-
// Find the most common namespace among the selected objects. If there are multiple (because of nested namespaces),
762-
// prefer the most specific. For example if you have:
763-
//
764-
// test1:test2:sphere
765-
// test1:test3:cube
766-
// test1:test2:cylinder
767-
//
768-
// test1 will be returned because all objects have that namespace in common. However if you have:
769-
//
770-
// test1:test2:sphere
771-
// test1:test2:cube
772-
// test1:test2:cylinder
773-
//
774-
// Then it will return test1:test2.
775-
proc string getCommonNamespace(string $origSelection[]){
776-
// gather up all the unique namespaces
777-
string $selectedNamespaces[];
778-
for($i = 0; $i < size($origSelection); $i++){
779-
string $currNamespace = getObjectNamespace($origSelection[$i]);
780-
while ($currNamespace != ":" && !stringArrayContains($currNamespace, $selectedNamespaces)){
781-
$selectedNamespaces[size($selectedNamespaces)] = $currNamespace;
782-
$currNamespace = `namespaceInfo -p $currNamespace`;
783-
// make sure the namespace always starts with a colon
784-
if(!startsWith($currNamespace, ":")){
785-
$currNamespace = ":" + $currNamespace;
786-
}
787-
}
788-
}
789-
790-
// go through selection to find common namespace to set as default
791-
string $commonNamespace = ":";
792-
int $maxNamespaceCount = 0;
793-
for($i = 0; $i < size($selectedNamespaces); $i++){
794-
$currNamespace = $selectedNamespaces[$i];
795-
// get contents of namespace
796-
string $namespaceContents[] = `namespaceInfo -ls $currNamespace -r`;
797-
string $intersection[] = getIntersection($origSelection, $namespaceContents);
798-
int $intersectionSize = size($intersection);
799-
if($intersectionSize > $maxNamespaceCount ||
800-
// prefer more specific namespaces
801-
($maxNamespaceCount > 0 &&
802-
$intersectionSize == $maxNamespaceCount &&
803-
size($currNamespace) > size($commonNamespace)))
804-
{
805-
$commonNamespace = $currNamespace;
806-
$maxNamespaceCount = $intersectionSize;
807-
}
808-
}
809-
return $commonNamespace;
810-
}
811-
812844
proc createExportSetDialog(int $exportType){
813845
$origSelection = `ls -sl`;
814846
if(size($origSelection) <= 0){
@@ -864,7 +896,7 @@ proc createExportSetDialog(int $exportType){
864896
}
865897
}
866898

867-
int $labelSize = 145;
899+
int $labelSize = 150;
868900
int $textFieldSize = 300;
869901

870902
// get namespace

0 commit comments

Comments
 (0)