Mayapy from C# exe, wrong directory/version

Loading mayapy from C# exe loads mayapy 2008 and not mayapy 2020.
Won’t load custom scripts path for userSetup.py

TLDR:
I have a C# exe that loads mayapy when you press a button (and then runs python scripts).
It loads:
C:\Program Files\Autodesk\Maya2008\bin\mayapy.exe
instead of:
C:\Program Files\Autodesk\Maya2020\bin\mayapy.exe.

I also can’t seem to get it to load my custom python script path ("\NetworkLocation\SCRIPTS\maya\python") which would be where the userSetup.py is located.

I am nearly certain the failure of the script is because of Maya2008 and not because of the script path though, so that’s the thing I’m really looking to resolve. I know for a fact that particular python script fails when called in Maya2008 (because of Py2.5), which explains why it fails on import.


C# exe:
My C# script executes this when you press a button:
(yes this currently is attempting to set the variables twice because I can’t yet get it to work at all)

string mayaVersion = "2020";

// adds the Maya2020 bin to the PATH
// HOWEVER when I test it in the python script, the Maya2020 value is not there
// when i test right after adding it here in C# it succeeds
// I tried adding the Env Var manually to Windows but it doesn't help
var scope = EnvironmentVariableTarget.Machine;
string oldPathValue = Environment.GetEnvironmentVariable("PATH", scope);
string newPathValue = oldPathValue;
string mayaCurrentVersionPath = @"C:\Program Files\Autodesk\Maya" + mayaVersion + @"\bin";
if (!(oldPathValue.Contains(mayaCurrentVersionPath)))
{
   newPathValue = mayaCurrentVersionPath + ";" + oldPathValue;
}
newPathValue = newPathValue.Replace(";;", ";").Replace("/", "\\");
Environment.SetEnvironmentVariable("PATH", newPathValue, scope);

// set environment variables
Environment.SetEnvironmentVariable("PYTHONPATH", @"\\NetworkLocation\SCRIPTS\maya\startup;\\NetworkLocation\3D\maya\maya"+mayaVersion+@"\scripts\studio_scripts;\\NetworkLocation\SCRIPTS\maya\python");
Environment.SetEnvironmentVariable("STUDIO_SCRIPTS_PATH", @"\\NetworkLocation\SCRIPTS\maya");
Environment.SetEnvironmentVariable("MAYA_MODULE_PATH", @"\\NetworkLocation\3D\maya\maya"+mayaVersion+@"\modules\windows");

// set startInfo for Process
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Program Files\Autodesk\Maya"+mayaVersion+@"\bin\mayapy.exe");
startInfo.FileName = "mayapy.exe";
startInfo.WorkingDirectory = @"C:\Program Files\Autodesk\Maya"+mayaVersion+@"\bin";

startInfo.EnvironmentVariables["PYTHONPATH"] = @"\\NetworkLocation\SCRIPTS\maya\startup;\\NetworkLocation\3D\maya\maya"+mayaVersion+@"\scripts\studio_scripts;\\NetworkLocation\SCRIPTS\maya\python";
startInfo.EnvironmentVariables["STUDIO_SCRIPTS_PATH"] = @"\\NetworkLocation\SCRIPTS\maya";
startInfo.EnvironmentVariables["MAYA_MODULE_PATH"] = @"\\NetworkLocation\3D\maya\maya"+mayaVersion+@"\modules\windows";

string pyScript = @"\\NetworkLocation\SCRIPTS\maya\python\subDir\myPythonScript.py";

startInfo.Arguments = "\"" + pyScript + "\" \"" + argOne+ "\" \"" + argTwo+ "\"";

startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;

// start up the Process logging results and errors
var errors = "";
var results = "";
using (var process = Process.Start(startInfo))
{
    errors = process.StandardError.ReadToEnd();
    results = process.StandardOutput.ReadToEnd();
}
Console.WriteLine("ERRORS:");
Console.WriteLine(errors);
Console.WriteLine();
Console.WriteLine("Results:");
Console.WriteLine(results);

The myPythonScript.py file is attempting to load maya.standalone but since mayapy is from 2008 its loading good old Maya2008 as well.

In addition, its not registering the custom python path ("\NetworkLocation\SCRIPTS\maya\python") which is where the userSetup.py file is which is necessary to setting up the instance. I’m currently trying to set that path both through the PYTHONPATH env variable and by using sys.path.append in the py file below.

import os
import sys
import maya.standalone

def writeLog(str):
    print(str)
    f.write(str)
    f.flush()

f = open(r"C:\Temp\processLog.log", "w")

# this is just a check of the paths
writeLog(str(os.getenv("PATH"))+"\n")

# this sets where the userSetup.py is and sets up where
# the scripts are read from including the "subDir" below
sys.path.append(r'\\NetworkLocation\SCRIPTS\maya\python')

import maya.cmds as cmds


if __name__ == "__main__":
    # initialize maya
    maya.standalone.initialize(name='python')

    # this is just a check of the paths
    writeLog(str(os.getenv("PATH"))+"\n")

    # grab the arguments passed in from C# exe
    valueOne = sys.argv[1]
    valueTwo = [str(sys.argv[2])]

    # import the script to run and run it
    writeLog("Before crash.\n")
    import subDir.anotherScript as anotherScript
    writeLog("After crash.\n")
    result = anotherScript.mainFunction( valueOne, valueTwo )
    
    writeLog(str(result)+"\n")

    # close out of everything
    maya.standalone.uninitialize()
    f.close()
    sys.exit(result)

I’ve found one other thing that confuses me. I’m printing the “PATH” env in 2 spots above in the python script and get 2 completely different results.

Any help or insight would be greatly appreciated!

So I’ve found a solution to launching the version of mayapy I’d like. Where I’ve SetEnvironmentVariable at the top of the C# script, I am now getting the PATH variable and removing all instance that include Maya, and then adding in the “C:\Program Files\Autodesk\Maya2023\bin” for 2023 or 2020 or whichever version I’d want.

That said I still have 2 issues:

  1. Nothing outputs to the Command Window, so I must be missing something necessary to see the output.

  2. If I disable the maya.standalone line and call the rest of the script it lets me finish execution of the script (or at least a cut down version for testing). When i have maya.standalone enabled though and try to execute a scrip that requires an import, it fails at that import, as if it can’t find the module even though the scripts directory is listing the correct python scripts root. Any idea why maya.standalone would fail on import? My assumption is that it would only fail if it couldn’t find the import module meaning it couldn’t import. But is there any other reason? Because when I query the scripts directory it does list the root folder for the module.