Computer
Graphics
Lab.2
Rotating an Object in Blender using Python (bpy)
To rotate an object in Blender using Python, we modify its rotation properties. The rotation
can be set using: Euler Angles (rotation_euler) – Defines rotation in radians
In Blender's rotation_euler, the .x, .y, and .z properties represent the object's rotation
around the three axes:
• .x → Rotation around the X-axis (Roll)
• .y → Rotation around the Y-axis (Swing)
• .z → Rotation around the Z-axis (Spin)
import bpy
[Link].primitive_cube_add()
cube = [Link].active_object
cube.rotation_euler.x = 45
Or you can use rotation_euler = (x, y, z) for setting multiple axes at once.
import bpy
[Link].primitive_cube_add()
cube = [Link].active_object
cube.rotation_euler = (45, 45, 45)
Note:
That 45 at Blender think its 45 rad which equal 2578° instead of 45°
Degrees vs. Radians
Blender internally uses radians for rotation, not degrees. But we usually think in degrees (like
90°, 180°, etc.), so we need to convert them.
• Degrees (°) → Common unit (0° to 360°)
• Radians (rad) → Used in math and programming (0 to 2π rad)
𝝅
Conversion Formula: 𝒓𝒂𝒅𝒊𝒂𝒏𝒔 = 𝒅𝒆𝒈𝒓𝒆𝒆𝒔 ×
𝟏𝟖𝟎
Convert Degrees to Radians in Python
You can convert degrees to radians in Python using math module.
import math
radians_value = [Link](45)
Example: Convert and Apply in Blender
import bpy
import math
[Link].primitive_cube_add()
cube = [Link].active_object
# Rotate the cube using Euler Angles (in radians)
cube.rotation_euler = ([Link](45), [Link](45), [Link](45))
Animating Rotation with Keyframes
We will create a cube and animate it rotating 135 degrees on the Z-axis over time
import bpy
import math
# Create a cube
[Link].primitive_cube_add()
cube = [Link].active_object
# Insert Initial Rotation (Frame 1)
cube.rotation_euler = (0, 0, 0) # Start with no rotation
cube.keyframe_insert("rotation_euler", frame=1)
# Insert 90-degree Rotation on the Z-Axis (Frame 100)
cube.rotation_euler = (0, 0, [Link](135)) # Rotate 135° on Z-axis
cube.keyframe_insert("rotation_euler", frame=100)
Frame 1 Frame 100
Rotate an object up to and then reverse direction of the rotation
We will animate a cube to rotate counterclockwise first and then return to its original
position.
Steps of the Animation
1. Start with a cube at 0° rotation (Frame 1).
2. Rotate 90° counterclockwise on the Z-axis (Frame 50).
3. Rotate back to 0° (original position) (Frame 100).
import bpy
import math
[Link].primitive_cube_add()
cube = [Link].active_object
# Insert Initial Rotation (Frame 1) - No rotation
cube.rotation_euler = (0, 0, 0)
cube.keyframe_insert("rotation_euler", frame=1)
# Rotate Counterclockwise (Positive Rotation) - Frame 50
cube.rotation_euler = (0, 0, [Link](90))
cube.keyframe_insert("rotation_euler", frame=50)
# Rotate Clockwise (Back to Original) - Frame 100
cube.rotation_euler = (0, 0, [Link](0))
cube.keyframe_insert("rotation_euler", frame=100)
In Blender's coordinate system, a positive rotation is counterclockwise, and a negative
rotation is clockwise when looking along the axis in the positive direction.
To rotate an object clockwise up to a certain frame and then reverse direction, follow these
steps:
1. Start with no rotation at frame 1.
2. Rotate the object clockwise (negative rotation) on the Z-axis at frame 50.
3. Reverse direction (rotate counterclockwise) at frame 100.
[Link].primitive_cube_add()
cube = [Link].active_object
# Insert Initial Rotation (Frame 1) - No rotation
cube.rotation_euler = (0, 0, 0)
cube.keyframe_insert("rotation_euler", frame=1)
# Rotate Clockwise (Negative Rotation) - Frame 50
cube.rotation_euler = (0, 0, [Link](-90))
cube.keyframe_insert("rotation_euler", frame=50)
# Rotate Counterclockwise (Positive Rotation) - Frame 100
cube.rotation_euler = (0, 0, [Link](90))
cube.keyframe_insert("rotation_euler", frame=100)
Animate Two Cubes Moving and Rotating in Opposite Directions
Create an animation where two cubes start at different positions, move toward each other's
starting points while rotating, then return to their original positions with a full rotation.
Steps
1. Set initial keyframes (Frame 1) for position and rotation.
2. Move and rotate the cubes (Frame 1 → Frame 125).
3. Swap positions and continue rotation (Frame 125 → Frame 250).
4. Insert final keyframes at Frame 250 to complete the animation.
Full Code
import bpy
import math
[Link].primitive_cube_add()
c = [Link].active_object # First cube
[Link].primitive_cube_add()
c1 = [Link].active_object # Second cube
# Set Initial Keyframes (Frame 1)
[Link] = (5, 5, 5)
[Link] = (-5, -5, -5)
c.keyframe_insert("location", frame=1)
c1.keyframe_insert("location", frame=1)
c.keyframe_insert("rotation_euler", frame=1)
c1.keyframe_insert("rotation_euler", frame=1)
# Swap Positions and Rotate (Frame 1 → Frame 125)
[Link] = (-5, -5, -5)
[Link] = (5, 5, 5)
c.rotation_euler.x = [Link](180)
c1.rotation_euler.x = [Link](-180)
c.keyframe_insert("location", frame=125)
c1.keyframe_insert("location", frame=125)
c.keyframe_insert("rotation_euler", frame=125)
c1.keyframe_insert("rotation_euler", frame=125)
# Return to their original positions and Rotate (Frame 125 → Frame 250)
[Link] = (5, 5, 5)
[Link] = (-5, -5, -5)
c.rotation_euler.x = [Link](360)
c1.rotation_euler.x = [Link](-360)
c.keyframe_insert("location", frame=250)
c1.keyframe_insert("location", frame=250)
c.keyframe_insert("rotation_euler", frame=250)
c1.keyframe_insert("rotation_euler", frame=250)