Skip to content

Commit c6408dc

Browse files
authored
Merge pull request #297 from Unity-Technologies/UNI-32843-allow-multi-file-import-maya
Uni 32843 allow multi file import maya
2 parents b734a17 + dc9fcca commit c6408dc

File tree

1 file changed

+109
-64
lines changed

1 file changed

+109
-64
lines changed

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

Lines changed: 109 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
global string $UnityExportSet = "UnityFbxExportSet";
2-
global string $UnityFbxFilePathAttr = "unityFbxFilePath";
3-
global string $UnityFbxFileNameAttr = "unityFbxFileName";
1+
global string $UnityExportSets[];
2+
global string $UnityFbxFilePathAttr = "unityFbxModelFilePath";
3+
global string $UnityFbxFileNameAttr = "unityFbxModelFileName";
4+
global string $UnityFbxAnimFilePathAttr = "unityFbxAnimFilePath";
5+
global string $UnityFbxAnimFileNameAttr = "unityFbxAnimFileName";
6+
global string $UnityExportSetNameFormat = "^1s_UnityExportSet";
47

58
global proc unityRemoveNativeMenuOnLoad(){
69
$removeSendToUnityMenu = `optionVar -q "UnityFbxForMaya_removeSendToUnityMenu"`;
@@ -67,6 +70,89 @@ proc int loadUnityPlugin(string $plugin){
6770
return true;
6871
};
6972

73+
proc importFile(string $filePathStr){
74+
// get the global variables
75+
global string $UnityExportSets[];
76+
global string $UnityFbxFilePathAttr;
77+
global string $UnityFbxFileNameAttr;
78+
global string $UnityFbxAnimFilePathAttr;
79+
global string $UnityFbxAnimFileNameAttr;
80+
global string $UnityExportSetNameFormat;
81+
82+
$tempPath = dirname($filePathStr);
83+
$tempName = basename($filePathStr, "");
84+
$tempAnimPath = "";
85+
$tempAnimName = "";
86+
$nameWithoutExt = basename($filePathStr, ".fbx");
87+
88+
$isAnimFile = false;
89+
if(match("@", basename($filePathStr, ".fbx")) != ""){
90+
// import as animation
91+
$isAnimFile = true;
92+
$tempAnimPath = $tempPath;
93+
$tempAnimName = $tempName;
94+
95+
$nameWithoutExt = match("[^@]+", $nameWithoutExt);
96+
}
97+
$unityExportSet = `format -stringArg $nameWithoutExt $UnityExportSetNameFormat`;
98+
99+
// Gather everything that is in the scene
100+
$origItemsInScene = `ls -tr -o -r true`;
101+
102+
// Get or create the Unity Fbx Export Set
103+
$setCreated = false;
104+
if (!setExists($unityExportSet)){
105+
// couldn't find export set so create it
106+
sets -name $unityExportSet;
107+
$setCreated = true;
108+
}
109+
110+
// unlock set so we can add attributes to it
111+
lockNode -lock false $unityExportSet;
112+
113+
if(!$isAnimFile){
114+
// reset attribute values, in case import fails
115+
storeAttribute($unityExportSet, $UnityFbxFilePathAttr, "");
116+
storeAttribute($unityExportSet, $UnityFbxFileNameAttr, "");
117+
}
118+
119+
FBXImport -f $filePathStr;
120+
121+
if ((!$isAnimFile || ($isAnimFile && $setCreated)) && $tempPath != ""){
122+
storeAttribute($unityExportSet, $UnityFbxFilePathAttr, $tempPath);
123+
}
124+
125+
if ((!$isAnimFile || ($isAnimFile && $setCreated)) && $tempName != ""){
126+
storeAttribute($unityExportSet,$UnityFbxFileNameAttr,$tempName);
127+
}
128+
129+
if($tempAnimPath != ""){
130+
storeAttribute($unityExportSet,$UnityFbxAnimFilePathAttr,$tempAnimPath);
131+
}
132+
133+
if($tempAnimName != ""){
134+
storeAttribute($unityExportSet,$UnityFbxAnimFileNameAttr,$tempAnimName);
135+
}
136+
137+
if (setExists($unityExportSet) == true){
138+
// figure out what has been added after import
139+
$itemsInScene = `ls -tr -o -r true`;
140+
141+
$newItems = stringArrayRemove($origItemsInScene, $itemsInScene);
142+
143+
// add newly imported items to set
144+
if (size($newItems) > 0){
145+
sets -include $unityExportSet $newItems;
146+
}
147+
// lock set so it doesn't get deleted when empty
148+
lockNode -lock true $unityExportSet;
149+
150+
if(!stringArrayContains($unityExportSet, $UnityExportSets)){
151+
$UnityExportSets[size($UnityExportSets)] = $unityExportSet;
152+
}
153+
}
154+
}
155+
70156

71157
global proc int loadUnityDependencies(){
72158
// GamePipeline plugin 'SendToUnitySelection' command used in export
@@ -90,11 +176,6 @@ global proc int loadUnityDependencies(){
90176
}
91177

92178
global proc unityImport(){
93-
// get the global variables
94-
global string $UnityExportSet;
95-
global string $UnityFbxFilePathAttr;
96-
global string $UnityFbxFileNameAttr;
97-
98179
if(!loadUnityDependencies()){
99180
error("Failed to load Unity dependencies");
100181
return;
@@ -106,69 +187,33 @@ global proc unityImport(){
106187

107188
$unityProject = `optionVar -q "UnityProject"`;
108189

109-
$filePath = `fileDialog2 -dialogStyle 2 -caption "FBX Import" -dir ($unityProject + "/Assets") -fileFilter "*.fbx" -selectFileFilter "FBX" -fileMode 1`;
190+
$filePaths = `fileDialog2 -dialogStyle 2 -caption "FBX Import" -dir ($unityProject + "/Assets") -fileFilter "*.fbx" -selectFileFilter "FBX" -fileMode 4`;
110191

111192
// store path and filename
112-
if(size($filePath) <= 0){
193+
if(size($filePaths) <= 0){
113194
return;
114195
}
115-
$filePathStr = $filePath[0];
116-
$tempPath = dirname($filePathStr);
117-
$tempName = basename($filePathStr, "");
118-
119-
// Gather everything that is in the scene
120-
$origItemsInScene = `ls -tr -o -r true`;
121-
122-
// Get or create the Unity Fbx Export Set
123-
if (!setExists($UnityExportSet)){
124-
// couldn't find export set so create it
125-
sets -name $UnityExportSet;
126-
// unlock set so we can add attributes to it
127-
lockNode -lock false $UnityExportSet;
128-
}
129-
// reset attribute values, in case import fails
130-
storeAttribute($UnityExportSet, $UnityFbxFilePathAttr, "");
131-
storeAttribute($UnityExportSet, $UnityFbxFileNameAttr, "");
132-
133-
FBXImport -f $filePath;
134-
135-
if ($tempPath != ""){
136-
storeAttribute($UnityExportSet, $UnityFbxFilePathAttr, $tempPath);
137-
138-
// Change Unity project if fbx is from a different Unity project.
139-
// Get the project based on the folder structure (i.e. folder above Assets)
140-
$head = dirname($tempPath);
141-
$tail = basename($tempPath, "");
142-
// Check that we are not at the root directory.
143-
// dirname($head) returns the last directory name in the path,
144-
// or head if head is the root directory.
145-
while ($head != "" && dirname($head) != $head){
146-
if (`strcmp $tail "Assets"` == 0){
147-
// this is a valid Unity project, so set it
148-
optionVar -sv "UnityProject" $head;
149-
break;
150-
}
151-
$head = dirname($head);
152-
$tail = basename($head, "");
153-
}
154-
}
155-
156-
if ($tempName != ""){
157-
storeAttribute($UnityExportSet,$UnityFbxFileNameAttr,$tempName);
196+
for($i=0; $i<size($filePaths); ++$i){
197+
$filePathStr = $filePaths[$i];
198+
importFile($filePathStr);
158199
}
159200

160-
if (setExists($UnityExportSet) == true){
161-
// figure out what has been added after import
162-
$itemsInScene = `ls -tr -o -r true`;
163-
164-
$newItems = stringArrayRemove($origItemsInScene, $itemsInScene);
165-
166-
// add newly imported items to set
167-
if (size($newItems) > 0){
168-
sets -include $UnityExportSet $newItems;
201+
$tempPath = dirname($filePaths[0]);
202+
// Change Unity project if fbx is from a different Unity project.
203+
// Get the project based on the folder structure (i.e. folder above Assets)
204+
$head = dirname($tempPath);
205+
$tail = basename($tempPath, "");
206+
// Check that we are not at the root directory.
207+
// dirname($head) returns the last directory name in the path,
208+
// or head if head is the root directory.
209+
while ($head != "" && dirname($head) != $head){
210+
if (`strcmp $tail "Assets"` == 0){
211+
// this is a valid Unity project, so set it
212+
optionVar -sv "UnityProject" $head;
213+
break;
169214
}
170-
// lock set so it doesn't get deleted when empty
171-
lockNode -lock true $UnityExportSet;
215+
$head = dirname($head);
216+
$tail = basename($head, "");
172217
}
173218
}
174219

0 commit comments

Comments
 (0)