Python: How to Start Scripting in Blender

thepythonconsole

Ok, let’s start up by opening up the Python Console. Go to your Editor Type selector, and it’ll be the first option that pops up.

Now, let’s do some automation.

Say, for instance, you want a bunch of little lights placed at exact intervals around a mesh—Python can do that real quick with loops. Loops are pieces of code that make something happen over and over again.

Here’s the format for a Python loop:

for index in range (X):
do this

Index starts at 0. Range(X) tells Blender how many times total you want the loop to run. Don’t worry if this sounds confusing. It’s easier to get with an example.

For a simple starter Python code, let’s put in 10 lights, all nice and lined up. Unfortunately you can’t just type:

for index in range (10):
make a light and be snappy about it

Blender will get pissed and spit out an error. But if you go to the Object Operators section of the Blender API Documentation page, it’ll tell you how to refer to objects in the code and how to customize pretty much everything about them.

A quick warning. The Blender documentation looks a bit intimating at first. Well, I’ll just show you. The LAMP_ADD entry looks like this.

blender

That’s a lot of text. So I’m just gonna take the 3 most necessary pieces.

blenderedited

There are 5 types of lamps—I’m going with a point light. And I want to specify location, otherwise they’ll all get stuck in a cluster.

My 3D bug is sitting square on top of the X/Y plane. The lights need to be above his head though, so I’m setting the Z value to 4. Y can remain a constant at 0. The X value needs to go up by 1 with each loop in order to make a line, so we can use the built-in INDEX variable to keep adding 1 with each round of the loop.

Here’s the code:

loop

Plug it into the console and press ENTER twice.

loopcode
If it worked, you’ll see ten {‘FINISHED’} lines below the code and 10 lights in the scene.

finishedloop
And here’s the little bugger all lit up:

rendered

You might be wondering, what if I want to save a piece of code as a Python file so I can run it on a different scene without having to retype everything?

You can do that.

Instead of the Python Console, go to the Text Editor.

Oh. Blender has a text editor.

Once you’ve got that screen, you can hit New, type in your script, and then save it as a .py file.

Python

Saving Scripts in Blender

And presto. You’ll have lights. Saved lights to run anytime you please.

Ready to try more?

Try out modules in this tutorial on generating randomness in Blender.

 

2 thoughts on “Python: How to Start Scripting in Blender

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s