Python for Maya 8.5

waisttherapeuticSoftware and s/w Development

Nov 4, 2013 (3 years and 9 months ago)


Python for Maya 8.5

The purpose of this document is to provide some basic information about the strengths of the
Python scripting language, as well as how it is implemented in Maya. It is a collection of insights
and opinions about how and why Python c
an be useful to our customers and is intended as a
high level view for people with limited programming experience. Note: This is for internal
consumption only and should not be shared directly with customers.

As a point of reference, Python is compared t
o MEL throughout this doc. However it is important
to note by integrating Python into Maya we are not trying to convert users away from MEL.
Rather we are trying to help more advanced “coders” who are frustrated or feel handcuffed by
various limitations
of MEL. As far as customers are concerned the first message should be that,
if MEL is already doing an adequate job, then there is no particular need to switch to Python.

With that said, there are many unique features and benefits associated with the Pyt
hon scripting


In Python, libraries of functionality and class definitions are organized as modules. There are
many of these built
in “modules” and many more freely downloadable resources available for
Python that extend the basic
functionality of the language.

For example…

math module : Assorted math routines

sys module : System libraries

re module : Regular Expression tools

string module: Routines for working with strings

Other modules contain routines for doing useful things

such as:

Sending an email with attachments (email module)

Accessing ftp sites (urllib2 module)

Manipulating file paths (os.path module)

Reading/writing with sockets (socket module)


You can also make and reuse your own modules, and easily share them with others. For
example, if you have a collection of related routines that you use frequently for multiple scripts,
you can store them all in one custom module

“The speed of outside development has just been exponentially increased.

The popularity of
MEL files on websites like highend3d points out our strong user development community.

imagine the functionality of any of those posted scripts being cr
referenced between newer

Not just cut and paste, but a developing library of higher and higher level customer
created tools and functionality.”

(anonymous Python user)


The initial focus of this release was on simply getting
python working in Maya. Eventually Python
will open users and us up to integrating with 3rd party tools, such as editors, debuggers, UI
toolkits, etc…

Note: Caution is required here, since we don't technically integrate with any yet. So the response
o our customers regarding this extensibility should be something like…

Moving to Python enables us to consider integration with 3rd party tools and libraries, and we'll
look into such future enhancements based on feedback from ours users, balanced of cour
se with
other requirements we get (as we do for planning any other features in Maya). The great thing is
that now we have the option to extend the language relatively easily to take advantage of other
tools. With MEL we don't have that as a readily availab
le option.

Development and Support

Python is a widely, actively developed and maintained language. It can be compared to Linux in
this regard.
The language has a depth that you only get from a distributed team effort and a
considerable following ove
r many years.

MEL is of course still actively maintained by Autodesk and it should be stressed that it is by no
means being abandoned. Still, MEL is proprietary to Maya. Where as Python is a widely used
standard in many areas including several of our ot
her products (e.g.. MotionBuilder and Toxik).

Additionally, many of our existing customers are

already working with Python in various parts of
their pipelines. Therefore integrating Maya tools written in python will be easier than trying to
integrate a
nd maintain those written in MEL and wrapped to fit Python.

Access to the Maya API

Now with Python you can make direct calls to the Maya API. This means that you can essentially
write plugins without a compiler and all the associated overhead of work
ing directly in the API.
Previously this was an obstacle to learning plugin creation. Where performance is an issue, the
particular bottlenecks can be rewritten in C++ and called in the original Python plugin.

The Maya API is technically known as OpenMa
ya. In Python, you can access the Maya API by
adding “import maya.OpenMaya” to the top of your script. Then you can use various Maya API

For example:

allows you to access the API’s
class and functions

Example scripted plugins can be found in


Portable and Protected Scripts

Python uses two file types: py and pyc. The file .py con
tains the source code (the actual editable
text of the script) similar to the MEL equivalent, which would be .mel. The file .pyc is the byte
compiled version of that script. Python creates these automatically. These .pyc files can be
shared and used jus
t like the .py file, only they are not easily read or reversed engineered.
Therefore these byte
compiled files give you some basic protection for your code.

Standalone Python and Maya

Like MEL, Python can be run in batch mode. This is done via an ex
ternal Python interpreter.
Maya modules can be used externally in this way as well. You can also write standalone
applications with the Maya API Python bindings. Maya 8.5 ships with the “
” command
which can be found in the standard bin folder.

The following lines will put you into a python
prompt mode…

import maya.standalone


This is the equivalent to mayabatch.

Other Specific Strengths (as compared to MEL)

Python provides higher level data structures
and operations that aren’t found in Mel


Unlike MEL they can contain different types of data like ints, floats and strings.

e.g… [4, 21.453, ‘Sphere’] They can be easily changed and have built
in methods for
things like sorting, appending and inse
rting items.


Similar to lists but they have a fixed size and contents don’t change once they’re created.
They are sometimes used because they are more efficient than


Also similar to lists but they are unordered and each item must be

unique. They have built
in functions for things like union and difference.


Associated lists. For instance, you can store a list of words with an associated list of
names and ages. e.g… {‘Steven’ : 36, ‘Cory’ : 35, ‘Lee’ : 38, ‘Marcel : 5


Unlike MEL, Python is object oriented. Classes allow for much more complex and flexible
data organization and management.

Python has better functional organization

than MEL

Python uses spaces and tabs as a means to group and organize cod
e. Some
programmers will find the tabbing in Python to be quirky and it may take some getting
used to
. However, the ability to use tabbed indention as a means to group statements,
as opposed to MEL’s curly brackets {….}, is generally preferred.

“It ma
kes it easier for someone learning to program and it is easier to read in the long
run. No more counting up those damn Curly Brackets!”

(anonymous Python user)

Additionally, editors that support the idiosyncrasies of standard languages such as
Python al
ready exist. These editors make this sort of housekeeping much easier.

Python is generally more elegant and concise

For example… what is done here in a couple of lines would require many more steps
(i.e.… more code) in MEL.

>>> list = [1,34,2,33,

>>> list2 = [x for x in list if x > 5]

result for list2 is : [34, 33, 53, 87]

“One line of code filters the list into a new list that contains only the elements that are
greater than 5.

To a Python coder, it is easy to read.

And, it is amazingly
compact and

(anonymous Python user)

Python has a more flexible variable implementation than MEL

Python variables don’t require the $ in front of them as MEL does. Additionally they are
dynamic, which means they can change or be changed if th
ey need to be. For example,
a variable can be an integer and then get changed to a string later on in the script. This
is not true with MEL.

Python allows you to create built in default command line arguments.

If you run a script with no arguments the
n it will default to a predetermined value. For
example if you had a script called

createCustomLocator() : may create a single locator.

createCustomLocator(5) : may create five locators.

Python provides a way of creating built
in help info for scripts

You can easily add a “doc” string to part of a script so that a user can get useful info
about the script if needed. For example, you can return
info about how to use the script.

) : would return somethi
ng like…

Help on function createCustomLocator in module ???:


Generates one or more custom locator objects to be used with a character rig.

Python handles regular expressions better than MEL

Regular expressions are used for matchi
ng and comparing strings of text. For example…
trying to find part of a name in a list of objects. The commands in MEL for finding these
“matches” are somewhat limited. However the re module in Python is very extensive and
quite powerful.

Maya command
s and MEL scripts can still be used in Python

Any Maya command can be used from within Python by adding “
import maya.cmds
” and

import maya.mel
” to the top of the script.
The need to write “maya.cmds.sphere()” in
Python as opposed to simply “sphere” wit
h MEL initially looks awkward in comparison,
but is functionally the same.

Any command not yet fully functional in Python can be called using


Additionally, Python can also be used from within MEL via the new “
command. Th
ese two crossover commands allow you to use any MEL script in Python
and any Python script in MEL. This means you don’t need to translate your MEL scripts
in order to use them with Python.

Useful Python resources

Maya Python Docs

Python in Maya

Byte of Python

Wiki Python