Package: pymel
Python in Maya Done RightPyMEL 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 themaya.cmdsmodule by organizing its commands into a class hierarchy, and by customizing them to operate in a more succinct and intuitive way.Project GoalsCreate an open-source python module for Maya that is intuitive to MEL users and python users alikeFix bugs and design limitations in Maya's python modues, maya.cmds and maya.melKeep code concise and readableAdd organization through class hierarchy and sub-modulesProvide documentation accessible via html and the builtin `help() functionMake it "just work"Supported Maya VersionsPyMEL supports four major versions of Maya:2020202220232024See the fullchangelog.Production ProvenSince its release in 2008, PyMEL has accumulated an impressive resume in both feature films and games, and is now bundled with every release of Maya.Here's what Seth Gibson of Bungie Studios, makers ofHalo, has to say:Having done production python code myself for many years, wrapping my head around Maya's native implementation took a little bit of time. With PyMel, I can think and write the python code and syntax I'm already used to, which speeds up my development time considerably. It's also going to help our other Technical Artists with their Python learning curve, since PyMEL's syntax is consistent with most other python packages. Kudos to the PyMel team for such a well thought out project!BSD LicensePyMEL is released under the BSD license, which is as open as open source gets. Your studio can freely use, contribute to, and modify this module with no strings attached.FeaturesAPI HybridizationPyMEL harnesses the API to create a name-independent representation of your object. This means that the annoying inconsistencies of string comparisons are over: no more worrying about short names versus long names, DAG paths, unique paths, instance paths... it's all handled intelligently for you. And what's more, if anything causes the name of your object to change it will automatically be reflected in your python object.PyMEL node classes now include hundreds of new methods derived from the API, but with the same intuitive and unified design as before. With PyMEL you get the benefits of API speed and versatility without the advanced learning curve.Improved Batch / Standalone SupportEver wonder why python scripts that work in the Maya UI or in Maya batch don't work in Maya's python interpreter?  Here's a possible explanation: in both GUI and batch modes Maya sources all of its lowest-level MEL scripts, like those that load user plugins, whereas mayapy andmaya.initializedoes not.PyMEL ensures that Maya is properly initialized when used from Maya's python interpreter, which makes runtime environments more consistent and your scripts more portable, which adds up to fewer bugs.Tighter MEL IntegrationExecuting a MEL script with arguments can be an unsightly mess when done the default way:defaultvalues=['one','two','three','four']maya.mel.eval('stringArrayRemoveDuplicates({"'+'","'.join(values)+'"})')So PyMEL provides a handy interface which makes calling MEL procedures just like calling a python function:PyMELvalues=['one','two','three','four']pm.mel.stringArrayRemoveDuplicates(values)Also, unlikemaya.mel.eval, PyMEL will give you the specific MEL error message in a python traceback, along with line numbers:>>>mel.myScript('foo',[])Traceback(mostrecentcalllast):...MelConversionError:ErroroccurredduringexecutionofMELscript:line2:Cannotconvertdataoftypestring[]totypefloat.Also, getting and setting MEL global variables is as easy as working with a dictionary:print(pm.melGlobals['gMainFileMenu'])pm.melGlobals['gGridDisplayGridLinesDefault']=2Powerful ClassesNodescamTrans,cam=pm.camera()# create a new cameracam.setFocalLength(100)fov=cam.getHorizontalFieldOfView()cam.dolly(-3)cam.track(left=10)cam.addBookmark('new')Attributess=pm.polySphere()[0]ifs.visibility.isKeyable()andnots.visibility.isLocked():s.visibility.set(True)s.visibility.lock()print(s.visibility.type())File pathsbackup all mb files in the current scene's directorybasedir=pm.sceneName().parentbackupDir=basedir/"backup"#slash op joins pathsifnotbackupDir.exists:backupDir.mkdir()forpathinbasedir.files('*.mb'):print("backing up: ",path.name)path.copy(backupDir/(path.namebase+".old"))Shape components and vectors/matricesselect all faces that point up in world spaces=pm.polySphere()[0]forfaceins.faces:ifface.getNormal('world').y>0.0:pm.select(face,add=1)optionVars dictionaryif'numbers'notinpm.optionVar:pm.optionVar['numbers']=[1,24,47]pm.optionVar['numbers'].append(9)numArray=pm.optionVar.pop('numbers')Who is PyMEL for?For the NoviceObject-oriented programming, like that provided by PyMEL, is more intuitive to learn because the functionality of an object is directly associated with the object itself.For an artist starting to program in Maya, the first question you might ask is "what can I do with this node?" Using a procedural approach, like that offered by MEL or maya.cmds, you'll have to dig through the thousands of MEL commands looking for the one that you want. For a camera node, thecameraMEL command is easy to find, but did you findorbit,track,dolly, andtumble, which also work on cameras?  What about the API methods?In PyMEL, all you have to do is typehelp(nt.Camera)in the python script editor to find out all the things a camera node can do, or just look up the Camera class in the PyMEL docs.For the MEL ScripterWhen we say PyMEL is concise and easy to read, we mean it.MELstring $sel[] = `ls -sl`;
string $shapes[] = `listRelatives -s $sel[0]`;
string $conn[] = `listConnections -s 1 -d 0 $shapes[0]`;
setAttr ( $conn[0] + ".radius") 3;PyMELpm.selected()[0].getShape().inputs()[0].radius.set(3)For the Technical DirectorFor those looking to master python in a production environment, PyMEL is more than a module for Maya scripting, it is a repository of example python code -- a self-contained pipeline demonstrating advanced python concepts like function factories, metaclasses, and decorators, as well as essential production practices such as parsing, pickling, logging, and unit testing.For those who are already masters of python and who naturally expect more out of a python package, PyMEL is for you, too. It was written for use in production by experienced programmers with a vision for how to add object-oriented design to Maya.Code Comparisonwith Melstring $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");

    // disconnect all connections to .sx
    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 a mel procedure
    myMelScript( `nodeType $x`, $trans );
}with default Pythonimportmaya.cmdsascmdsobjs=cmds.ls(type='transform')# returns None when it finds no matchesifobjsisnotNone:forxinobjs:print(mm.eval('longNameOf("%s")'%x))# make and break some connectionscmds.connectAttr('%s.sx'%x,'%s.sy'%x)cmds.connectAttr('%s.sx'%x,'%s.sz'%x)# disconnect all connections to .sxconn=cmds.listConnections(x+".sx",s=0,d=1,p=1)# returns None when it finds no match:ifconnisnotNone:forinputPluginconn:cmds.disconnectAttr(x+".sx",inputPlug)# add and set a string array attribute with the history of this transform's shapeifnotmm.eval('attributeExists "newAt" "%s"'%x):cmds.addAttr(x,ln='newAt',dataType='stringArray')shape=cmds.listRelatives(x,s=1)ifshapeisnotNone:history=cmds.listHistory(shape[0])else:history=[]args=tuple(['%s.newAt'%x,len(history)]+history)cmds.setAttr(*args,type='stringArray')# get and set some attributescmds.setAttr('%s.rotate'%x,1,1,1)scale=cmds.getAttr('%s.scale'%x)# maya packs the previous result in a list for no apparent reason:scale=scale[0]# the tuple must be converted to a list for item assignment:trans=list(cmds.getAttr('%s.translate'%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])# call a mel proceduremm.eval('myMelScript("%s",{%s,%s,%s})'%(cmds.nodeType(x),trans[0],trans[1],trans[2]))with Pymel# safe to import into main namespace (but only recommended when scripting interactively)frompymelimport*forxinls(type='transform'):# object oriented designprint(x.longName())# make and break some connectionsx.sx.connect(x.sy)x.sx.connect(x.sz)# disconnect all connections to .sxx.sx.disconnect()# add and set a string array attribute with the history of this transform's shapex.setAttr('newAt',x.getShape().history(),force=1)# get and set some attributesx.rotate.set([1,1,1])trans=x.translate.get()# vector math:trans*=x.scale.get()# ability to pass list/vector argsx.translate.set(trans)# call a mel proceduremel.myMelScript(x.type(),trans)With PyMEL, python and maya play well together.InstallationTo install, usepipwith themayapyinterpreter.Installing into Maya's site-packages directoryThe following commands will install PyMEL into the site-packages of the Maya application directory:On MacOS:/Applications/Autodesk/maya<Version>/Maya.app/Contents/bin/mayapy -m pip install pymelOn Linux:sudo /usr/autodesk/maya<Version>/bin/mayapy -m pip install pymelOn Windows:(from a command window running as Administrator)/Applications/Autodesk/maya<Version>/Maya.app/Contents/bin/mayapy -m pip install pymelInstalling into your user's site-packages directoryIf you do not have permissions to install into Maya's site-packages, you can
install into your user site-packages by adding the--userflag, e.g./usr/autodesk/maya<Version>/bin/mayapy -m pip install pymel --userTo see wherepymelwas installed you can run:mayapy -c "import pymel;print(pymel.__file__)"CaveatsOlder versions of Maya (2021 and earlier) may require installingpipbefore runningpip install:On Linux and Mac:curl https://bootstrap.pypa.io/get-pip.py | /usr/autodesk/maya2021/bin/mayapyPyMEL is developed and maintained byLuma Pictures.