Skip to content

Commit 4fab4e0

Browse files
authored
Merge pull request #311 from Unity-Technologies/UNI-37428-resolve-name-clashing-on-import
Uni 37428 resolve name clashing on import
2 parents 7de21e1 + c5ab17d commit 4fab4e0

File tree

2 files changed

+162
-27
lines changed

2 files changed

+162
-27
lines changed

Assets/Integrations/Autodesk/maya/scripts/unityCommands.mel

Lines changed: 160 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ global string $UnityFbxFilePathAttr = "unityFbxModelFilePath";
33
global string $UnityFbxFileNameAttr = "unityFbxModelFileName";
44
global string $UnityFbxAnimFilePathAttr = "unityFbxAnimFilePath";
55
global string $UnityFbxAnimFileNameAttr = "unityFbxAnimFileName";
6+
global string $UnityFbxNamespaceAttr = "unityFbxNamespace";
67
global string $UnityExportSetNameFormat = "^1s_UnityExportSet";
78

89
global proc unityRemoveNativeMenuOnLoad(){
@@ -70,41 +71,153 @@ proc int loadUnityPlugin(string $plugin){
7071
return true;
7172
};
7273

74+
// show a yes/no style dialog, return true if user clicked confirm, false if user canceled
75+
proc int showConfirmDialog(string $title, string $message, string $confirmButtonName, string $cancelButtonName){
76+
// create a confirm dialog with a yes and no button. Specif
77+
$response = `confirmDialog -title $title
78+
-message $message
79+
-button $confirmButtonName
80+
-button $cancelButtonName
81+
-defaultButton $confirmButtonName
82+
-cancelButton $cancelButtonName
83+
-dismissString $cancelButtonName`;
84+
85+
return ( $response == $confirmButtonName );
86+
}
87+
88+
// get the namespace of the given object
89+
proc string getObjectNamespace(string $objectName){
90+
string $lsResult[] = `ls -showNamespace -an $objectName`;
91+
return $lsResult[1];
92+
}
93+
94+
proc string getNewNamespace(string $n1, string $n2){
95+
string $n2Tokens[];
96+
int $n2NumTokens = `tokenize $n2 ":" $n2Tokens`;
97+
98+
string $newNamespace = ":";
99+
string $n1BaseName = `namespaceInfo -baseName $n1`;
100+
101+
for($i=$n2NumTokens-1; $i>=0; --$i){
102+
if($n2Tokens[$i] == $n1BaseName){
103+
break;
104+
}
105+
$n2Tokens[$i] = "";
106+
}
107+
$newNamespace = $newNamespace + stringArrayToString($n2Tokens, ":");
108+
if($newNamespace == ":"){
109+
return $n2;
110+
}
111+
return $newNamespace;
112+
}
113+
114+
proc string checkNamespaceNeedsUpdate(string $unitySet, string $unityFbxNamespace, string $objectNamespace){
115+
global string $UnityFbxNamespaceAttr;
116+
117+
string $newNamespace = getNewNamespace($unityFbxNamespace, $objectNamespace);
118+
if($unityFbxNamespace != $newNamespace){
119+
// popup asking to change namespace value to new namespace
120+
if(showConfirmDialog($unitySet,
121+
"Set namespace has been modified from "+$unityFbxNamespace+" to "+$newNamespace+", update export set namespace attribute?",
122+
"Yes", "No"
123+
)){
124+
storeAttribute($unitySet, $UnityFbxNamespaceAttr, $newNamespace);
125+
126+
return $newNamespace;
127+
}
128+
}
129+
return $unityFbxNamespace;
130+
}
131+
73132
proc importFile(string $filePathStr){
74133
// get the global variables
75134
global string $UnityExportSets[];
76135
global string $UnityFbxFilePathAttr;
77136
global string $UnityFbxFileNameAttr;
78137
global string $UnityFbxAnimFilePathAttr;
79138
global string $UnityFbxAnimFileNameAttr;
139+
global string $UnityFbxNamespaceAttr;
80140
global string $UnityExportSetNameFormat;
81-
82-
$tempPath = dirname($filePathStr);
83-
$tempName = basename($filePathStr, "");
84-
$tempAnimPath = "";
85-
$tempAnimName = "";
86-
$nameWithoutExt = basename($filePathStr, ".fbx");
141+
142+
$currentDir = dirname($filePathStr);
143+
$fileName = basename($filePathStr, "");
144+
$currentAnimDir = "";
145+
$animFileName = "";
146+
$fileNameWithoutExt = basename($filePathStr, ".fbx");
87147

88148
$isAnimFile = false;
89149
if(match("@", basename($filePathStr, ".fbx")) != ""){
90150
// import as animation
91151
$isAnimFile = true;
92-
$tempAnimPath = $tempPath;
93-
$tempAnimName = $tempName;
152+
$currentAnimDir = $currentDir;
153+
$animFileName = $fileName;
94154

95-
$nameWithoutExt = match("[^@]+", $nameWithoutExt);
155+
$fileNameWithoutExt = match("[^@]+", $fileNameWithoutExt);
96156
}
97-
$unityExportSet = `format -stringArg $nameWithoutExt $UnityExportSetNameFormat`;
157+
$unityExportSet = `format -stringArg $fileNameWithoutExt $UnityExportSetNameFormat`;
98158

159+
string $origNamespace = `namespaceInfo -cur -an`;
160+
string $targetNamespace = ":" + $fileNameWithoutExt;
161+
// make sure there are no duplicate colons in namespace name
162+
if($origNamespace != ":"){
163+
$targetNamespace = `format -s $origNamespace -s $fileNameWithoutExt "^1s:^2s"`;
164+
}
165+
166+
$setNamespaceExists = false;
167+
if (setExists($unityExportSet)){
168+
string $unitySetContents[] = `listConnections $unityExportSet`;
169+
170+
// get namespace from set
171+
$setNamespace = getAttribute($unityExportSet, $UnityFbxNamespaceAttr);
172+
if(size($unitySetContents) > 0){
173+
$setNamespace = checkNamespaceNeedsUpdate($unityExportSet, $setNamespace, getObjectNamespace($unitySetContents[0]));
174+
}
175+
176+
if($setNamespace != "" && `namespace -exists $setNamespace`){
177+
$targetNamespace = $setNamespace;
178+
$setNamespaceExists = true;
179+
}
180+
}
181+
182+
// warn if namespace already exists
183+
if(!$setNamespaceExists){
184+
if(`namespace -exists $targetNamespace`){
185+
if(!showConfirmDialog("Warning: " + $fileName,
186+
$targetNamespace + " namespace already exists, the imported objects will be added to the existing namespace and export set.",
187+
"Continue", "Cancel"
188+
)){
189+
// cancelled, don't import this fbx
190+
return;
191+
}
192+
}
193+
else{
194+
namespace -add $targetNamespace;
195+
}
196+
}
197+
99198
// Gather everything that is in the scene
100199
$origItemsInScene = `ls -tr -o -r true`;
101200

102201
// Get or create the Unity Fbx Export Set
103202
$setCreated = false;
104203
if (!setExists($unityExportSet)){
204+
if(!`namespaceInfo -isRootNamespace $origNamespace`){
205+
namespace -set ":";
206+
}
207+
208+
// if a set is selected when creating a new set, then
209+
// the selected set will be added into the new set.
210+
// avoid this by temporarily deselecting everything.
211+
$origSelection = `ls -sl`;
212+
select -clear;
213+
105214
// couldn't find export set so create it
106215
sets -name $unityExportSet;
107216
$setCreated = true;
217+
218+
if(size($origSelection) > 0){
219+
select -r $origSelection;
220+
}
108221
}
109222

110223
// unlock set so we can add attributes to it
@@ -114,24 +227,36 @@ proc importFile(string $filePathStr){
114227
// reset attribute values, in case import fails
115228
storeAttribute($unityExportSet, $UnityFbxFilePathAttr, "");
116229
storeAttribute($unityExportSet, $UnityFbxFileNameAttr, "");
230+
storeAttribute($unityExportSet, $UnityFbxNamespaceAttr, "");
231+
}
232+
233+
if(`namespaceInfo -cur -an` != $targetNamespace){
234+
namespace -set $targetNamespace;
117235
}
236+
file -import -type "FBX" -ignoreVersion -ra true -mergeNamespacesOnClash true -pr -importFrameRate true -importTimeRange "override" $filePathStr;
118237

119-
FBXImport -f $filePathStr;
238+
if(`namespaceInfo -cur -an` != $origNamespace){
239+
namespace -set $origNamespace;
240+
}
241+
242+
if ((!$isAnimFile || ($isAnimFile && $setCreated)) && $currentDir != ""){
243+
storeAttribute($unityExportSet, $UnityFbxFilePathAttr, $currentDir);
244+
}
120245

121-
if ((!$isAnimFile || ($isAnimFile && $setCreated)) && $tempPath != ""){
122-
storeAttribute($unityExportSet, $UnityFbxFilePathAttr, $tempPath);
246+
if ((!$isAnimFile || ($isAnimFile && $setCreated)) && $fileName != ""){
247+
storeAttribute($unityExportSet,$UnityFbxFileNameAttr,$fileName);
123248
}
124249

125-
if ((!$isAnimFile || ($isAnimFile && $setCreated)) && $tempName != ""){
126-
storeAttribute($unityExportSet,$UnityFbxFileNameAttr,$tempName);
250+
if($currentAnimDir != ""){
251+
storeAttribute($unityExportSet,$UnityFbxAnimFilePathAttr,$currentAnimDir);
127252
}
128253

129-
if($tempAnimPath != ""){
130-
storeAttribute($unityExportSet,$UnityFbxAnimFilePathAttr,$tempAnimPath);
254+
if($animFileName != ""){
255+
storeAttribute($unityExportSet,$UnityFbxAnimFileNameAttr,$animFileName);
131256
}
132257

133-
if($tempAnimName != ""){
134-
storeAttribute($unityExportSet,$UnityFbxAnimFileNameAttr,$tempAnimName);
258+
if($fileNameWithoutExt != ""){
259+
storeAttribute($unityExportSet, $UnityFbxNamespaceAttr, $targetNamespace);
135260
}
136261

137262
if (setExists($unityExportSet) == true){
@@ -198,11 +323,11 @@ global proc unityImport(){
198323
importFile($filePathStr);
199324
}
200325

201-
$tempPath = dirname($filePaths[0]);
326+
$currentDir = dirname($filePaths[0]);
202327
// Change Unity project if fbx is from a different Unity project.
203328
// Get the project based on the folder structure (i.e. folder above Assets)
204-
$head = dirname($tempPath);
205-
$tail = basename($tempPath, "");
329+
$head = dirname($currentDir);
330+
$tail = basename($currentDir, "");
206331
// Check that we are not at the root directory.
207332
// dirname($head) returns the last directory name in the path,
208333
// or head if head is the root directory.
@@ -238,8 +363,14 @@ proc exportSet(string $unitySet, int $exportAnim){
238363
global string $UnityFbxFileNameAttr;
239364
global string $UnityFbxAnimFilePathAttr;
240365
global string $UnityFbxAnimFileNameAttr;
366+
global string $UnityFbxNamespaceAttr;
241367

242368
string $unitySetContents[] = `listConnections $unitySet`;
369+
string $unityFbxNamespace = getAttribute($unitySet, $UnityFbxNamespaceAttr);
370+
371+
if(size($unitySetContents) > 0){
372+
$unityFbxNamespace = checkNamespaceNeedsUpdate($unitySet, $unityFbxNamespace, getObjectNamespace($unitySetContents[0]));
373+
}
243374

244375
string $animatedObjectSet = "";
245376
if($exportAnim){
@@ -264,16 +395,18 @@ proc exportSet(string $unitySet, int $exportAnim){
264395
$nameAttr = $UnityFbxAnimFileNameAttr;
265396
}
266397

267-
$unity_fbx_file_path = getAttribute($unitySet, $pathAttr);
268-
$unity_fbx_file_name = getAttribute($unitySet, $nameAttr);
398+
string $unityFbxFilePath = getAttribute($unitySet, $pathAttr);
399+
string $unityFbxFileName = getAttribute($unitySet, $nameAttr);
269400

270401
$strCmd = "";
271-
if ($unity_fbx_file_path != "" && $unity_fbx_file_name != ""){
272-
$strCmd = "file -force -options \"\" -typ \"FBX export\" -pr -es \"" + $unity_fbx_file_path + "/" + $unity_fbx_file_name + "\"";
402+
if ($unityFbxFilePath != "" && $unityFbxFileName != ""){
403+
// export selected, relative to given namespace
404+
string $exportFormat = "file -force -options \"\" -typ \"FBX export\" -relativeNamespace \"^1s\" -es \"^2s/^3s\"";
405+
$strCmd = `format -s $unityFbxNamespace -s $unityFbxFilePath -s $unityFbxFileName $exportFormat`;
273406
eval $strCmd;
274407
}
275408

276-
if(setExists($animatedObjectSet)){
409+
if(`objExists $animatedObjectSet`){
277410
delete $animatedObjectSet;
278411
}
279412
}

Assets/Integrations/Autodesk/maya/scripts/unityFbxImportSettings.mel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ FBXImportAxisConversionEnable -v true;
44
FBXImportCameras -v true;
55
FBXImportLights -v true;
66

7+
FBXImportSetTake -takeIndex -1;
8+
79
// Add and update animation
810
FBXImportMode -v merge;

0 commit comments

Comments
 (0)