@@ -115,7 +115,11 @@ public override void OnInspectorGUI() {
115
115
int result = EditorGUILayout . Popup ( exportSettings . selectedMayaApp , options ) ;
116
116
if ( result == options . Length - 1 ) {
117
117
string mayaPath = EditorUtility . OpenFilePanel ( "Select Maya Application" , ExportSettings . kDefaultAdskRoot , "exe" ) ;
118
- if ( ! string . IsNullOrEmpty ( mayaPath ) ) {
118
+
119
+ // check that the path is valid and references the maya executable
120
+ if ( ! string . IsNullOrEmpty ( mayaPath ) &&
121
+ Path . GetFileNameWithoutExtension ( mayaPath ) . ToLower ( ) . Equals ( "maya" )
122
+ ) {
119
123
ExportSettings . AddMayaOption ( mayaPath ) ;
120
124
Repaint ( ) ;
121
125
}
@@ -183,10 +187,10 @@ public class ExportSettings : ScriptableSingleton<ExportSettings>
183
187
[ SerializeField ]
184
188
string convertToModelSavePath ;
185
189
186
- // [SerializeField]
190
+ [ SerializeField ]
187
191
// List of paths in order that they appear in the option list
188
192
private System . Collections . Generic . List < string > mayaOptionPaths ;
189
- // [SerializeField]
193
+ [ SerializeField ]
190
194
// Dictionary of path -> display name
191
195
private System . Collections . Generic . Dictionary < string , string > mayaAppOptions ;
192
196
@@ -200,15 +204,49 @@ protected override void LoadDefaults()
200
204
mayaAppOptions = null ;
201
205
}
202
206
207
+ /// <summary>
208
+ /// Increments the name if there is a duplicate in MayaAppOptions dictionary.
209
+ /// </summary>
210
+ /// <returns>The unique name.</returns>
211
+ /// <param name="name">Name.</param>
212
+ private static string GetUniqueName ( string name ) {
213
+ if ( ! instance . mayaAppOptions . ContainsValue ( name ) ) {
214
+ return name ;
215
+ }
216
+ var format = "{1} ({0})" ;
217
+ int index = 1 ;
218
+ // try extracting the current index from the name and incrementing it
219
+ var result = System . Text . RegularExpressions . Regex . Match ( name , @"\((?<number>\d+?)\)$" ) ;
220
+ if ( result != null ) {
221
+ var number = result . Groups [ "number" ] . Value ;
222
+ int tempIndex ;
223
+ if ( int . TryParse ( number , out tempIndex ) ) {
224
+ var indexOfNumber = name . LastIndexOf ( number ) ;
225
+ format = name . Remove ( indexOfNumber , number . Length ) . Insert ( indexOfNumber , "{0}" ) ;
226
+ index = tempIndex + 1 ;
227
+ }
228
+ }
229
+
230
+ string uniqueName = null ;
231
+ do {
232
+ uniqueName = string . Format ( format , index , name ) ;
233
+ index ++ ;
234
+ } while ( File . Exists ( uniqueName ) ) ;
235
+
236
+ return uniqueName ;
237
+ }
238
+
203
239
/// <summary>
204
240
/// Find Maya installations at default install path.
205
241
/// Add results to given dictionary.
206
242
///
207
243
/// If MAYA_LOCATION is set, add this to the list as well.
208
244
/// </summary>
209
- private static System . Collections . Generic . Dictionary < string , string > FindMayaInstalls ( ) {
210
- System . Collections . Generic . Dictionary < string , string > mayaAppOptions =
211
- new System . Collections . Generic . Dictionary < string , string > ( ) ;
245
+ private static void FindMayaInstalls ( ) {
246
+ if ( instance . mayaAppOptions == null ) {
247
+ instance . mayaAppOptions = new System . Collections . Generic . Dictionary < string , string > ( ) ;
248
+ }
249
+ var mayaAppOptions = instance . mayaAppOptions ;
212
250
213
251
// If the location is given by the environment, use it.
214
252
var location = System . Environment . GetEnvironmentVariable ( "MAYA_LOCATION" ) ;
@@ -231,9 +269,8 @@ private static System.Collections.Generic.Dictionary<string, string> FindMayaIns
231
269
if ( product . StartsWith ( "mayalt" , StringComparison . InvariantCultureIgnoreCase ) ) {
232
270
continue ;
233
271
}
234
- mayaAppOptions . Add ( GetMayaExePath ( productDir . FullName . Replace ( "\\ " , "/" ) ) , product ) ;
272
+ mayaAppOptions . Add ( GetMayaExePath ( productDir . FullName . Replace ( "\\ " , "/" ) ) , GetUniqueName ( product ) ) ;
235
273
}
236
- return mayaAppOptions ;
237
274
}
238
275
239
276
/// <summary>
@@ -264,7 +301,7 @@ private static string GetMayaExePath(string location)
264
301
265
302
public static GUIContent [ ] GetMayaOptions ( ) {
266
303
if ( instance . mayaAppOptions == null ) {
267
- instance . mayaAppOptions = FindMayaInstalls ( ) ;
304
+ FindMayaInstalls ( ) ;
268
305
instance . mayaOptionPaths = new System . Collections . Generic . List < string > ( ) ;
269
306
foreach ( var key in instance . mayaAppOptions . Keys ) {
270
307
instance . mayaOptionPaths . Add ( key ) ;
@@ -288,11 +325,42 @@ public static void AddMayaOption(string newOption){
288
325
instance . selectedMayaApp = instance . mayaOptionPaths . IndexOf ( newOption ) ;
289
326
return ;
290
327
}
291
- mayaAppOptions . Add ( newOption , "Custom Maya Location" ) ;
328
+
329
+ // get the version
330
+ var version = AskMayaVersion ( newOption ) ;
331
+ mayaAppOptions . Add ( newOption , GetUniqueName ( "Maya" + version ) ) ;
292
332
instance . mayaOptionPaths . Add ( newOption ) ;
293
333
instance . selectedMayaApp = instance . mayaOptionPaths . Count - 1 ;
294
334
}
295
335
336
+ /// <summary>
337
+ /// Ask the version number by running maya.
338
+ /// </summary>
339
+ static string AskMayaVersion ( string exePath ) {
340
+ System . Diagnostics . Process myProcess = new System . Diagnostics . Process ( ) ;
341
+ myProcess . StartInfo . FileName = exePath ;
342
+ myProcess . StartInfo . WindowStyle = System . Diagnostics . ProcessWindowStyle . Hidden ;
343
+ myProcess . StartInfo . CreateNoWindow = true ;
344
+ myProcess . StartInfo . UseShellExecute = false ;
345
+ myProcess . StartInfo . RedirectStandardOutput = true ;
346
+ myProcess . StartInfo . Arguments = "-v" ;
347
+ myProcess . EnableRaisingEvents = true ;
348
+ myProcess . Start ( ) ;
349
+ string resultString = myProcess . StandardOutput . ReadToEnd ( ) ;
350
+ myProcess . WaitForExit ( ) ;
351
+
352
+ // Output is like: Maya 2018, Cut Number 201706261615
353
+ // We want the stuff after 'Maya ' and before the comma.
354
+ // TODO: less brittle! Consider also the mel command "about -version".
355
+ var commaIndex = resultString . IndexOf ( ',' ) ;
356
+ return resultString . Substring ( 0 , commaIndex ) . Substring ( "Maya " . Length ) ;
357
+ }
358
+
359
+ public static string GetSelectedMayaPath ( )
360
+ {
361
+ return instance . mayaOptionPaths [ instance . selectedMayaApp ] ;
362
+ }
363
+
296
364
public static string GetTurnTableSceneName ( ) {
297
365
if ( instance . turntableScene ) {
298
366
return instance . turntableScene . name ;
0 commit comments