Skip to content

Commit 873665f

Browse files
committed
use selected maya install for installing integration
- also make sure that all names are unique in the dropdown - serialize the list and dictionary for the dropdown - get the version of any Maya install that is not found by default
1 parent 868deb3 commit 873665f

File tree

3 files changed

+79
-49
lines changed

3 files changed

+79
-49
lines changed

Assets/FbxExporters/Editor/FbxExportSettings.cs

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ public override void OnInspectorGUI() {
115115
int result = EditorGUILayout.Popup(exportSettings.selectedMayaApp, options);
116116
if (result == options.Length - 1) {
117117
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+
) {
119123
ExportSettings.AddMayaOption (mayaPath);
120124
Repaint ();
121125
}
@@ -183,10 +187,10 @@ public class ExportSettings : ScriptableSingleton<ExportSettings>
183187
[SerializeField]
184188
string convertToModelSavePath;
185189

186-
//[SerializeField]
190+
[SerializeField]
187191
// List of paths in order that they appear in the option list
188192
private System.Collections.Generic.List<string> mayaOptionPaths;
189-
//[SerializeField]
193+
[SerializeField]
190194
// Dictionary of path -> display name
191195
private System.Collections.Generic.Dictionary<string, string> mayaAppOptions;
192196

@@ -200,15 +204,49 @@ protected override void LoadDefaults()
200204
mayaAppOptions = null;
201205
}
202206

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+
203239
/// <summary>
204240
/// Find Maya installations at default install path.
205241
/// Add results to given dictionary.
206242
///
207243
/// If MAYA_LOCATION is set, add this to the list as well.
208244
/// </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;
212250

213251
// If the location is given by the environment, use it.
214252
var location = System.Environment.GetEnvironmentVariable ("MAYA_LOCATION");
@@ -231,9 +269,8 @@ private static System.Collections.Generic.Dictionary<string, string> FindMayaIns
231269
if (product.StartsWith("mayalt", StringComparison.InvariantCultureIgnoreCase)) {
232270
continue;
233271
}
234-
mayaAppOptions.Add (GetMayaExePath(productDir.FullName.Replace("\\","/")), product);
272+
mayaAppOptions.Add (GetMayaExePath(productDir.FullName.Replace("\\","/")), GetUniqueName(product));
235273
}
236-
return mayaAppOptions;
237274
}
238275

239276
/// <summary>
@@ -264,7 +301,7 @@ private static string GetMayaExePath(string location)
264301

265302
public static GUIContent[] GetMayaOptions(){
266303
if (instance.mayaAppOptions == null) {
267-
instance.mayaAppOptions = FindMayaInstalls ();
304+
FindMayaInstalls ();
268305
instance.mayaOptionPaths = new System.Collections.Generic.List<string> ();
269306
foreach (var key in instance.mayaAppOptions.Keys) {
270307
instance.mayaOptionPaths.Add (key);
@@ -288,11 +325,42 @@ public static void AddMayaOption(string newOption){
288325
instance.selectedMayaApp = instance.mayaOptionPaths.IndexOf (newOption);
289326
return;
290327
}
291-
mayaAppOptions.Add (newOption, "Custom Maya Location");
328+
329+
// get the version
330+
var version = AskMayaVersion(newOption);
331+
mayaAppOptions.Add (newOption, GetUniqueName("Maya"+version));
292332
instance.mayaOptionPaths.Add (newOption);
293333
instance.selectedMayaApp = instance.mayaOptionPaths.Count - 1;
294334
}
295335

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+
296364
public static string GetTurnTableSceneName(){
297365
if (instance.turntableScene) {
298366
return instance.turntableScene.name;

Assets/FbxExporters/Editor/InstallIntegration.cs

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -334,46 +334,11 @@ public static bool InstallMaya(bool verbose = false)
334334

335335
class IntegrationsUI
336336
{
337-
/// <summary>
338-
/// The path where all the different versions of Maya are installed
339-
/// by default. Depends on the platform.
340-
/// </summary>
341-
public const string DefaultAdskRoot =
342-
#if UNITY_EDITOR_OSX
343-
"/Applications/Autodesk"
344-
#elif UNITY_EDITOR_LINUX
345-
"/usr/autodesk"
346-
#else // WINDOWS
347-
"C:/Program Files/Autodesk"
348-
#endif
349-
;
350-
351337
/// <summary>
352338
/// The path of the Maya executable.
353339
/// </summary>
354340
public static string GetMayaExe () {
355-
var location = EditorUtility.OpenFolderPanel ("Select Maya Root Folder", DefaultAdskRoot, "");
356-
if (string.IsNullOrEmpty(location)) {
357-
return null;
358-
}
359-
360-
#if UNITY_EDITOR_OSX
361-
// MAYA_LOCATION on mac is set by Autodesk to be the
362-
// Contents directory. But let's make it easier on people
363-
// and allow just having it be the app bundle or a
364-
// directory that holds the app bundle.
365-
if (location.EndsWith(".app/Contents")) {
366-
return location + "/MacOS/Maya";
367-
} else if (location.EndsWith(".app")) {
368-
return location + "/Contents/MacOS/Maya";
369-
} else {
370-
return location + "/Maya.app/Contents/MacOS/Maya";
371-
}
372-
#elif UNITY_EDITOR_LINUX
373-
return location + "/bin/maya";
374-
#else // WINDOWS
375-
return location + "/bin/maya.exe";
376-
#endif
341+
return FbxExporters.EditorTools.ExportSettings.GetSelectedMayaPath ();
377342
}
378343

379344
public static void InstallMayaIntegration ()

Assets/FbxExporters/Editor/UnitTests/IntegrationsTest.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ void LogNonEmptyString(string name, string str) {
2727

2828
[Test]
2929
public void BasicTest() {
30-
// Note: This test assumes that Maya is actually installed in a default location.
31-
Assert.IsTrue(Directory.Exists(Editor.IntegrationsUI.DefaultAdskRoot));
32-
3330
Assert.IsFalse(Editor.Integrations.IsHeadlessInstall());
3431

3532
LogNonEmptyString("module path", Editor.Integrations.GetModulePath());

0 commit comments

Comments
 (0)