pymel

116 views
Skip to first unread message

farsheed

unread,
Dec 15, 2007, 4:08:28 AM12/15/07
to python_inside_maya
DETAILS:
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.
Now compatible with both maya8.5 and maya2008


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 commands: 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.

Its Just Better:

* Fixes Bugs in maya.cmds: with pymel, maya's bugs are corrected
behind the scenes so you don't even know they exist!
* Use python's help() command on nearly any object to get
meaningful help docs.

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



take a look at this simple comparison:

with Mel


string $objs[] = `ls -type transform`;
for ($x in $objs) {
print (longNameOf($x)); print "n";

// make and break some connections
connectAttr( $x + ".sx") ($x + ".sy");
connectAttr( $x + ".sx") ($x + ".sz");
disconnectAttr( $x + ".sx") ($x + ".sy");
string $conn[] = `listConnections -s 0 -d 1 -p 1 ($x + ".sx")`;
for ($inputPlug in $conn)
disconnectAttr ($x + ".sx") $inputPlug;

// add and set a string array attribute
// with the history of this transform's shape
if ( !`attributeExists "newAt" $x`)
addAttr -ln newAt -dataType stringArray $x;
string $shape[] = `listRelatives -s $x`;
string $history[] = `listHistory $shape[0]`;
string $elements = "";
for ($elem in $history)
$elements += """ + $elem + "" ";
eval ("setAttr -type stringArray " + $x + ".newAt " + `size
$history` + $elements);
print `getAttr ( $x + ".newAt" )`;

// get and set some attributes
setAttr ($x + ".rotate") 1 1 1;
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];

// call some other scripts
myMelScript( `nodeType $x`, $trans );
}


default Python:


import maya.cmds as cmds
import maya.mel as mm
objs = cmds.ls( type= 'transform')
if objs is not None: # returns None when it finds no matches
for x in objs:
print mm.eval('longNameOf("%s")' % x)

# make and break some connections
cmds.connectAttr( '%s.sx' % x, '%s.sy' % x )
cmds.connectAttr( '%s.sx' % x, '%s.sz' % x )
cmds.disconnectAttr( '%s.sx' % x, '%s.sy' % x)

conn = cmds.listConnections( x + ".sx", s=0, d=1, p=1)
# returns None when it finds no matches
if conn is not None:
for inputPlug in conn:
cmds.disconnectAttr( x + ".sx", inputPlug )

# add and set a string array attribute
# with the history of this transform's shape
if not mm.eval( 'attributeExists "newAt" "%s"' % x):
cmds.addAttr( x, ln='newAt', dataType='stringArray')
shape = cmds.listRelatives( x, s=1 )
history = cmds.listHistory( shape[0] )
args = tuple( ['%s.newAt' % x, len(history)] + history )
cmds.setAttr( *args , **{ 'type' : 'stringArray' } )
print cmds.getAttr ( x + ".newAt" )

# get and set some attributes
cmds.setAttr ( '%s.rotate' % x, 1, 1, 1 )
scale = cmds.getAttr ( '%s.scale' % x )
scale = scale[0] # maya packs the previous result in a list
for no apparent reason
trans = list( cmds.getAttr ( '%s.translate' % x )[0] ) # the
tuple must be converted to a list for item assignment
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]) )


with Pymel:


from pymel import * # safe to import into main namespace
for x in ls( type='transform'):
print x.longName() # object oriented design

# make and break some connections
x.sx >> x.sy # connection operator
x.sx >> x.sz
x.sx <> x.sy # disconnection operator
x.sx.disconnect() # smarter methods (no args = disconnect all)

# add and set a string array attribute
# with the history of this transform's shape
if not x.newAt.exists():
x.newAt.add( dataType='stringArray')
x.newAt = x.getShape().history()

# get and set some attributes
x.rotate = [1,1,1]
trans = x._translate.get()
trans *= x.scale.get() # vector math
x._translate = trans # ability to pass list/vector args
mel.myMelScript( x.type(), trans) # automatic handling of mel
commands


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

reference: http://www.luma-pictures.com

cheers, Farsheed.

Chadrik

unread,
Dec 15, 2007, 5:18:40 PM12/15/07
to python_inside_maya
i use pymel everyday in a production environment, and i can promise
you if you want to use python in maya, you have to have something like
it or you're only leveraging a fraction of python's power. it's
recently become an open-source collaboration between myself and
another programmer at Attitude Studio in France, but we're always
looking for feedback and help.

there's a google group with update notifications:
http://groups.google.com/group/pymel

and the google code site, which hosts the svn repo with the latest
version:
http://code.google.com/p/pymel/

a new version should be coming out this week.

-chad

farsheed

unread,
Dec 15, 2007, 6:22:40 PM12/15/07
to python_inside_maya
Totally agree, and It is a pleasure to see you here. Thanks for pymel
and thank you for doing such a great service to CG society.
I also use it in my every day production and very curious about the
bugs of pymel. In my tests it shows no bugs, Am I correct?
Hasn't it any bugs? or you have seen bugs on it? Could you please
explain here what are current problems with pymel?
TIA, Farsheed.

Chadrik

unread,
Dec 15, 2007, 9:11:06 PM12/15/07
to python_inside_maya

well, its not a bug until someone discovers it!

version 0.6 is the version on highend3d, and this version has a few
minor bugs which have been addressed in 0.7, which is available on the
google code site via svn. This will be released on highend3d very
soon.

Currently, the only real "problems" with pymel are those inherited
from maya.cmds, and fixing these problems is one of the primary goals
of the pymel module. Beyond bugs there is the less black-and-white
issue of usability and design: ensuring that all of the commands and
classes within pymel are consistent and intuitive for the end-user,
particularly mel scripters "graduating" to python.

a big consideration is balancing backward-compatibility and new
features. most of pymel has been designed such that you can simply
import pymel instead of maya.cmds and your code will continue working.

for example
cmds.listConnections( c=1 )
might return something like:
['foo', 'bar', 'this', 'that']
maya.cmds version of this command returns a flat list because mel is
incapable of doing 2d arrays and maya.cmds is just a wrapper for mel,
really.

but pymel will return a list of pairs
[('foo', 'bar'), ('this', 'that')]
which makes it much easier to loop through, but which breaks bacwkard-
compatibility with maya.cmds. just for the sake of thoroughness, we
will eventually include a set of global preferences that the user can
alter to change certain behavior of pymel. for instance, one global
might enforce backward-compatibility across all commands. however, i
think that this is a small issue for most users of pymel.

keeping pymel competitive with maya.cmds speed-wise is also a
concern. every fix that is made to maya.cmds in pymel comes at a
speed cost. you'll never notice it unless you're working with 30,000
objects in your scene, but sometimes you *may* have that many, so i
want to make sure it is not a detraction. one way around this that
leads into some pretty exciting new territories is the potential for
crossover between pymel and the maya api. if maya.cmds.ls is too slow
once we start adding our customizations to it, we can write our own
using the OpenMaya api module. we can rapidly prototype the "front-
end" interface of pymel as we are doing now, and gradually swap out
necessary components with api commands later without affecting code
that users are writing now. and while pymel can eventually serve as a
bridge to api work, i don't want it to become too convoluted or
complicated -- i want it to stay a rapid scripting language that a mel
scripter can quickly pick up but which does not place restraints on a
seasoned python programmer.

ultimately, it's up to users like yourself to give us great feedback
and help us direct the development of pymel. so keep pushing it to its
limits and post your bugs and suggestions at our google code page.

-chad

farsheed

unread,
Dec 16, 2007, 2:06:55 AM12/16/07
to python_inside_maya
I am at your service. absolutely when I saw a bug, I report that to
you. I've use it since October and I can't find any bus yet.
Thanks for your time.
Farsheed.

Chadrik

unread,
Jan 4, 2008, 10:04:23 PM1/4/08
to python_inside_maya

hey guys, the new version of pymel has been released on highend

http://www.highend3d.com/maya/downloads/tools/syntax_scripting/4844.html

there have been a lot of changes, so i'm posting here first so that
you guys can check it out and let me know if there are any glaring
bugs that i have overlooked. this should fix the joint index problem
posted in the other thread.

-chad

farshid ashouri

unread,
Jan 4, 2008, 10:30:10 PM1/4/08
to python_in...@googlegroups.com
Thanks man. nice work.:)

farshid ashouri

unread,
Jan 4, 2008, 11:30:43 PM1/4/08
to python_in...@googlegroups.com
I can't import the module. I tested few times but I can't. then I reinstall 0.6 and every thing works great.
What is happening with 0.7?
Do I wrong? I import it like:

from pymel import *

It is simple but error occurs in 0.7 and I put it in my PYTHONPATH. Also there is no other pymel packages in my PYTHONPATH.
TIA.


Chadrik

unread,
Jan 5, 2008, 12:02:53 AM1/5/08
to python_inside_maya
what specifically is the error?
are all the files under a single folder called pymel?

Chadrik

unread,
Jan 5, 2008, 12:15:38 AM1/5/08
to python_inside_maya
haha, i expected something like this to happen. it always does.
something in my prep script screwed up the release. download again and
it should work fine.

-chad

farsheed Ashouri

unread,
Jan 5, 2008, 2:41:52 AM1/5/08
to python_inside_maya
ok,It works now. Thanks.

Chadrik

unread,
Jan 24, 2008, 11:43:23 AM1/24/08
to python_inside_maya

hey everybody,
i released a patch version of pymel on highend. it includes a few bug
fixes and a handful of new features. we're still brewing up some huge
new features for version 0.8.

fixed bug in PyNode that was failing to cast Attributes to Nodes
fixed a bug in createSurfaceShader which was failing to return the
correctly renamed shadingGroup
fixed a bug in mel2py when attempting to resolve path of a mel script
that uses whitespace
fixed several minor bugs in mel2py and added many formatting
improvements
renamed Reference to FileReference to avoid conflict with
node.Reference
added listAnimatable
added mel2pyStr for converting a string representing mel code into
python
improved mel2py formatting - now attempts to match lists and commands
that span multiple lines
fixed a bug in Transform.zeroTransformPivots (thx koreno)
fixed a bug in Transform.centerPivots (thx koreno)
all commands, including custom commands, are now brought into the main
namespace (excepting those we *wish* to filter)
fixed bugs in Attribute.getParent, Attribute.getChildren,
Attribute.getSiblings, where results were not being returned as
Attribute classes
fixed bug in Constraint.getWeight, Constraint.setWeight, and all
constraint nodes that inherit from it.
added Attribute.lastPlugAttr, which will only return the last plug
attribute of a compound attribute
Attribute commands which wrap attributeQuery now use lastPlugAttr for
compatibility with compound attributes
added Attribute.array for retreiving the array (multi) attribute of
the current element

farsheed Ashouri

unread,
Jan 24, 2008, 12:58:20 PM1/24/08
to python_inside_maya
Thanks Chadrik. I test it successfully. waiting for 0.8.
Cheers.

Chadrik

unread,
Jan 25, 2008, 5:11:35 PM1/25/08
to python_inside_maya
i just released 0.7.6. this fixed a stupid bug i introduced in
0.7.5.

also, against my better judgement, i've included my Script Editor
replacement for maya 2008. this is a plugin / mel script that allows
the script editor to translate all mel output into python. this is
still very beta, and i'm only announcing it on this thread to get some
early feedback on it. the mel2py translator that it is based on is by
no means perfect, but after a lot of work it can now get through all
4,000 of mel the scripts in maya scripts directory without error. let
me stress again that this is beta and only works on 2008.

-chad
Reply all
Reply to author
Forward
0 new messages