;

Pymel Blog Rss Feed

Saturday, May 19, 2012


Prior to starting any of these lessons be sure to import the PyMEL library. See here for why and how.



I wanted to keep these tutorials fairly Maya and PyMEL centric. I didn't want to delve too much into topics like Object Oriented Programming. If you want to know about that stuff, there are other resources on the interwebs that you can peruse. But I do think it's important to know how using PyMEL differs from using Default Maya Python (DMP).

 

Let's start with some basics. We'll create a sphere using both DMP and PyMEL.

 

import maya.cmds as cmds
defaultSphere = cmds.sphere()[0]
print type(defaultSphere) #prints unicode

If you're wondering why we have the '[0]', head on over to the lists tutorial. When you create a sphere this way, it returns a unicode object. For our purposes, you can consider this to be a type of string. If you don't know what a string is, check here.

 

Now if we wanted to move this newly created sphere we'd have to do something like the following:

import maya.cmds as cmds
cmds.xform(defaultSphere, ws=True, t= [1,2,3])

Now let's try the same using PyMEL

 

pymelSphere = sphere()[0]
print type(pymelSphere ) #prints class 'pymel.core.nodetypes.Transform'

Now when we create a sphere, we get an object of type 'pymel.core.nodetypes.Transform'. Again, I don't want to go too far into classes and objects and instantiation. But this allows us to move the sphere by doing the following:

pymelSphere.translate.set(1,2,3)

Now doesn't that read a bit easier? This ease of attribute manipulation extends beyond transformation attributes. Let's say we had an attribute on our spheres called rate, and we wanted to change it to 5.

#DMP way
import maya.cmds as cmds
cmds.setAttr(defaultSphere + ".rate", 5)

#PyMEL way
pymelSphere.rate.set(5)

How are we able to pull off this magic with PyMEL? Because PyMEL deals with objects and DMP deals with strings. So in the case of setting the rate attribute, with DMP we have to concatenate the string ".rate" on to defaultSphere. But with PyMEL, we have a Transform object called pymelSphere that has a rate attribute that we can set to 5. Very smooth.

 

For a little more detail, check out the documentation on the pymel's Transform object. Here you'll find all the methods for the Transform class. We've already seen the translate() method earlier. And you can find the rest here. We can easily show and hide tranform objects using show() and hide(). We can get the bounding box of an object using getBoundingBox(). And we can get the shape node attached to a transform using getShape(). And as you can see from the list, I'm just scratching the surface.

 

So basically any object that is a Transform object has access to these methods.

polyCubeObj = polyCube()[0] #create and store a transform object
polyCubeObjShapeNode = polyCubeObj.getShape() #get the shape node of the transform object.
polyCubeObj.getBoundingBox()

We can easily create transform nodes by using the PyNode() function. So assuming we have an exisiting object in our scene called 'dog'. We could say:

pymelDog = PyNode("dog")  #store the node dog into a python variable called pymelDog.
pymelDog.scale.set(5,5,5) #set the scale uniformly to 5

I'll show you a few more examples using some types other than transform nodes.

 

The Button Object

myWindow = window(t="Sample Window")
mainLayout = columnLayout()
with mainLayout:
    myButton = button(l="My Button!")  #create a button object
    myButton.setEnable(False)  #use the setEnable() method of a button object
myWindow.show()

The PolyMoveVertexObject

import random
curObj = selected()[0] #store the first selected object
numVertex = len(curObj.vtx)
for i in range(0, numVertex):
	xVal = random.uniform(-0.03, 0.03)
	yVal = random.uniform(-0.03, 0.03)
	zVal = random.uniform(-0.03, 0.03)
	randVector = dt.Vector(xVal, yVal, zVal)
	polyMoveVtxObj = polyMoveVertex(curObj.vtx[i])[0] #create a new PolyMoveVertex object
	polyMoveVtxObj.setTranslate(randVector) #invoke the setTranslate Method
	delete(curObj, ch=True)

Generally speaking using a function found in the python command reference will yield a PyMEL object. For example:

myButton = button(l="My Button!")  #create a button object
polyMoveVtxObj = polyMoveVertex(curObj.vtx[i])[0] #create a new PolyMoveVertex object

I know that this can be kind of hard to wrap your head around. But once you get used to the idea of using PyMEL objects, you won't want to do without them. Let me know if I can clear anything up!

My comment system is powered by Disqus. And they require you to put in a name and an email. But I'd love to hear what you think. So if you want to comment anonymously, just put any name and test@test.com for your email.

blog comments powered by Disqus