|
1 | 1 | global proc unityImport(){
|
| 2 | + global string $UnityExportSet = "UnityFbxExportSet"; |
| 3 | + global string $UnityFbxFilePathAttr = "unityFbxFilePath"; |
| 4 | + global string $UnityFbxFileNameAttr = "unityFbxFileName"; |
| 5 | + |
2 | 6 | loadUnityDependencies;
|
3 | 7 |
|
4 |
| - // set Unity project as the current workspace |
5 |
| - $currWorkspace = `workspace -o -q`; |
6 | 8 | $unityProject = `optionVar -q "UnityProject"`;
|
7 | 9 |
|
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`; |
11 | 11 |
|
12 |
| - $tempPath = ""; |
13 |
| - $tempName = ""; |
14 |
| - $origItemsInScene = ""; |
15 | 12 |
|
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, ""); |
18 | 34 |
|
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 | + } |
20 | 57 |
|
21 |
| - if ($currWorkspace != "") { |
22 |
| - workspace -o $currWorkspace; |
| 58 | + if ($tempName != ""){ |
| 59 | + storeAttribute($UnityExportSet,$UnityFbxFileNameAttr,$tempName); |
23 | 60 | }
|
24 | 61 |
|
| 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 | + } |
25 | 75 | }
|
26 | 76 |
|
| 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 | +} |
27 | 88 |
|
28 | 89 | global proc int loadUnityPlugin(string $plugin){
|
29 | 90 | if (`pluginInfo -q -loaded $plugin` == false){
|
|
0 commit comments