Running a Maya Python Script from within a Mel script?

12,390 views
Skip to first unread message

sam.ma...@gmail.com

unread,
Dec 12, 2013, 2:10:25 PM12/12/13
to python_in...@googlegroups.com
I'd like to run a Maya Python Script (NOT a single Python command) from within my userSetup.mel script. The Python script is simple but does use some Maya-specific commands. The Pythong script for testing is:

#Python script:
import maya.cmds as cmds
Returned=cmds.fileDialog(mode=1, title="Save Layer/Pass list for Miranda:")

The closest I've gotten to running it from MEL is something like:

//Within the MEL Script:
python (exec("\\Server\MyFolder\SaveLPLFile.py"));


I've tried various cnfigurations of \\, \\\\, //, etc to accomodate the potential errors of escape characters but nothing seems to have any effect. The python/exec command appears to run without error (to the Maya console) but the actual script is not running because a cmds.fileDialog() never appears. Even when there is a typo in the filename the command appears to run.....apparently there is some long-standing bug in python that doesn't return an error message properly under some instances.

Any alternatives that might be a better method of running a Python script from within MEL?


Justin Israel

unread,
Dec 12, 2013, 2:56:38 PM12/12/13
to python_in...@googlegroups.com
exec (MEL)
"The string argument is a command which gets executed as a background process from the shell"

That would be launching your python script in its own process, outside of Maya, and most likely failing but you aren't checking the return code to know.

I think what you really want is the python "execfile"
"Read and execute a Python script from a file.
The globals and locals are dictionaries, defaulting to the current
globals and locals.  If only globals is given, locals defaults to it."

Also, just use forward slashes all the time, even on Windows

Does this work the way you expect?

    python("execfile('/Server/MyFolder/Save/LPLFile.py')")








--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/231755dc-700c-4961-bc54-1a214ac0fc75%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jesse Kretschmer

unread,
Dec 12, 2013, 4:37:54 PM12/12/13
to python_in...@googlegroups.com
I believe Justin hit the most important point regarding exec() in MEL versus exec() in Python versus execfile() in python. These commands are all quite different.

However, there are some major caveats to execfile(). Using it will mix your namespace with the currently running script. Perhaps this is the feature that you are relying on. Are you collecting the value of Returned after executing your script? If so, this sort of back-handed variable can lead to very confusing code and make it hard to debug in the future.

As an alternative, this is how I would design your code:
1. Skip the userSetup.mel file and instead use userSetup.py. Maya will execute every userSetup.py in the PYTHONPATH. You can still have your current userSetup.mel. Both will run.

2. You should then make sure your script, SaveLPLFile.py, is in the python path. This can be done through PYTHONPATH or a sys.path.append in the userSetup.py. 

3. Restructure your script to be used a module. It's common to wrap all the bare code in a function named main(). 

4. Call your new module from userSetup.py
import SaveLPLFile
SaveLPLFile.main()


I'd be happy to further discuss the program design if that's interesting to you.
Cheers,
Jesse



Jesse Kretschmer

unread,
Sep 3, 2015, 4:32:47 AM9/3/15
to dimitrij...@gmail.com, Python Programming for Autodesk Maya

The PYTHONPATH is a system environment variable. There are a few ways to change those and reach operating system is a bit different. It will only be read when the application is first launched.

The point of the python path is to prepare python to have access to the correct python libraries. You can get similar functionality with sys.path.append('/my/path')


On Thu, Sep 3, 2015, 10:05  <dimitrij...@gmail.com> wrote:
I have questions about PYTHONPATH, where is it by default? How can I change it and what I get by changing it?

dimitrij...@gmail.com

unread,
Sep 3, 2015, 6:41:11 AM9/3/15
to Python Programming for Autodesk Maya, je...@krets.com

Mahmoodreza Aarabi

unread,
Sep 3, 2015, 8:18:52 AM9/3/15
to python_in...@googlegroups.com
Hello
you can add it in this way and add extra path to the default pythonpath:

in windows: control pannel > System >  Advance System Settings > Advance (tab) > Environment Variables > New (button)
variable name: PYTHONPATH
variable value: c:\your\path;d:\your\path2;

good luck!

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Benjam901

unread,
Sep 4, 2015, 4:17:51 AM9/4/15
to Python Programming for Autodesk Maya, sam.ma...@gmail.com
Hello,

I have had to do this in a MEL script a couple of times, mainly when running Maya in batch mode.

In your Python script make sure you have a run function that calls all the functions you need for the script.

In your MEL script (make sure the script is available to be imported) and call in your MEL script:

python("import myscript");

python("
myscript.run()");


I have never looked into "execfile" as Justin suggested but this solution but this solution has worked for me in the past.

I hope it helps. 

-- Ben


On Thursday, December 12th, 2013 at. 20:10:25 UTC + 1 wrote sam.ma ... @ gmail.com:
I'd like to run a Maya Python Script (NOT a single Python command) from within my userSetup.mel script. The Python script is simple but does use some Mayan-Specific Commands. The Pythong script for testing is:

#Python Script:
import maya.cmds as CMDS
Returned = cmds.fileDialog (mode = 1, title = "Save Layer / Pass drop Miranda")

The closest I've gotten to running it from MEL is something like:

// Within the MEL ​​Script:

python (exec ("\\ Server \ myfolder \ SaveLPLFile.py"));


I've tried various cnfigurations of \\, \\\\, //, etc to accomodate the potential errors of escape characters but nothing Seems to have any effect. The python / exec command Appears to run without error (to the Maya console) but the actual script is not running Because a cmds.fileDialog () Never Appear. Even when there is a typo in the filename The command Appears to run ..... apparently there is some long-standing bug in python That does not return an error message properly in some instances.

Any alternatives That might be a better method of running a Python script from within the MEL?


april...@gmail.com

unread,
Oct 9, 2019, 2:43:21 PM10/9/19
to Python Programming for Autodesk Maya
Here is an example of running it through a Mel command that I used for a button inside a mel script:

string $MelCommand = "python(\"import sys\"); python(\"sys.path.append('" + $sourceLocationPath + "')\"); python(\"import YOURMODULE\");python(\"YOURMODULE.launchMethod()\");";
 
Alternatively, you could run the same code as previously stated, except you should be able to use any path this way.

python("import sys");
python("sys.path.append('" + $sourceLocationPath + "')"); 
python("import YOURMODULE");
python("YOURMODULE.launchMethod()");

Using a "launchMethod()" in this way will let all of your Python code live in another file and leaving you to not write an exuberant amount of "python();" lines.
Reply all
Reply to author
Forward
0 new messages