Search code examples
pythonfilemayamel

Call MEL script from Python - Error occurred during execution of MEL script


I'm trying to create a simple Python script that will call a MEL script from within Maya to create a cube. Yay! Should be fairly straight-forward althougth I may have got the syntax for the source files wrong.

Here's what I have:

The runMEL.py Python file: import maya.mel as mel

def runMEL():
  print ("Running MEL from Python!")
  mel.eval('"source D:\Maya_Python\myMELScript.mel;"') # source of the file
  mel.eval("myMELScript;") #name of the function

runMEL() # call the function above

And the MEL script myMELScript.mel

global proc myMELScript()
// call a MEL script with Python
{
  polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1;
  print("MEL just made a cube!");
}

I get the following from the console:

Running MEL from Python!
// Error: "source D:\Maya_Python\myMELScript.mel;"; // 
// Error: Line 1.40: Syntax error // 
# Error: RuntimeError: file <maya console> line 5: Error occurred during execution of MEL script
Line 1.40: Syntax error #

Solution

  • You almost got it right, you have to pass the path as a string and escape it. Furthermore mel is picky with the forward / and backward \ slashes, it expects /

    That should do:

    mel.eval('source "D:/Maya_Python/myMELScript.mel"')
    

    Note: Normally in python you could write your Path as well as

    D:\\Maya_Python\\myMELScript.mel
    

    but mel is not clever enough so it will escape the escape symbol :D