@@ -75,6 +75,18 @@ def loadUnityFbxExportSettings(self):
75
75
maya .mel .eval (contents )
76
76
return True
77
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 )
78
90
79
91
class importCmd (BaseCommand ):
80
92
"""
@@ -94,6 +106,9 @@ def __init__(self):
94
106
# temporarily store the path and name of the imported FBX
95
107
self ._tempPath = None
96
108
self ._tempName = None
109
+
110
+ # temporarily store items in scene before import
111
+ self ._origItemsInScene = []
97
112
98
113
@classmethod
99
114
def creator (cls ):
@@ -113,43 +128,48 @@ def beforeImport(self, retCode, file, clientData):
113
128
self ._tempPath = file .resolvedPath ()
114
129
self ._tempName = file .resolvedName ()
115
130
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
+
116
146
def afterImport (self , * args , ** kwargs ):
117
147
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 )
121
149
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 )
125
159
126
160
def doIt (self , args ):
127
161
self .loadDependencies ()
128
162
129
163
self ._tempPath = None
130
164
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
+
138
167
callbackId = OpenMaya .MSceneMessage .addCheckFileCallback (OpenMaya .MSceneMessage .kBeforeImportCheck , self .beforeImport )
139
168
callbackId2 = OpenMaya .MSceneMessage .addCallback (OpenMaya .MSceneMessage .kAfterImport , self .afterImport )
140
169
141
- # Gather everything that is in the scene
142
- origItemsInScene = maya .cmds .ls (tr = True , o = True , r = True )
143
-
144
170
strCmd = 'Import'
145
171
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 ()
153
173
154
174
OpenMaya .MMessage .removeCallback (callbackId )
155
175
OpenMaya .MMessage .removeCallback (callbackId2 )
@@ -276,10 +296,8 @@ def doIt(self, args):
276
296
if not self .loadUnityFbxExportSettings ():
277
297
return
278
298
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 )
283
301
284
302
if unity_fbx_file_path and unity_fbx_file_name :
285
303
strCmd = r'file -force -options "" -typ "FBX export" -pr -es "{0}{1}"' .format (unity_fbx_file_path , unity_fbx_file_name );
0 commit comments