What's new? | Help | Directory | Sign in
Google
pymel
Python in Maya Done Right
  
  
  
    
Show all Featured Wiki Pages:
About Autodesk Modifiers RoadMap
Join project
Project owners:
  chadrik
Project members:
orenouard, koreno

Pymel

Python in Maya Done Right

Pymel makes python scripting with Maya work the way it should. Maya's command module is a direct translation of mel commands into python commands. The result is a very awkward and unpythonic syntax which does not take advantage of python's strengths -- particulary, a flexible, object-oriented design. Pymel builds on the cmds module by organizing many of its commands into a class hierarchy, and by customizing them to operate in a more succinct and intuitive way.

Powerful new classes

Do More with Less Code

Includes Tools to Ease Your Transition to Python

Code Comparison

with Mel

string $objs[] = `ls -type transform`;
for ($x in $objs) {
    connectAttr( $x + ".sx") ($x + ".sy");
    disconnectAttr( $x + ".sx") ($x + ".sy");
    setAttr ($x + ".rotate") 1 1 1;
    string $conn[] = `listConnections -s 0 -d 1 -p 1 ($x + ".tx")`;
    for ($inputPlug in $conn)
        disconnectAttr $inputPlug ($x + ".tx");
    move 1 2 3 $x;
    float $trans[] = `getAttr ($x + ".translate")`;
    float $scale[] = `getAttr ($x + ".scale")`;
    $trans[0] *= $scale[0];
    $trans[1] *= $scale[1];
    $trans[2] *= $scale[2];
    setAttr ($x + ".scale") $trans[0] $trans[1] $trans[2];
    myMelScript( `nodeType $x`, $trans );
    print longNameOf($x); print "n";
    print basenameEx( `file -q -sn`); print "n";
}

default Python

import maya.cmds as cmds
import maya.mel as mm
objs = cmds.ls( type= 'transform')
if objs is not None: # maya.cmds.ls returns None when it finds no matches
    for x in objs:
        cmds.connectAttr( '%s.sx' % x, '%s.sy' % x)
        cmds.disconnectAttr( '%s.sx' % x, '%s.sy' % x)
        cmds.setAttr ( '%s.rotate' % x, 1, 1, 1 )
        conn = listConnections( x + ".tx", s=0, d=1, p=1)
        if conn:
            for (inputPlug in conn)
            disconnectAttr( inputPlug, x + ".tx")
        cmds.move( 1, 2, 3, x ) # confusing syntax
        trans = cmds.getAttr ( '%s.translate' % x )
        trans = trans[0] # maya packs the list in a tuple for no reason
        scale = cmds.getAttr ( '%s.sclae' % x )[0]
        trans[0] *= scale[0]
        trans[1] *= scale[1]
        trans[2] *= scale[2]
        cmds.setAttr ( '%s.scale' % x, trans[0], trans[1], trans[2] )
        mm.eval('myMelScript("%s",{%s,%s,%s})' % (cmds.nodeType(x), trans[0], trans[1], trans[2])
        print mm.eval('longNameOf("%s")', x)
        print mm.eval( 'basenameEx( "' + cmds.file( q=1, sn=1) + '") ' )

with Pymel

from pymel import * # safe to import into main namespace
for x in ls( type='transform'):
    x.sx >> s.sy # connection operator
    x.sx <> s.sy # disconnection operator
    x.tx.disconnect() # smarter methods (all conn. broken when no arg passed)
    x.rotate.set( 1, 1, 1 )
    move( x, [1, 2, 3] )
    trans = x.translate.get()
    trans *= x.scale.get() # vector math
    x.translate.set(trans) # ability to pass list/vector args
    mel.myMelScript( x.type(), trans) # automatic handling of mel commands
    print x.longName() # object oriented design
    print sceneName().namebase # path manipulation

there's a reason why python is rapidly becoming the industry stanadard. with pymel, python and maya finally play well together.

Pymel is developed in-house at Luma Pictures.