Pymel Blog Rss Feed

Saturday, April 7, 2012

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

Computer programming allows us to automate and to repeat. As humans we don’t like to do tedious tasks. And we certainly don’t like to do them over and over. Over the course of these tutorials, one of my hopes is that you learn how to automate the tasks you regularly perform in Maya. My other hope is that you all practice good personal hygiene…after all, even though we sit for hours and hours and hours in front of a computer, that doesn’t mean that we shouldn’t bathe.


Loops allow us to repeat commands in a smooth, efficient manner. There are a couple of different types of loops in Python. There are 'while' loops and 'for' loops. Here, I’m going to focus on the one that I use the most: the 'for' loop:

for name in ["Leo", "Mikey", "Don", "Raph"]:
	print name


As you can see, this prints out the names of our favorite turtles.


The 'range' function comes in handy if you need to loop over a sequence of numbers.

#Result: [0, 1, 2, 3, 4]

range(2, 10)
#Result: [ 2, 3, 4, 5, 6, 7, 8, 9]

Then putting all this together, we can rewrite our first loop to include the 'range' function.

turtleList = ["Leo", "Mikey", "Don", "Raph"]
listSize = len(turtleList)
for i in range(0, listSize):
	print turtleList[i]

Now, I know that this second version looks more complicated than the first version. But it actually looks more like a loop that we’d use in production. Well it looks like one I’d use anyways. I feel that using the 'range' function in conjunction with the 'i' variable gives us a bit more flexibility. Speaking of 'i', you may wonder how you can put a variable inside the brackets.


Think of it this way: the first time through the loop, 'i' will be assigned the first value on the range function…in this case, zero. So turtleList[i] becomes turtleList[0] which yields "Leo". The next time through the loop, the range function will assign 1 to 'i'. This in turn will give us "Mikey" in the end. I think you get the idea. And like I said, although it seems a bit more complicated, using a separate variable for looping will come in handy for more complex operations.


Here’s a short script that moves the selected objects to random positions:

import random
objSel = selection()
numObj = len(objSel)
for i in range(0, numObj):
	xVal = random.uniform(-10,10)
	yVal = random.uniform(-10,10)
	zVal = random.uniform(-10,10)
	curObj = objSel[i]
	curObj.translate.set(xVal, yVal, zVal)

Here we make use of Python’s random number generator. You can learn more about random numbers in this tutorial. We store the selected objects into a list. Then we determine the size of the list. After generating some random numbers, we iterate (fancy talk for loop) over the list storing our current object into 'curObj'. 'curObj' is housing the object at the 'ith' index of the list. That’s what makes it current! Lastly, we move the current object to the random position.


Alright kids, that’s all I’ve got for now! Let me know what you think below!

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