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

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;
主站蜘蛛池模板: 乌兰察布市| 旺苍县| 淮阳县| 木里| 永吉县| 东山县| 治县。| 天峨县| 达州市| 保靖县| 盈江县| 施甸县| 新邵县| 和林格尔县| 梨树县| 宁乡县| 鄯善县| 鄂州市| 内黄县| 固安县| 天等县| 柘荣县| 凉城县| 涿鹿县| 三门峡市| 赣榆县| 南丹县| 华坪县| 明水县| 南充市| 比如县| 辽中县| 贺州市| 哈巴河县| 南康市| 龙海市| 华蓥市| 元谋县| 迁西县| 高邮市| 平塘县|