Pymel Blog Rss Feed

Sunday, May 20, 2012

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

Quite often, we don't really want to create new objects. Instead, what we really want to do is manipulate existing objects. PyMEL has graciously provided us with a function that easily allows us to store existing objects into PyMEL objects. We can easily create nodes by using the PyNode() function.


So assuming we have an exisiting transform node in our scene called 'dog'. We could say:


pymelDog = PyNode("dog")  #store the tranform node dog into a python variable called pymelDog.
print type(pymelDog) #prints pymel.core.nodetypes.Transform
pymelDog.scale.set(5,5,5) #set the scale uniformly to 5

PyMEL automatically instantiates, that is creates, a Transform object from the transform node called "dog". Then we are free to manipulate the transform node using the Transform Object.


We can do the same for other type of nodes. For example, let's say we have an air field randomly sitting in our scene called, I don't know, 'airField1'. We use the same PyNode function to convert it into a PyMEL object. And then we can perform PyMEL operations on the airfield node using our newly created airfield object.

myAirField = PyNode("airField1") #create an pymel.core.nodetypes.AirField object
print type(myAirField) #prints pymel.core.nodetypes.AirField
myAirField.setSpeed(0.5) #set the speed to 0.5

I should note that with default Maya python we'd have to do the following to change the speed of an air field.


cmds.air(myAirField, e=True, s=0.5)



User interface types have an analogous function called uitypes.PyUI(). The following converts an existing UI element called button1 into a PyMEL object:

pyMelButton = uitypes.PyUI(button1)

For a more detailed explanation of the relationship between Objects and PyMEL's classes, check this out.


With the PyNode() function, you too can prevent unnecessarily cryptic code!

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