;

Pymel Blog Rss Feed

Friday, April 6, 2012


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



In this tutorial, we’re going to talk about conditions. I would say that conditional programming was the coolest thing since sliced bread, but to be perfectly honest with you, I think that sliced bread is pretty cool. Still, conditions come in a close second. And that’s because, conditions add power to programming:

intVar = 17
if(intVar > 10):
	print "intVar is more than 10"
else:
	print "intVar is less that 10"

This frivolous example shows off some of the basics of conditional programming. We set our variable to 17 and then we check to see if it’s greater than 10 or not. And then we print a glaringly obtuse message. Try changing intVar and checking out the result.  It should come as no surprise that values greater than 10 will produce one message, and values less than 10 will produce another.

 

Note the syntax. All if statements have some condition being checked inside of the parenthesis. And the colons are mandatory. Lastly, indentation is very important. So important you should review the indentation tutorial if you don’t believe me.

 

So let’s try a more Maya centric example, shall we?  Assuming you have an object called 'pSphere1' in your scene:

sphereObj = PyNode("pSphere1")
objPosition = sphereObj.tx.get()
if(objPosition > 0):
	sphereObj.translate.set(10,10,10)
	sphereObj.scale.set(5,5,5)
elif(objPosition < 0):
	sphereObj.translate.set(-10,-10,-10)
	sphereObj.scale.set(0.5, 0.5, 0.5)
else:
	sphereObj.translate.set(0,0,0)
	sphereObj.scale.set(1, 1, 1)

Check the PyNode tutorial if you’re confused about the use of the Pynode method. So here, we check to see if the X translate is positive. Then I add a new wrinkle: the elif statement. Here we check an alternative condition. Then we finally have our else statement.

 

Think of it this way:

 

• if (condition):
– Code to execute if this condition is true
• elif(alternate condition):
– Code to execute if this condition is true
• elif (alternative condition):
– Code to execute if this condition is true
…(more conditions if necessary)…
• else:
– Code to execute if all else fails

 

The elifs and the else are optional. But if you can’t have an elif or an else without an if. Got it? Good.

 

It is also possible to check more than one condition at a time.

if((3<4) and (3<5)):
	print("Both are true!")
else:
	print("One is false!")

This will print "both are true". Here I’m using the 'and' operator. This requires that both conditions be true for the entire condition to be true.

 

Let’s modify this a bit:

if((3<4) or (3>5)):
	print("One or more are true!")
else:
	print("Both are false!")

This prints one or both are true. By using the 'or' operator, we require only one of the conditions to be true. Nice huh?

 

Lastly, you can have an if statement within an if statement.

 

outerCondition = True
innerCondition = True
if(outerCondition==True):
	#do some stuff
	#do some more stuff
	if(innerCondition==True):
		print "outerCondition and innerCondition are both true!"

First we check to see if 'outerCondition' is true. If it is, we do what we need to do and then we do something interesting…we check another condition. Then if this inner condition is true, we execute the print statement. You can nest as many if statements as you want. Just be careful because it can get confusing if you get more than 2 levels deep.

 

Here’s a table of some of the conditions you’ll find in python:

 

 

Operator

Function

< less than
> greater than
<= less than or equal
>= greater than or equal
== equal to
!= not equal to
and And
or Or
not Not

 

Conditional statements allow us to make decisions based on what’s happening in our scene. That makes conditions extremely excellent tools that we will be executing extensively excluding extenuating circumstances. And now I’m done with this tutorial. So please excuse me.

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