Sub topic 17
Transforming objects with Python
This chapter assumes you can navigate Blender’s 3D Viewport, understand the X, Y, and Z axes, and run a script in Blender’s Scripting workspace. The examples use Object Mode and the default Blender Python API, bpy.
Blender stores an object’s transform in three main properties:
| Property | Meaning | Typical value |
|---|---|---|
| `location` | Position in world space | `(1.0, 2.0, 0.5)` |
| `rotation_euler` | Rotation around X, Y, and Z | `(0.0, 0.0, 0.785)` |
| `scale` | Size multiplier on each axis | `(2.0, 2.0, 2.0)` |
The values are usually three-component vectors. Euler rotation values are in radians, even though Blender’s interface often displays degrees. Use math.radians() when you want to enter degrees in a script.
Reading and setting transforms
The active object is the object that Blender treats as the primary selected object. The following script reads its current transform:
import bpy
obj = bpy.context.active_object
if obj is None:
raise RuntimeError("Select an object first")
print("Name:", obj.name)
print("Location:", tuple(obj.location))
print("Rotation in radians:", tuple(obj.rotation_euler))
print("Scale:", tuple(obj.scale))You can assign new values directly. This changes the object’s transform without changing the underlying mesh vertices:
import bpy
import math
obj = bpy.context.active_object
if obj is None:
raise RuntimeError("Select an object first")
obj.location = (1.0, 2.0, 0.5)
obj.rotation_euler = (0.0, 0.0, math.radians(45))
obj.scale = (2.0, 2.0, 2.0)This is similar to using G, R, and S in the viewport followed by numeric input. Assignment is useful when you need repeatable positions or want to generate many objects with the same layout.
Selection and context
Python can also reproduce selection work. Selecting an object does not automatically make it active, so set both states when a later operation depends on the active object:
import bpy
bpy.ops.object.select_all(action='DESELECT')
obj = bpy.data.objects.get("Cube")
if obj is None:
raise RuntimeError("No object named Cube exists")
obj.select_set(True)
bpy.context.view_layer.objects.active = objbpy.data.objects.get() returns None if the name is missing. Checking for this case prevents a confusing attribute error.
Applying transforms
A transform is unapplied when its values remain on the object. For example, an object may have a scale of (2, 2, 2) while its mesh data still contains the original dimensions. Applying rotation and scale transfers those changes into the mesh and resets the object properties to identity values:
import bpy
obj = bpy.context.active_object
if obj is None or obj.mode != 'OBJECT':
raise RuntimeError("Select an object in Object Mode")
bpy.ops.object.transform_apply(
location=False,
rotation=True,
scale=True,
)This operator depends on Blender’s context: the correct object must be active, selected, and in Object Mode. Applying transforms can affect modifiers, physics, and export results, so do it deliberately.
Exercise
What value should you assign to rotate an object 90 degrees around the Z axis?
Show answer
Use obj.rotation_euler.z = math.radians(90) after importing math.