;

Pymel Blog Rss Feed

Thursday, April 5, 2012


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



In this lesson, we’re going to learn about variables. If you don’t understand variables, you won’t understand the rest of the tutorials on this site. Now, that wouldn’t make you a bad person. You’d just be a person who didn’t understand Python. And you don’t want that. Huh? Would you? So you’d better listen up good!

 

You can think of variables as containers. Containers of what you ask? Containers of information! And they’re called variables because the information contained can, and usually does, change! Get it? The information inside a variable is variable! Now that that earth shattering truth has been bestowed upon you, let’s see some examples:

 

Ints

 

If you remember from grade school, integers are whole numbers. They’re the counting numbers. This includes zero and the negative numbers. So 1, 33, 0, and -67 all qualify as integers.

 

To create an int variable, we type:

intVarA = 7  #assigns t to intVarA
print intVarA  #prints 7

We’ve now created a variable called intVarA and assigned to it the value of 7. Remember: variables are containers. In this case our container is named intVarA; and if we were to look inside, we’d see the number 7.

 

Let’s continue:

intVarA = 7
intVarB = 5
intVarC = intVarA + intVarB  #adds intVarA and intVarB
print intVarC #prints 12

We’ve now added a little math. If you follow code, it should come as no surprise that Maya prints out a "12". Subtraction, multiplication and division all work almost the same way. I say almost because division reveals an important part about ints.

intA  = 15
intResult = intA / 2
print intResult  #prints 7

You may be surprised to find that Maya prints out a 7. What’s going on?!?!?! We all know that 15/2 is 7.5!!! You see, Python doesn’t round up or round down, it chops…with a giant meat cleaver. So a result of "17.9" would be truncated to 17. Keep this in mind when working with integers in Python. So…what about decimal values?

 

Floats

 

Syntactically, you work with floats the same way as with ints.

floatVarA = 8.6
floatVarB = 15.3
floatVarC = floatVarA + floatVarB
print floatVarC  #prints 23.9

As expected, Maya prints "23.9". Great! But what about our division problem?

floatA = 15.0
floatResult = floatA / 2
print floatResult  #prints 7.5

Maya now print out a "7.5"! All is right with the world. Let’s move on to a different subject…strings.

 

Strings

 

In Maya, all text information is represented by strings. Object names, user interface labels, all are stored as strings.

stringVar = "Hello World!"
print stringVar #prints "Hello World"

What kind of introduction to programming would this without the obligatory "Hello World" script/program? Sorry it took so long to get to it!

 

Anyways…strings can be "added" together too. I know what you’re thinking. You can’t add words! You’re right. You can’t add them…well…not in the mathematical sense. Watch and learn.

firstName = "Abe"
lastName = "Lincoln"
fullName = firstName + " " + lastName
print fullName #print "Abe Lincoln"

 

Maya prints "Abe Lincoln". We "added" to two words together. Or as we programmers like to say, we concatenated them. Notice that we even concatenated the space in between the two strings.

 

I could spend a lot of time on strings. Volumes have been written on them. They are extremely powerful. And you’d do well to feel comfortable working with them. We’ll learn more as we go. But for this intro tutorial, you have all you need to know.

 

Vectors

 

Vectors are mathematical abstractions used for storing sequences of numbers that are encoded with a magnitude and direction. And unlike that last sentence, they are totally easy to understand and to work with. In fact if you’ve been using Maya any amount of time, you’ve already encountered them. Vectors are found all over Maya. Translation, Velocity, Color, just to name a few, can all be represented with vectors. Python doesn’t have a robust built-in library for vectors. And rather than write our own, we’ll use PyMEL’s powerful vector class. Does that make us lazy? Yes it does. Good programmers are lazy. If you’re reinventing the wheel, you’re trying way too hard.

 

To use PyMEL’s Vectors we need to use the dt.Vector class.

vecVarA = dt.Vector(0,0,0)
vecVarB = dt.Vector(3,5,7)
print vecVarA #prints [0.0, 0.0, 0.0]
print vecVarB #print [3.0, 5.0, 7.0]

vecVarA was initialized to have values of 0 on X, Y, and Z. vecVarB was initialized to 3 on X, 5 on Y, and 7 on Z.

 

Vectors can be added and subtracted.

vecVarOne = dt.Vector(2,4,6)
vecVarTwo = dt.Vector(3,6,9)
vecVarThree = vecVarOne + vecVarTwo
print vecVarThree #print [5.0, 10.0, 15.0]

You should see that vecVarThree has values of 5, 10, and 15.

 

Now what if I wanted to access each individual element of a vector? What would I do? We use .x, .y, and .z

vecVarA = dt.Vector(30, 60,90)
floatVarA = 23.9
vecVarA.x = 20  #we assign 20 to the x element of vecVarA
floatVarB  = vecVarB.y #assign 60 to floatVarB
vecVarA.z = floatVar #assigns 23.9 to z component of vecVarA

 

I could go on and on about vectors. Cross products, dot products, I hardly know where to begin. But rather than go into crazy detail now, we’ll learn more about vectors in later tutorials.

 

Other Objects

 

Python is not typeless. We can check the values of the variables you create.

 

intVar = 7
print type(intVar)  #prints  type 'int'

Though definitely not an inclusive list, here are some other types:

type 'float'
type 'str'
class 'pymel.core.datatypes.Vector'
class 'pymel.core.nodetypes.Transform'
class 'pymel.core.general.MeshVertex'

 

Python assigns the type to the variable after you assign a value it. So as you can see, it’s just like mom used to say: It’s what’s on the inside that counts.

 

Lastly, I should point out that we could have used any valid name for a variable name. And you can use any convention you’d like.

 

varName = 7
var_name = 7
varname = 7
VARNAME = 7
Hsdlhfkjhd = 7

I prefer the first convention. But it’s up to you how you name your variables. Just make them descriptive. Use name like "objHeight", or "velocityDiff". Don’t use hsdlhfkjhd. I mean, unless you’re from the planet kdfkhhd. Then I suppose that hsdlhfkjhd is a common word:)

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