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

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;
主站蜘蛛池模板: 龙海市| 全椒县| 开封县| 光山县| 鹤峰县| 抚顺县| 甘孜| 禹州市| 巴彦淖尔市| 黄陵县| 迁安市| 大新县| 延寿县| 湘潭县| 常山县| 西平县| 门源| 禄丰县| 古蔺县| 彩票| 孝昌县| 梅河口市| 溧水县| 会东县| 鄂州市| 鹤庆县| 肃宁县| 青神县| 安达市| 威信县| 石阡县| 仁怀市| 雅安市| 三穗县| 广州市| 永城市| 连城县| 临夏县| 同德县| 米林县| 南江县|