Skip to content

Commit 680acb7

Browse files
authored
Merge pull request #98 from Unity-Technologies/UNI-22022-remember-export-settings
Uni 22022 remember export settings
2 parents 56a4072 + d2115cb commit 680acb7

File tree

5 files changed

+148
-19
lines changed

5 files changed

+148
-19
lines changed

Assets/FbxExporters/Editor/InstallIntegration.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Integrations
1414
private const string VERSION_TAG = "{Version}";
1515
private const string PROJECT_TAG = "{UnityProject}";
1616

17+
private const string FBX_EXPORT_SETTINGS_PATH = "Integrations/Autodesk/maya2017/scripts/unityFbxExportSettings.mel";
18+
1719
public class MayaException : System.Exception {
1820
public MayaException() { }
1921
public MayaException(string message) : base(message) { }
@@ -175,8 +177,8 @@ static string AskVersion(string exePath) {
175177
#endif
176178

177179
private static string MAYA_COMMANDS { get {
178-
return string.Format("configureUnityOneClick {0}{1}{0} {0}{2}{0} {0}{3}{0} {4}; scriptJob -idleEvent quit;",
179-
ESCAPED_QUOTE, GetProjectPath(), GetAppPath(), GetTempSavePath(), (IsHeadlessInstall()?1:0));
180+
return string.Format("configureUnityOneClick {0}{1}{0} {0}{2}{0} {0}{3}{0} {0}{4}{0} {5}; scriptJob -idleEvent quit;",
181+
ESCAPED_QUOTE, GetProjectPath(), GetAppPath(), GetTempSavePath(), GetExportSettingsPath(), (IsHeadlessInstall()?1:0));
180182
}}
181183
private static Char[] FIELD_SEPARATORS = new Char[] {':'};
182184

@@ -249,6 +251,16 @@ public static string GetTempSavePath()
249251
return System.IO.Path.Combine(Application.dataPath, FbxExporters.Review.TurnTable.TempSavePath).Replace("\\", "/");
250252
}
251253

254+
/// <summary>
255+
/// Gets the path to the export settings file.
256+
/// Returns an absolute path with forward slashes as path separators.
257+
/// </summary>
258+
/// <returns>The export settings path.</returns>
259+
public static string GetExportSettingsPath()
260+
{
261+
return Application.dataPath + '/' + FBX_EXPORT_SETTINGS_PATH;
262+
}
263+
252264
public static string GetPackageVersion()
253265
{
254266
string result = null;

Assets/FbxExporters/Editor/UnitTests/IntegrationsTest.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ public void BasicTest() {
4848
LogNonEmptyString("package path", Editor.Integrations.GetPackagePath());
4949
LogNonEmptyString("package version", Editor.Integrations.GetPackageVersion());
5050
LogNonEmptyString("temp path", Editor.Integrations.GetTempSavePath());
51+
LogNonEmptyString("export settings path", Editor.Integrations.GetExportSettingsPath ());
52+
53+
// test that the paths don't contain backslashes
54+
Assert.IsFalse(Editor.Integrations.GetAppPath().Contains("\\"));
55+
Assert.IsFalse(Editor.Integrations.GetProjectPath().Contains("\\"));
56+
Assert.IsFalse(Editor.Integrations.GetTempSavePath().Contains("\\"));
57+
Assert.IsFalse(Editor.Integrations.GetExportSettingsPath ().Contains("\\"));
5158
}
5259
}
5360
}

Assets/Integrations/Autodesk/maya2017/scripts/configureUnityOneClick.mel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
global proc configureUnityOneClick(string $unityProject, string $unityApp, string $unityTempSavePath, int $headless)
1+
global proc configureUnityOneClick(
2+
string $unityProject, string $unityApp,
3+
string $unityTempSavePath, string $unityFbxExportSettings, int $headless)
24
{
35
// configure plugin settings
46
optionVar -stringValue "UnityApp" $unityApp;
57
optionVar -stringValue "UnityProject" $unityProject;
68
optionVar -stringValue "UnityTempSavePath" $unityTempSavePath;
9+
optionVar -stringValue "UnityFbxExportSettings" $unityFbxExportSettings;
710
optionVar -intValue "UnityOneClick_Headless" $headless;
811

912
// configure auto load of plugin
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FBXResetExport;
2+
3+
// FBX file format
4+
FBXExportInAscii -v false;
5+
FBXExportFileVersion -v FBX201600;
6+
7+
// Geometry
8+
FBXExportSmoothMesh -v false;
9+
FBXExportInstances -v true;
10+
FBXExportReferencedAssetsContent -v false;
11+
12+
// Animation
13+
FBXExportAnimationOnly -v false;
14+
15+
FBXExportCameras -v false;
16+
FBXExportLights -v false;
17+
18+
FBXExportEmbeddedTextures -v false;
19+
20+
// Units
21+
FBXExportScaleFactor 1;
22+
FBXExportConvertUnitString cm;
23+
24+
// Axis Conversion
25+
FBXExportUpAxis y;

Assets/Integrations/Autodesk/maya2017/scripts/unityOneClick/commands.py

Lines changed: 98 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
ctypes.pythonapi.PyCObject_AsVoidPtr.restype = ctypes.c_void_p
3333
ctypes.pythonapi.PyCObject_AsVoidPtr.argtypes = [ctypes.py_object]
3434

35+
import os
36+
3537
class BaseCommand(OpenMayaMPx.MPxCommand, LoggerMixin):
3638
"""
3739
Base class for UnityOneClick Plugin Commands.
@@ -40,6 +42,8 @@ def __init__(self):
4042
OpenMayaMPx.MPxCommand.__init__(self)
4143
LoggerMixin.__init__(self)
4244
self._exportSet = "UnityFbxExportSet"
45+
self._unityFbxFilePathAttr = "unityFbxFilePath"
46+
self._unityFbxFileNameAttr = "unityFbxFileName"
4347

4448
def __del__(self):
4549
LoggerMixin.__del__(self)
@@ -54,7 +58,35 @@ def loadPlugin(self, plugin):
5458
return True
5559

5660
def loadDependencies(self):
57-
return self.loadPlugin('GamePipeline.mll')
61+
return self.loadPlugin('GamePipeline.mll') and self.loadPlugin('fbxmaya.mll')
62+
63+
def loadUnityFbxExportSettings(self):
64+
"""
65+
Load the Export Settings from file
66+
"""
67+
fileName = maya.cmds.optionVar(q="UnityFbxExportSettings")
68+
if not os.path.isfile(fileName):
69+
maya.cmds.error("Failed to find Unity Fbx Export Settings at: {0}".format(fileName))
70+
return False
71+
72+
with open(fileName) as f:
73+
contents = f.read()
74+
75+
maya.mel.eval(contents)
76+
return True
77+
78+
def storeAttribute(self, node, attr, attrValue, attrType="string"):
79+
if not maya.mel.eval('attributeExists "{0}" "{1}"'.format(attr, node)):
80+
maya.cmds.addAttr(node, shortName=attr, storable=True, dataType=attrType)
81+
maya.cmds.setAttr("{0}.{1}".format(node, attr), attrValue, type=attrType)
82+
83+
def getAttribute(self, node, attr):
84+
if maya.mel.eval('attributeExists "{0}" "{1}"'.format(attr, node)):
85+
return maya.cmds.getAttr("{0}.{1}".format(node, attr))
86+
return None
87+
88+
def setExists(self, setName):
89+
return setName in maya.cmds.listSets(allSets=True)
5890

5991
class importCmd(BaseCommand):
6092
"""
@@ -70,6 +102,13 @@ class importCmd(BaseCommand):
70102

71103
def __init__(self):
72104
super(self.__class__, self).__init__()
105+
106+
# temporarily store the path and name of the imported FBX
107+
self._tempPath = None
108+
self._tempName = None
109+
110+
# temporarily store items in scene before import
111+
self._origItemsInScene = []
73112

74113
@classmethod
75114
def creator(cls):
@@ -84,26 +123,57 @@ def syntaxCreator(cls):
84123
def scriptCmd(cls):
85124
return
86125

87-
def doIt(self, args):
88-
# Gather everything that is in the scene
89-
origItemsInScene = maya.cmds.ls(tr=True, o=True, r=True)
90-
91-
strCmd = 'Import'
92-
self.displayDebug('doIt {0}'.format(strCmd))
93-
result = maya.cmds.Import()
126+
def beforeImport(self, retCode, file, clientData):
127+
# store path and filename
128+
self._tempPath = file.resolvedPath()
129+
self._tempName = file.resolvedName()
94130

95-
# figure out what has been added after import
96-
itemsInScene = maya.cmds.ls(tr=True, o=True, r=True)
97-
newItems = list(set(itemsInScene) - set(origItemsInScene))
131+
# Gather everything that is in the scene
132+
self._origItemsInScene = maya.cmds.ls(tr=True, o=True, r=True)
98133

99134
# Get or create the Unity Fbx Export Set
100-
allSets = maya.cmds.listSets(allSets=True)
101-
if not self._exportSet in allSets:
135+
if not self.setExists(self._exportSet):
102136
# couldn't find export set so create it
103137
maya.cmds.sets(name=self._exportSet)
138+
else:
139+
# remove all items from set
140+
maya.cmds.sets(clear=self._exportSet)
104141

105-
maya.cmds.sets(newItems, add=self._exportSet)
142+
# reset attribute values, in case import fails
143+
self.storeAttribute(self._exportSet, self._unityFbxFilePathAttr, "")
144+
self.storeAttribute(self._exportSet, self._unityFbxFileNameAttr, "")
106145

146+
def afterImport(self, *args, **kwargs):
147+
if self._tempPath:
148+
self.storeAttribute(self._exportSet, self._unityFbxFilePathAttr, self._tempPath)
149+
if self._tempName:
150+
self.storeAttribute(self._exportSet, self._unityFbxFileNameAttr, self._tempName)
151+
152+
if self.setExists(self._exportSet):
153+
# figure out what has been added after import
154+
itemsInScene = maya.cmds.ls(tr=True, o=True, r=True)
155+
newItems = list(set(itemsInScene) - set(self._origItemsInScene))
156+
157+
# add newly imported items to set
158+
maya.cmds.sets(newItems, add=self._exportSet)
159+
160+
def doIt(self, args):
161+
self.loadDependencies()
162+
163+
self._tempPath = None
164+
self._tempName = None
165+
self._origItemsInScene = []
166+
167+
callbackId = OpenMaya.MSceneMessage.addCheckFileCallback(OpenMaya.MSceneMessage.kBeforeImportCheck, self.beforeImport)
168+
callbackId2 = OpenMaya.MSceneMessage.addCallback(OpenMaya.MSceneMessage.kAfterImport, self.afterImport)
169+
170+
strCmd = 'Import'
171+
self.displayDebug('doIt {0}'.format(strCmd))
172+
maya.cmds.Import()
173+
174+
OpenMaya.MMessage.removeCallback(callbackId)
175+
OpenMaya.MMessage.removeCallback(callbackId2)
176+
107177
@classmethod
108178
def invoke(cls):
109179
"""
@@ -147,9 +217,12 @@ def doIt(self, args):
147217
unityProjectPath = maya.cmds.optionVar(q='UnityProject')
148218
unityTempSavePath = maya.cmds.optionVar(q='UnityTempSavePath')
149219
unityCommand = "FbxExporters.Review.TurnTable.LastSavedModel"
220+
221+
if not self.loadUnityFbxExportSettings():
222+
return
150223

151224
# make sure the GamePipeline and fbxmaya plugins are loaded
152-
if self.loadDependencies() and self.loadPlugin('fbxmaya.mll'):
225+
if self.loadDependencies():
153226
# save fbx to Assets/_safe_to_delete/
154227
savePath = unityTempSavePath
155228
maya.cmds.sysFile(savePath, makeDir=True)
@@ -220,7 +293,16 @@ def doIt(self, args):
220293
if not self.loadDependencies():
221294
return
222295

223-
strCmd = 'SendToUnitySelection'
296+
if not self.loadUnityFbxExportSettings():
297+
return
298+
299+
unity_fbx_file_path = self.getAttribute(self._exportSet, self._unityFbxFilePathAttr)
300+
unity_fbx_file_name = self.getAttribute(self._exportSet, self._unityFbxFileNameAttr)
301+
302+
if unity_fbx_file_path and unity_fbx_file_name:
303+
strCmd = r'file -force -options "" -typ "FBX export" -pr -es "{0}{1}"'.format(unity_fbx_file_path, unity_fbx_file_name);
304+
else:
305+
strCmd = 'SendToUnitySelection'
224306
self.displayDebug('doIt {0}'.format(strCmd))
225307
maya.mel.eval(strCmd)
226308

0 commit comments

Comments
 (0)