|
|
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
- Node classes for nearly every major DAG object
- An Attribute class organizes all the attribute commands in one place
- Slice and dice your file paths with ease
- Operate on sets as if they were python's builtin set type
- Perform vector math, convert between spaces, and easily set object attributes with the results
Do More with Less Code
- Customized operators for succinct scripting
- Call Mel procedures as if they were python functions: no more annoying string formatting
- Manage optionVars as a python dictionary
- Enhanced Commands with intelligent defaults and new flags
- Automatic Casting to Pymel Types: values are returned as pymel Nodes, Paths, Vectors, etc.
- Fixes Bugs in maya.cmds: with pymel, maya's bugs are corrected behind the scenes so you don't even know they exist!
Includes Tools to Ease Your Transition to Python
- mel-to-python translator for converting mel script into python scripts
- python-to-mel plugin factory for turning python classes into mel commands
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 manipulationthere'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.
