PyMel

object oriented programming introduces the concept of classes or “types”, and each occurrence of a type is called an instance. For example, a string is a type of object that represents a series of characters. An instance of a string could be the word ‘hello’. You’ll probably find that the concepts behind object oriented programming are fairly familiar, because Maya itself is designed in a very object-oriented way. Maya has many types of nodes, each with its own attributes, properties, and capabilities. Each unique occurrence of one of these node types in your scene is like an instance of a class in python.

The great thing about python is, unlike MEL, we’re not stuck with the default data types. We can make new types! That’s a big part of what PyMEL adds: new Maya-specific data types to represent nodes, attributes, UI elements, vectors, matrices, etc.

The two examples start out the same way: with the ls function. This is how we get our raw material to work with, in this case our node objects. In general, we can get nodes in 3 ways:

  • list them based on some criteria

  • create them

  • get them by their name if we know it

Once we have our list of nodes, we can begin to work in an object-oriented fashion. There is not a hard-fast rule for converting from a procedural style to an object-oriented one, but here are some general guidelines:

  • the argument to the command – in this case cam – becomes the operating object on the left

  • instead of flags – focalLength and aspectRatio we use methods, which are attached to right of the object with a period .

  • query methods are typically prefixed with ‘get’ and edit methods are prefixed with ‘set’. If a query does not have a corresponding edit, it may not have a ‘get’ prefix.

So, in our case query=True, aspectRatio=True becomes .getAspectRatio. And query=True, focalLength=True becomes .getFocalLength.

Use these guidelines for what aspects of PyMEL are best suited to object-oriented programming:

  1. creating nodes and UI elements : remains mostly procedural

  2. listing objects and UI elements: object-oriented, except for general listing commands like ls

  3. querying and editing objects and UI elements: object-oriented, except for commands that operate on many nodes at once, like select and delete

PyMEL uses PyNode instances to represent Maya objects. Each of these PyNodes can have a number of methods that process and return information related to that object. Basically, each object can be represented as a class instance that has it's own methods. (3DevArtist)

Why should I use Pymel?

More like Python than maya.cmds, more object-oriented. Easier to handle both API function and traditional MEL function at once.

In a nutshell, PyMEL results in less lines of code at the expense of being

more heavy to compute. So its a trade-off between readability and

performance. Depending on your needs, one is typically more suited than the

other for certain tasks. As an example, you might not write a deformer

using PyMEL, but you might write a scene saver.

(Marcus)

You really really should use PyMel, except for stuff that involves tens of thousands of nodes, where MayaPy will be signifcantly faster. Even though PyMel is slower, the speed of developing in PyMel versus MayaPy far outweighs the (mostly) insignficant speed difference.

Theres an enormous difference between just coding in a text editor (or the Maya script editor) and with a proper IDe such as Eclipse or WingIDE. Rather than having to put print statements all over the place to try to find a bug, you can do stuff like stepping through the code, setting breakpoints, watching variables, etc. You'll literally save days of work once you write anything lager than a few hundred lines

Download the 30 days WingDB trial and try using it with Maya:

http://www.wingware.com/doc/howtos/maya

(CaptainSam)

Why shouldn't I use Pymel?

Excruciatingly slow, according to Hamish McKenzie. Not integrated with earlier versions of Maya, so may cause compatibility issues and/or not work cross-platform, plus requires plug-in installation.

Chad et. al are working on it. It would help if people would send requests to AD to help support migrating/intergrating PyMEL deeper into the API.

The core of the issue is translation. PyMEL has to translate the API objects into strings and back again because it is written as a wrapper. If it was a first class citizen =)

ALTHOUGH, here is some code to show you that PyMEL is not really lagging, nor is MEL completely lagging, but in the end, depending on what you are doing (i.e. iterating over thousands of vertices) it may make a difference:

import pymel.core as pm

import maya.cmds as cmds

import maya.mel as mel

import maya.OpenMaya as OpenMaya

def MayaAPI(obj):

list = OpenMaya.MSelectionList()

list.add(obj.longName())

node = OpenMaya.MObject()

list.getDependNode(0, node)

nodeFn = OpenMaya.MFnDependencyNode(node)

x = nodeFn.attributeCount()

for i in range(0, x):

attr = nodeFn.attribute(i)

def PyMELAPI(obj):

pm.listAttr(obj.longName())


def MayaCmds(obj):

cmds.listAttr(obj.longName())

import time

obj = pm.ls(sl=1)[0]

# MAYA API SPEED

start = time.time()

for i in range(0,2000):

MayaAPI(obj)

print time.time()-start

# MAYA CMDS SPEED

start = time.time()

for i in range(0,2000):

MayaCmds(obj)

print time.time()-start

# PYMEL SPEED

start = time.time()

for i in range(0,2000):

PyMELAPI(obj)

print time.time()-start

# MEL SPEED

start = time.time()

for i in range(0,2000):

mel.eval('listAttr %s' % obj.longName() )

print time.time()-start

Boilerplate I use to anecdotally test new PyMEL/Maya versions.

(Alexander Morano)

Nahh I should rather keep avoiding PyMmel and do stuff on my own. Surely PyMmel offers you a lot of stuff out of the box for the easily convinced man... I'm obviously not that kind of man ;]

And as it turns out stuff is not always necessary and bloats the entire thing on the backend. On the other hand I still have to code a lot for Maya2009 and I surely don't go and install PyMmel on every machine in the company.

PyMmel inspires me a lot! But Ok I actually find MRV much more impressive! Both are comprehensive packages. Just like Mr Hamish McKenzie I tend to build my own package of tools.. This way you learn the most :]

(Eric Werner)

from Jason Parks:

Patently wrong right here:

"Not integrated with earlier versions of Maya, so may cause compatibility issues and/or not work cross-platform, plus requires plug-in installation."

No plug-in required, works fine on Mac, should be fine on Linux because it is just ascii.

And this is just insane:

"Nahh I should rather keep avoiding PyMmel and do stuff on my own. Surely PyMmel offers you a lot of stuff out of the box for the easily convinced man... I'm obviously not that kind of man ;]

And as it turns out stuff is not always necessary and bloats the entire thing on the backend. On the other hand I still have to code a lot for Maya2009 and I surely don't go and install PyMmel on every machine in the company."

This person has no clue what he is missing. Don't blame them, until they try, they won't know the power of PyMEL's class based, IDE-compatible, auto-complete system. It doesn't "bloat" anything on the backend. You don't have to install it on every machine in the company, you want it all to be imported off of the network or through version control. That way everybody is always on the same version and there is no install of the scripts. Just adding a pythonpath to their startup.

Also--his GDC 2011 talk here!

FAQ

How do I get Python to show up in the script editor?

  • Pymel comes with a plugin for that. It adds an option

  • to output Python under History->History Output in the Script Editor.

  • You need the py/pyc and the mel from:

  • C:\Program Files\Autodesk\Maya2011\Python\Lib\site-packages\pymel\tools\scriptEditor\

What does import pymel.core as pm do?

Import the pymel library

Glossary

methods

a method is a function bound to an object. ex. ‘bar’ is the method in foo.bar()

PYTHONPATH

To find available modules, python searches directories set in an environment variable called PYTHONPATH. This environment variable can be set for each Maya installation using the Maya.env file, or it can be set at the system level, which will set it for all instances of python, including those built into Maya (aka “mayapy”). Each of these methods have their pros and cons.

On, Windows you might create a directory for python development at C:\My Documents\python. Then you would add this line to your Maya.env:

PYTHONPATH = C:\My Documents\python

Resources

Tokeru's Pymel Tutorials

Maya Python google group

Power Python for Maya (Jason Parks GDC talk)

Download Pymel here (for older versions--for Maya 2011, just go to the Maya help)

Learn Python The Hard Way

CG Bootcamp's Intro