- Maya Programming with Python Cookbook
- Adrian Herbez
- 509字
- 2021-07-14 10:46:42
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;
- Java程序設計與開發
- Apache Spark 2.x Machine Learning Cookbook
- Apache Hive Essentials
- Java技術手冊(原書第7版)
- 軟件測試工程師面試秘籍
- The React Workshop
- Backbone.js Blueprints
- Gradle for Android
- Tableau 10 Bootcamp
- Extreme C
- Clojure for Java Developers
- jQuery for Designers Beginner's Guide Second Edition
- OpenCV 3.0 Computer Vision with Java
- Access數據庫應用教程(2010版)
- 關系數據庫與SQL Server 2012(第3版)