Skip to content

Commit 83aa53b

Browse files
committed
create + populate export set on import
add implementation for creating + populating the export set when importing. Also add the path and name attributes to the set. Use fileDialog2 to create the import dialog instead of the regular import dialog. This prevents us from having to set the workspace as well as using callbacks on import
1 parent 01b8178 commit 83aa53b

File tree

1 file changed

+74
-13
lines changed

1 file changed

+74
-13
lines changed

Assets/Integrations/Autodesk/mayalt/scripts/unityImport.mel

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,90 @@
11
global proc unityImport(){
2+
global string $UnityExportSet = "UnityFbxExportSet";
3+
global string $UnityFbxFilePathAttr = "unityFbxFilePath";
4+
global string $UnityFbxFileNameAttr = "unityFbxFileName";
5+
26
loadUnityDependencies;
37

4-
// set Unity project as the current workspace
5-
$currWorkspace = `workspace -o -q`;
68
$unityProject = `optionVar -q "UnityProject"`;
79

8-
if ($unityProject != ""){
9-
workspace -o $unityProject;
10-
}
10+
$filePath = `fileDialog2 -dialogStyle 2 -caption "FBX Import" -dir ($unityProject + "/Assets") -fileFilter "*.fbx" -selectFileFilter "FBX" -fileMode 1`;
1111

12-
$tempPath = "";
13-
$tempName = "";
14-
$origItemsInScene = "";
1512

16-
$callbackId = -1;
17-
$callbackId2 = -1;
13+
// store path and filename
14+
if(size($filePath) <= 0){
15+
return;
16+
}
17+
$filePathStr = $filePath[0];
18+
$tempPath = dirname($filePathStr);
19+
$tempName = basename($filePathStr, "");
20+
21+
// Gather everything that is in the scene
22+
$origItemsInScene = `ls -tr -o -r true`;
23+
24+
// Get or create the Unity Fbx Export Set
25+
if (!setExists($UnityExportSet)){
26+
// couldn't find export set so create it
27+
sets -name $UnityExportSet;
28+
// unlock set so we can add attributes to it
29+
lockNode -lock false $UnityExportSet;
30+
}
31+
// reset attribute values, in case import fails
32+
storeAttribute($UnityExportSet, $UnityFbxFilePathAttr, "");
33+
storeAttribute($UnityExportSet, $UnityFbxFileNameAttr, "");
1834

19-
Import;
35+
FBXImport -f $filePath;
36+
37+
if ($tempPath != ""){
38+
storeAttribute($UnityExportSet, $UnityFbxFilePathAttr, $tempPath);
39+
40+
// Change Unity project if fbx is from a different Unity project.
41+
// Get the project based on the folder structure (i.e. folder above Assets)
42+
$head = dirname($tempPath);
43+
$tail = basename($tempPath, "");
44+
// Check that we are not at the root directory.
45+
// dirname($head) returns the last directory name in the path,
46+
// or head if head is the root directory.
47+
while ($head != "" && dirname($head) != $head){
48+
if (`strcmp $tail "Assets"` == 0){
49+
// this is a valid Unity project, so set it
50+
optionVar -sv "UnityProject" $head;
51+
break;
52+
}
53+
$head = dirname($head);
54+
$tail = basename($head, "");
55+
}
56+
}
2057

21-
if ($currWorkspace != "") {
22-
workspace -o $currWorkspace;
58+
if ($tempName != ""){
59+
storeAttribute($UnityExportSet,$UnityFbxFileNameAttr,$tempName);
2360
}
2461

62+
if (setExists($UnityExportSet) == true){
63+
// figure out what has been added after import
64+
$itemsInScene = `ls -tr -o -r true`;
65+
66+
$newItems = stringArrayRemove($origItemsInScene, $itemsInScene);
67+
68+
// add newly imported items to set
69+
if (size($newItems) > 0){
70+
sets -include $UnityExportSet $newItems;
71+
}
72+
// lock set so it doesn't get deleted when empty
73+
lockNode -lock true $UnityExportSet;
74+
}
2575
}
2676

77+
global proc storeAttribute(string $node, string $attr, string $attrValue){
78+
$attrType="string";
79+
if (!attributeExists($attr, $node)){
80+
addAttr -shortName $attr -storable true -dataType $attrType $node;
81+
}
82+
setAttr ($node+"."+$attr) -type $attrType $attrValue;
83+
}
84+
85+
global proc int setExists(string $setName){
86+
return stringArrayContains($setName, `listSets -allSets`);
87+
}
2788

2889
global proc int loadUnityPlugin(string $plugin){
2990
if (`pluginInfo -q -loaded $plugin` == false){

0 commit comments

Comments
 (0)