32
32
ctypes .pythonapi .PyCObject_AsVoidPtr .restype = ctypes .c_void_p
33
33
ctypes .pythonapi .PyCObject_AsVoidPtr .argtypes = [ctypes .py_object ]
34
34
35
+ import os
36
+
35
37
class BaseCommand (OpenMayaMPx .MPxCommand , LoggerMixin ):
36
38
"""
37
39
Base class for UnityOneClick Plugin Commands.
@@ -40,6 +42,8 @@ def __init__(self):
40
42
OpenMayaMPx .MPxCommand .__init__ (self )
41
43
LoggerMixin .__init__ (self )
42
44
self ._exportSet = "UnityFbxExportSet"
45
+ self ._unityFbxFilePathAttr = "unityFbxFilePath"
46
+ self ._unityFbxFileNameAttr = "unityFbxFileName"
43
47
44
48
def __del__ (self ):
45
49
LoggerMixin .__del__ (self )
@@ -54,7 +58,35 @@ def loadPlugin(self, plugin):
54
58
return True
55
59
56
60
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 )
58
90
59
91
class importCmd (BaseCommand ):
60
92
"""
@@ -70,6 +102,13 @@ class importCmd(BaseCommand):
70
102
71
103
def __init__ (self ):
72
104
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 = []
73
112
74
113
@classmethod
75
114
def creator (cls ):
@@ -84,26 +123,57 @@ def syntaxCreator(cls):
84
123
def scriptCmd (cls ):
85
124
return
86
125
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 ()
94
130
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 )
98
133
99
134
# 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 ):
102
136
# couldn't find export set so create it
103
137
maya .cmds .sets (name = self ._exportSet )
138
+ else :
139
+ # remove all items from set
140
+ maya .cmds .sets (clear = self ._exportSet )
104
141
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 , "" )
106
145
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
+
107
177
@classmethod
108
178
def invoke (cls ):
109
179
"""
@@ -147,9 +217,12 @@ def doIt(self, args):
147
217
unityProjectPath = maya .cmds .optionVar (q = 'UnityProject' )
148
218
unityTempSavePath = maya .cmds .optionVar (q = 'UnityTempSavePath' )
149
219
unityCommand = "FbxExporters.Review.TurnTable.LastSavedModel"
220
+
221
+ if not self .loadUnityFbxExportSettings ():
222
+ return
150
223
151
224
# make sure the GamePipeline and fbxmaya plugins are loaded
152
- if self .loadDependencies () and self . loadPlugin ( 'fbxmaya.mll' ) :
225
+ if self .loadDependencies ():
153
226
# save fbx to Assets/_safe_to_delete/
154
227
savePath = unityTempSavePath
155
228
maya .cmds .sysFile (savePath , makeDir = True )
@@ -220,7 +293,16 @@ def doIt(self, args):
220
293
if not self .loadDependencies ():
221
294
return
222
295
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'
224
306
self .displayDebug ('doIt {0}' .format (strCmd ))
225
307
maya .mel .eval (strCmd )
226
308
0 commit comments