Skip to content

Commit d2115cb

Browse files
committed
code review fixes + clean up
- moved attribute handling to two functions. - only create set when we are actually importing - if set already exists then remove all items from it - only add items to set if import was successful
1 parent 610e538 commit d2115cb

File tree

1 file changed

+45
-27
lines changed
  • Assets/Integrations/Autodesk/maya2017/scripts/unityOneClick

1 file changed

+45
-27
lines changed

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

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ def loadUnityFbxExportSettings(self):
7575
maya.mel.eval(contents)
7676
return True
7777

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)
7890

7991
class importCmd(BaseCommand):
8092
"""
@@ -94,6 +106,9 @@ def __init__(self):
94106
# temporarily store the path and name of the imported FBX
95107
self._tempPath = None
96108
self._tempName = None
109+
110+
# temporarily store items in scene before import
111+
self._origItemsInScene = []
97112

98113
@classmethod
99114
def creator(cls):
@@ -113,43 +128,48 @@ def beforeImport(self, retCode, file, clientData):
113128
self._tempPath = file.resolvedPath()
114129
self._tempName = file.resolvedName()
115130

131+
# Gather everything that is in the scene
132+
self._origItemsInScene = maya.cmds.ls(tr=True, o=True, r=True)
133+
134+
# Get or create the Unity Fbx Export Set
135+
if not self.setExists(self._exportSet):
136+
# couldn't find export set so create it
137+
maya.cmds.sets(name=self._exportSet)
138+
else:
139+
# remove all items from set
140+
maya.cmds.sets(clear=self._exportSet)
141+
142+
# reset attribute values, in case import fails
143+
self.storeAttribute(self._exportSet, self._unityFbxFilePathAttr, "")
144+
self.storeAttribute(self._exportSet, self._unityFbxFileNameAttr, "")
145+
116146
def afterImport(self, *args, **kwargs):
117147
if self._tempPath:
118-
if not maya.mel.eval('attributeExists "{0}" "{1}"'.format(self._unityFbxFilePathAttr, self._exportSet)):
119-
maya.cmds.addAttr(self._exportSet, shortName=self._unityFbxFilePathAttr, storable=True, dataType="string")
120-
maya.cmds.setAttr("{0}.{1}".format(self._exportSet, self._unityFbxFilePathAttr), self._tempPath, type="string")
148+
self.storeAttribute(self._exportSet, self._unityFbxFilePathAttr, self._tempPath)
121149
if self._tempName:
122-
if not maya.mel.eval('attributeExists "{0}" "{1}"'.format(self._unityFbxFileNameAttr, self._exportSet)):
123-
maya.cmds.addAttr(self._exportSet, shortName=self._unityFbxFileNameAttr, storable=True, dataType="string")
124-
maya.cmds.setAttr("{0}.{1}".format(self._exportSet, self._unityFbxFileNameAttr), self._tempName, type="string")
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)
125159

126160
def doIt(self, args):
127161
self.loadDependencies()
128162

129163
self._tempPath = None
130164
self._tempName = None
131-
132-
# Get or create the Unity Fbx Export Set
133-
allSets = maya.cmds.listSets(allSets=True)
134-
if not self._exportSet in allSets:
135-
# couldn't find export set so create it
136-
maya.cmds.sets(name=self._exportSet)
137-
165+
self._origItemsInScene = []
166+
138167
callbackId = OpenMaya.MSceneMessage.addCheckFileCallback(OpenMaya.MSceneMessage.kBeforeImportCheck, self.beforeImport)
139168
callbackId2 = OpenMaya.MSceneMessage.addCallback(OpenMaya.MSceneMessage.kAfterImport, self.afterImport)
140169

141-
# Gather everything that is in the scene
142-
origItemsInScene = maya.cmds.ls(tr=True, o=True, r=True)
143-
144170
strCmd = 'Import'
145171
self.displayDebug('doIt {0}'.format(strCmd))
146-
result = maya.cmds.Import()
147-
148-
# figure out what has been added after import
149-
itemsInScene = maya.cmds.ls(tr=True, o=True, r=True)
150-
newItems = list(set(itemsInScene) - set(origItemsInScene))
151-
152-
maya.cmds.sets(newItems, add=self._exportSet)
172+
maya.cmds.Import()
153173

154174
OpenMaya.MMessage.removeCallback(callbackId)
155175
OpenMaya.MMessage.removeCallback(callbackId2)
@@ -276,10 +296,8 @@ def doIt(self, args):
276296
if not self.loadUnityFbxExportSettings():
277297
return
278298

279-
if maya.mel.eval('attributeExists "{0}" "{1}"'.format(self._unityFbxFilePathAttr, self._exportSet)) and \
280-
maya.mel.eval('attributeExists "{0}" "{1}"'.format(self._unityFbxFileNameAttr, self._exportSet)):
281-
unity_fbx_file_path = maya.cmds.getAttr("{0}.{1}".format(self._exportSet, self._unityFbxFilePathAttr))
282-
unity_fbx_file_name = maya.cmds.getAttr("{0}.{1}".format(self._exportSet, self._unityFbxFileNameAttr))
299+
unity_fbx_file_path = self.getAttribute(self._exportSet, self._unityFbxFilePathAttr)
300+
unity_fbx_file_name = self.getAttribute(self._exportSet, self._unityFbxFileNameAttr)
283301

284302
if unity_fbx_file_path and unity_fbx_file_name:
285303
strCmd = r'file -force -options "" -typ "FBX export" -pr -es "{0}{1}"'.format(unity_fbx_file_path, unity_fbx_file_name);

0 commit comments

Comments
 (0)