Pymel Blog Rss Feed

Saturday, March 24, 2012

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

So far we’ve covered variables, which are great for holding singular pieces of information. But what if we wanted to store a list of data? As you can guess by the title of this tutorial, we’d use lists! I know, how anticlimactic. Nevertheless, on with the tutorial…


Lists provide a cool way to store lots of data. For example, let’s say we wanted to store the names of four fun loving, pizza eating, crime fighting, giant turtles. We could do as before store them all separately:


turtle1 = "Leo"
turtle2 = "Don"
turtle3 = "Mike"
turtle4 = "Raph"

Or we could use lists! Which we’ll do because otherwise the tutorial would end here. And that would leave us with a lot of awkward silence.


teamTurtle = ["Leo", "Don", "Mike", "Raph"]
print teamTurtle #prints  ["Leo", "Don", "Mike", "Raph"]

Tada! We’ve now created a list of strings…But before you start getting too excited, let’s see how we can access individual elements. We access individual elements of a list by using the element’s index. This index starts with zero.

currentTurtle = teamTurtle[0]
print currentTurtle #prints "Leo"
currentTurtle = teamTurtle[3]
print currentTurtle #prints "Raph"

We can alter the list with the same zero-based index system.


teamTurtle[1]  = "Donatello"
print teamTurtle #prints  ["Leo", "Donatello", "Mike", "Raph"]

I say zero-based because with lists, indices start with zero, not one. As a side note, the word "index" is a rare type of word with two plural forms: "indices" and "indexes". This is site is full of all sorts of useful information huh?!


Knowing the length of a list is something that will become very important. We use the len() function to find out how long a list is.

listSize = len(teamTurtle)
print listSize #prints 4

Appending to lists is easy as pie, pizza pie:


print teamTurtle #prints  ["Leo", "Donatello", "Mike", "Raph", "Splinter", "April"]
listSize = len(teamTurtle)
print listSize #prints 6

I know what you’re thinking. Splinter and April aren’t turtles. But I say that they’re part of the team!


Now you can create lists of all sorts of structures, not just strings. You can have a list of ints, floats, vectors, and as we will soon learn, PyNodes.

intList = [0, 2, 4, 6, 8, 10, 12] #creates a list of  ints
floatList = [1.1, 3.4, 5.2, 134.67, -19.7] #creates a list of floats

vec1 = dt.Vector(2,4,6)
vec2 = dt.Vector(-9, -9, -9)
vec3 = dt.Vector(10, 20, 30)
vec4 = dt.Vector(100, -300, 5.5)
vecList = [vec1, vec2, vec3, vec4]

objList = selected() #creates a list full of the selected objects in the scene.


If you don’t understand the last line, don’t worry. We’ll get to it in later tutorials. For now, just know that 'objList' contains a bunch of PyNodes.


I hope that you get the basics of lists. Of course, please 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