官术网_书友最值得收藏!

Calling a MEL script with Python

Maya offers two different languages with which to create custom functionality via scripting—both Maya Embedded Language (MEL) scripts and Python. In practice, you'll only want to use one, and of the two, Python offers a much better and more flexible experience.

However, it is not uncommon to have legacy scripts that were written back before Maya added Python functionality. While the "right" solution would be to rewrite the scripts using Python, there's not always enough time to do so. In those cases, it can sometimes be helpful to be able to call out to legacy, MEL-based functionality from within your Python scripts.

Getting ready

Although Python is definitely the better way to create new functionality, it may sometimes be the case that you have older scripts that were written in MEL that you would like to incorporate.

The best option is to rewrite the script in Python, but if the script is complex or you don't have time, it may be easier to just invoke the MEL functionality from within a Python script. This way, you can incorporate legacy functionality without completely reinventing the wheel.

How to do it...

For this recipe, you'll need the MEL script. If you don't have one handy, open up a new file and enter the following:

global proc myMELScript()
{
    polyCube;
    print("Hello from MEL!");
}

Save this as myMELScript.mel in your maya/scripts directory. Although we won't be going into the details of MEL, do note that the file has the same name as the function that we're defining. Most MEL scripts will follow that convention. Also note the inclusion of semicolons after the end of each line. Although Python doesn't require them, MEL (and many other languages) does.

Once you have that, create a new file and name it runMEL.py. Enter the following:

import maya.cmds as cmds
import maya.mel as mel

def runMEL():
    print("Running MEL from Python")
    mel.eval("source myMELScript;")
    mel.eval("myMELScript;")

runMEL()

Save the script and run it with:

import runMEL

Because the last line of our script invokes the runMEL command, it will automatically take effect. You should see a new cube, as well as the following output in the Script Editor:

Running MEL from Python
Hello from MEL!

How it works...

In this example, we imported both maya.cmds and maya.mel. The maya.mel library provides support to interface Python with MEL, with one of its most useful commands being the eval function, which takes an arbitrary string and attempts to run it as an MEL command. In the earlier example, we do that twice, with the first command being:

source myMEL;

The source command does the same thing as reload, in that it ensures that Maya will reread the entire source file, rather than rerunning a potentially outdated version. This shouldn't matter because it's only necessary if you're making changes to the MEL script (and hopefully you're not doing that, use Python instead!) but it's a good thing to include just in case.

Once we've done this, we actually run the MEL script with:

myMEL;
主站蜘蛛池模板: 新巴尔虎右旗| 巴里| 雅江县| 平乐县| 广元市| 昌乐县| 高青县| 绥中县| 阿克苏市| 新田县| 吉林市| 东兰县| 马公市| 郁南县| 溆浦县| 荃湾区| 海安县| 井研县| 湟中县| 克什克腾旗| 金昌市| 福鼎市| 广安市| 观塘区| 平泉县| 宜黄县| 郧西县| 镇平县| 庆安县| 砚山县| 丽江市| 绥宁县| 巩义市| 昆山市| 永定县| 涿州市| 雅安市| 许昌市| 繁峙县| 澄城县| 新田县|