@@ -3,6 +3,7 @@ global string $UnityFbxFilePathAttr = "unityFbxModelFilePath";
3
3
global string $UnityFbxFileNameAttr = "unityFbxModelFileName";
4
4
global string $UnityFbxAnimFilePathAttr = "unityFbxAnimFilePath";
5
5
global string $UnityFbxAnimFileNameAttr = "unityFbxAnimFileName";
6
+ global string $UnityFbxNamespaceAttr = "unityFbxNamespace";
6
7
global string $UnityExportSetNameFormat = "^1s_UnityExportSet";
7
8
8
9
global proc unityRemoveNativeMenuOnLoad(){
@@ -70,41 +71,153 @@ proc int loadUnityPlugin(string $plugin){
70
71
return true;
71
72
};
72
73
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
+
73
132
proc importFile(string $filePathStr){
74
133
// get the global variables
75
134
global string $UnityExportSets[];
76
135
global string $UnityFbxFilePathAttr;
77
136
global string $UnityFbxFileNameAttr;
78
137
global string $UnityFbxAnimFilePathAttr;
79
138
global string $UnityFbxAnimFileNameAttr;
139
+ global string $UnityFbxNamespaceAttr;
80
140
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");
87
147
88
148
$isAnimFile = false;
89
149
if(match("@", basename($filePathStr, ".fbx")) != ""){
90
150
// import as animation
91
151
$isAnimFile = true;
92
- $tempAnimPath = $tempPath ;
93
- $tempAnimName = $tempName ;
152
+ $currentAnimDir = $currentDir ;
153
+ $animFileName = $fileName ;
94
154
95
- $nameWithoutExt = match("[^@]+", $nameWithoutExt );
155
+ $fileNameWithoutExt = match("[^@]+", $fileNameWithoutExt );
96
156
}
97
- $unityExportSet = `format -stringArg $nameWithoutExt $UnityExportSetNameFormat`;
157
+ $unityExportSet = `format -stringArg $fileNameWithoutExt $UnityExportSetNameFormat`;
98
158
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
+
99
198
// Gather everything that is in the scene
100
199
$origItemsInScene = `ls -tr -o -r true`;
101
200
102
201
// Get or create the Unity Fbx Export Set
103
202
$setCreated = false;
104
203
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
+
105
214
// couldn't find export set so create it
106
215
sets -name $unityExportSet;
107
216
$setCreated = true;
217
+
218
+ if(size($origSelection) > 0){
219
+ select -r $origSelection;
220
+ }
108
221
}
109
222
110
223
// unlock set so we can add attributes to it
@@ -114,24 +227,36 @@ proc importFile(string $filePathStr){
114
227
// reset attribute values, in case import fails
115
228
storeAttribute($unityExportSet, $UnityFbxFilePathAttr, "");
116
229
storeAttribute($unityExportSet, $UnityFbxFileNameAttr, "");
230
+ storeAttribute($unityExportSet, $UnityFbxNamespaceAttr, "");
231
+ }
232
+
233
+ if(`namespaceInfo -cur -an` != $targetNamespace){
234
+ namespace -set $targetNamespace;
117
235
}
236
+ file -import -type "FBX" -ignoreVersion -ra true -mergeNamespacesOnClash true -pr -importFrameRate true -importTimeRange "override" $filePathStr;
118
237
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
+ }
120
245
121
- if ((!$isAnimFile || ($isAnimFile && $setCreated)) && $tempPath != ""){
122
- storeAttribute($unityExportSet, $UnityFbxFilePathAttr, $tempPath );
246
+ if ((!$isAnimFile || ($isAnimFile && $setCreated)) && $fileName != ""){
247
+ storeAttribute($unityExportSet,$UnityFbxFileNameAttr,$fileName );
123
248
}
124
249
125
- if ((!$isAnimFile || ($isAnimFile && $setCreated)) && $tempName != ""){
126
- storeAttribute($unityExportSet,$UnityFbxFileNameAttr,$tempName );
250
+ if($currentAnimDir != ""){
251
+ storeAttribute($unityExportSet,$UnityFbxAnimFilePathAttr,$currentAnimDir );
127
252
}
128
253
129
- if($tempAnimPath != ""){
130
- storeAttribute($unityExportSet,$UnityFbxAnimFilePathAttr,$tempAnimPath );
254
+ if($animFileName != ""){
255
+ storeAttribute($unityExportSet,$UnityFbxAnimFileNameAttr,$animFileName );
131
256
}
132
257
133
- if($tempAnimName != ""){
134
- storeAttribute($unityExportSet,$UnityFbxAnimFileNameAttr,$tempAnimName );
258
+ if($fileNameWithoutExt != ""){
259
+ storeAttribute($unityExportSet, $UnityFbxNamespaceAttr, $targetNamespace );
135
260
}
136
261
137
262
if (setExists($unityExportSet) == true){
@@ -198,11 +323,11 @@ global proc unityImport(){
198
323
importFile($filePathStr);
199
324
}
200
325
201
- $tempPath = dirname($filePaths[0]);
326
+ $currentDir = dirname($filePaths[0]);
202
327
// Change Unity project if fbx is from a different Unity project.
203
328
// 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 , "");
206
331
// Check that we are not at the root directory.
207
332
// dirname($head) returns the last directory name in the path,
208
333
// or head if head is the root directory.
@@ -238,8 +363,14 @@ proc exportSet(string $unitySet, int $exportAnim){
238
363
global string $UnityFbxFileNameAttr;
239
364
global string $UnityFbxAnimFilePathAttr;
240
365
global string $UnityFbxAnimFileNameAttr;
366
+ global string $UnityFbxNamespaceAttr;
241
367
242
368
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
+ }
243
374
244
375
string $animatedObjectSet = "";
245
376
if($exportAnim){
@@ -264,16 +395,18 @@ proc exportSet(string $unitySet, int $exportAnim){
264
395
$nameAttr = $UnityFbxAnimFileNameAttr;
265
396
}
266
397
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);
269
400
270
401
$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`;
273
406
eval $strCmd;
274
407
}
275
408
276
- if(setExists( $animatedObjectSet) ){
409
+ if(`objExists $animatedObjectSet` ){
277
410
delete $animatedObjectSet;
278
411
}
279
412
}
0 commit comments