;

Pymel Blog Rss Feed

Tuesday, May 22, 2012


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



Often times one of my students will ask me where I got my incredible sense of style. And then they'll ask me if white space matters in Python. After explaining to them that "I just got it like that", I'll proceed to tell them that usually, white space doesn't matter...usually.

 

For example, the following lines are identical:

 

sphere(r=5)
sphere(r = 5)
sphere( r = 5 )
sphere(       r      =     5     )

Now that last one is completely legal, unreadable, but legal. The white space has no bearing in the execution of the code.

 

Now when it comes to code blocks and indentation, it matters a great deal.

for i in range(0,10):
	sphere()
	polyCube()
	cone()
circle()
nurbsSquare()

We wrote a simple script that loops ten times. Each time it creates a sphere, a poly cube, and a cone. So in the end, you'll have 10 spheres, 10 cubes, and 10 cones. But take a close look the three lines that create the shapes on lines 2 through 4. They are indented respective to the 'for' keyword. It's this indentation that tells Python what code is part of the 'for' loop. Then when you unindent, you leave the code block. That means that Lines 5 and 6 are not part of the loop, as they are not indented.  That is why only one circle and one square are created.

 

Things get real interesting with you have indentation within indentation.

def TestUI():
	mainWindow = window(t="Test Window", rtf=True)
	mainLayout = columnLayout(cat=["both", 200], w=1000)
	with mainLayout:
		rcLayout = rowColumnLayout(nc=3, w=600, rs=[1,5], cs=[[2,5],[3,5]])
		with rcLayout:
			button(l="Button 1", w=190)
			button(l="Button 2", w=190)
			button(l="Button 3", w=190)
			button(l="Button 4", w=190)
			button(l="Button 5", w=190)
			button(l="Button 6", w=190)
		fLayout = frameLayout(l="Bla", w=600)
		with fLayout:
			radioButtonGrp(nrb=4, l="Test", la4=["Doe","Ray","Mi","Fa"])
	mainWindow.show()
TestUI()

It's important to keep your wits amidst all the indentation, as it can get confusing. Hopefully my color coded example below of the code above provides a visual representation of what code belongs to which code block.

 

def TestUI():

mainWindow = window(t="Test Window", rtf=True)

mainLayout = columnLayout(cat=["both", 200], w=1000)

with mainLayout:

rcLayout = rowColumnLayout(nc=3, w=600, rs=[1,5], cs=[[2,5],[3,5]])

with rcLayout:

button(l="Button 1", w=190)

button(l="Button 2", w=190)

button(l="Button 3", w=190)

button(l="Button 4", w=190)

button(l="Button 5", w=190)

button(l="Button 6", w=190)

fLayout = frameLayout(l="Bla", w=600)

with fLayout:

radioButtonGrp(nrb=4, l="Test", la4=["Doe","Ray","Mi","Fa"])

mainWindow.show()

TestUI()

 

So what are the ramifications of incorrectly indented code? Your family could disown you. It happened to me. And if that doesn’t scare you, if you don't indent your code properly, your code may not run. And even worse, it could run...but run incorrectly.

 

For example, here we create 10 randomly placed cubes:

import random
for i in range(0, 10):
    xVal = random.uniform(-20,20)
    yVal = random.uniform(-20,20)
    zVal = random.uniform(-20,20)
    curCube = polyCube()[0]
    curCube.translate.set(xVal, yVal, zVal)

But watch what happens once we move the object creation and object placement outside of the loop's code block.

import random
for i in range(0, 10):
    xVal = random.uniform(-20,20)
    yVal = random.uniform(-20,20)
    zVal = random.uniform(-20,20)
curCube = polyCube()[0]
curCube.translate.set(xVal, yVal, zVal)

The loop happens as before, but this time, only one cube is created. This is because the cubes aren't created in the loop. We loop first and then create one cube. Bummer.

 

In programming, there's nothing worse than code that runs incorrectly. You'd rather it break when you try to run it. That way you know that there's something wrong. So bottom line, make sure you indent properly.

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