Today we’ll see how to use the three basic transform operators in Blender Python code. There are lots of transform operators, but we’ll just stick to moving, rotating and scaling objects in both global and local coordinates. I’m going to keep it simple. Although each of the functions discussed below comes with a whole lot of optional parameters, I’ll just use the most basic ones. I’ll be using the 2.81 version of Blender.
If you want to watch the video first, here it is:
And here are the steps:
Table of Contents
Step 1 – Go to Scripting Workspace to Use Blender Python
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 – Explore the Blender Python Functions You’re Going to Use
Now let’s select the default cube. This way all the operations will apply to this object. Now, the functions that we can use to move, rotate and scale objects in Python are:
bpy.ops.transform.translate
bpy.ops.transform.rotate
bpy.ops.transform.resize
respectively. Let’s discuss them one by one. First, we’re going to use the global coordinates, which are the default ones.
Step 3 – Translate in Code
Let’s start by translating the cube -4 units along the X axis (so to the left), 2 units along the Y axis and 5 units along the Z axis. Here’s how we can do it in code:
bpy.ops.transform.translate(value=(-4, 2, 5))
If you run this script (A), you will notice the cube move in the 3D View (B).
Step 4 – Scale in Code
Now comment out the last line of code because we don’t want to move the cube again, which would happen if you ran the script again. Now we want to scale the cube 5 times along the X axis and 3 times along the Z axis. There should be no scaling along the Y axis, so the Y-component will be set to 1:
bpy.ops.transform.resize(value=(5, 1, 3))
If you run this code, you will see the cube resize in the 3D View.
Step 5 – Rotate in Code
Finally, let’s rotate the cube. Well, as far as rotating is concerned, the unit we use is radians. Before I show you how to use degrees instead, let’s have a brief recap on how to use radians.
To keep things short, a straight angle, so an angle of 180 degrees is an angle of pi radians, where pi is the constant equal to about 3.14. So, if you need to rotate your object 45 degrees, for example, which is ¼ of the straight angle, all you have to do is divide pi by 4, which will give you about 0.785.
If you want to be very precise, the approximations above may not be enough. You may want to use the pi constant defined in the math module. If so, just import the math module by adding this line to your code:
import math
And now you can use it to rotate your object. So, let’s use it to rotate the cube 45 degrees (pi/4 radians) around the Y axis. Don’t forget to comment out the scaling code before, because we don’t want to scale again.
Here’s the full script up to now:
import bpy
import math
#bpy.ops.transform.translate(value=(-4, 2, 5))
#bpy.ops.transform.resize(value=(5, 1, 3))
bpy.ops.transform.rotate(value=math.pi/4, orient_axis='Y')
If you run the script, the cube will be rotated:
Step 6 – Use Degrees Instead of Radians in Blender Python
And now, as promised, I’ll show you how to use degrees instead of radians. It’s dead simple. You also need the math module, which happens to have the radians function. You can pass the angle in degrees as an argument to this function and it will convert it to radians for the internal use of the rotate function. So, let’s comment out the previous line of code and let’s rotate the cube 45 degrees around the X axis this time. Here’s how to do that:
bpy.ops.transform.rotate(value=-math.radians(45), orient_axis='X')
And here’s the result that you should get when you run your code:
Step 7 – Transformations in Local Space
Good. All the transformations were in global space so far. Now let’s see how to apply them in local space.
All you have to do is pass the orient_type keyword argument to the appropriate function and set it to LOCAL. So, let’s move the cube, or rather what has become of it so far, -15 units along its local Y axis. Comment out the previous code and type this line of code:
bpy.ops.transform.translate(value=(0, -15, 0), orient_type='LOCAL')
This is what you get when you run this code:
Step 8 – Scale in Local Space
Now let’s scale the cube 4 times along its local Y axis. Comment out the previous line of code and type this one:
bpy.ops.transform.resize(value=(1, 4, 1), orient_type='LOCAL')
Here’s the result:
Step 9 – Rotate in Local Space
Finally, let’s rotate the cube 60 degrees around its local Z axis. Don’t forget to comment out the previous line of code first. Here’s the code:
bpy.ops.transform.rotate(value=math.radians(60), orient_axis='X', orient_type='LOCAL')
Here’s the result:
As you can see, moving, rotating and scaling objects in Blender Python is pretty straightforward.