Today we’re going to do some very basic stuff in Blender, add some objects. We’re going to do it in Python, though. I’m using the 2.82 version of Blender. So, let’s get started and see how to add a Blender object in Python.
If you want to see the video version first, here it is:
And here are the steps:
Table of Contents
Step 1 – The First Blender Object – Cube
Let’s save our file right away. You can name it Adding Objects in Python.blend or whatever you want.
Now the first thing we want to do is to go to the Scripting workspace and create a new script. Then we have to import the bpy module:
import bpy
Step 2 – Type In the Python Code to Create the Cubes
Select all in the 3D View and delete. We’re going to add some cubes in a loop. The function that you use to add a cube is:
bpy.ops.mesh.primitive_cube_add
You can use it with many interesting arguments, but I’m going to keep it simple. Type in the following code. The comments are just for you to make it clear what is going on. Now the code:
# We want to create 100 cubes along the X axis, from x = -250 up to x = 250, one cube
# every 5 units, that's why we're going to multiply the loop variable i by 5 in the first
# line inside the loop. The second argument in the range function must be 51, because it's
# not included.
for i in range (-50, 51):
x = i * 5
# The cubes will be scaled along the Y-axis in a parabolic pattern, so by a factor
# equal the the square of i.
depth = i ** 2
# We also need a parabolic pattern along the Z-axis.
height = i ** 2
# This is the actual line of code that adds the cube.
bpy.ops.mesh.primitive_cube_add(size=0.1, location=(x, 0, 0))
# And here we're scaling the cube along the X, Y and Z axes (the constant value of 20
# for X, depth and height for Y and Z respectively).
bpy.ops.transform.resize(value=(20, depth, height))
Step 3 – Run the Cubes Script
If you run this script, this is what you should get:
Step 4 – The Second Blender Object – Sphere
Select and delete all in the 3D View editor. Then in the text editor add a new script. This time we’re going to add ico spheres in a loop that will be located along a cosine wave. The function you need to add an ico sphere is the following:
bpy.ops.mesh.primitive_ico_sphere_add
Step 5 – Type In the Python Code to Create the Spheres
Type in the following code. The comments make it clear what’s going on:
import bpy
# We're going to need the cos function and the pi constant from the math module.
import math
# We're going to add ico spheres along the X axis. The Z values are calculated
# using the cosine function and the value of x.
for i in range (-200, 201):
# The values of x should be equal to multiples of pi/20 radians.
x = math.pi/20 * i
# The z values will be multiplied by 10 to make the pattern more visible.
z = math.cos(x) * 10
# This is the actual line of code that adds the ico spheres.
bpy.ops.mesh.primitive_ico_sphere_add(radius=0.5, location=(x, 0, z))
If you want to learn more about trig functions in Python, you can go here.
Step 6 – Run the Spheres Script
This is what you will get when you run the script:
Step 7 – The Info Editor
As you just saw there are different arguments that you can use with different functions. If you want to add another primitive shape like a UV sphere, cone or torus for example, you can look up the function you need in documentation. But there is a faster way. Suppose you want to add a torus in code. If you are in the Scripting workspace, there is the Info editor where everything you do is logged, regardless of whether you do it manually or in code.
Step 8 – Watch the Code
Before we add the torus, let’s select and delete all in the 3D View editor. The deletion of 401 objects is logged in the Info editor, but, what’s even more important for us right now, there’s the Python code that was executed. Here you can see the code that was run to select all and then delete the selected elements.
Step 9 – Manually Add a Torus Blender Object
Now add a torus manually in the 3D View editor, either using the Add menu or the Shift + A hotkey. When you do that, you will immediately notice in the Info editor that the code run to create the torus is there.
Step 10 – Copy and Tweak the Code
Click on the code in the Info editor (A) and copy it using the Ctrl+C shortcut. Paste it in the Text editor. As you can see, the line of code is pretty long. This is because there are quite a few arguments passed to the function. If you want to see the whole line, just click the Word Wrap button (B). Now you know what function you need and you can tweak it, leaving out the arguments you don’t need for example, and use it in your code.
As you can see, adding objects in Python is pretty straightforward.
Very interesting.
Thanks a lot 🙂