0% found this document useful (0 votes)
3 views

Lab.2_Computer Graphics

The document provides a guide on how to rotate objects in Blender using Python (bpy), focusing on the use of Euler angles and the conversion between degrees and radians. It includes examples of rotating a cube, animating rotations with keyframes, and creating animations with two cubes moving and rotating in opposite directions. Key concepts include the importance of radians in Blender and the steps to animate object rotations effectively.

Uploaded by

vohev62095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab.2_Computer Graphics

The document provides a guide on how to rotate objects in Blender using Python (bpy), focusing on the use of Euler angles and the conversion between degrees and radians. It includes examples of rotating a cube, animating rotations with keyframes, and creating animations with two cubes moving and rotating in opposite directions. Key concepts include the importance of radians in Blender and the steps to animate object rotations effectively.

Uploaded by

vohev62095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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
bpy.ops.mesh.primitive_cube_add()
cube = bpy.context.active_object

cube.rotation_euler.x = 45

Or you can use rotation_euler = (x, y, z) for setting multiple axes at once.

import bpy
bpy.ops.mesh.primitive_cube_add()
cube = bpy.context.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 = math.radians(45)

Example: Convert and Apply in Blender

import bpy
import math

bpy.ops.mesh.primitive_cube_add()
cube = bpy.context.active_object

# Rotate the cube using Euler Angles (in radians)


cube.rotation_euler = (math.radians(45), math.radians(45), math.radians(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
bpy.ops.mesh.primitive_cube_add()
cube = bpy.context.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, math.radians(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

bpy.ops.mesh.primitive_cube_add()
cube = bpy.context.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, math.radians(90))
cube.keyframe_insert("rotation_euler", frame=50)

# Rotate Clockwise (Back to Original) - Frame 100


cube.rotation_euler = (0, 0, math.radians(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.

bpy.ops.mesh.primitive_cube_add()
cube = bpy.context.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, math.radians(-90))
cube.keyframe_insert("rotation_euler", frame=50)

# Rotate Counterclockwise (Positive Rotation) - Frame 100


cube.rotation_euler = (0, 0, math.radians(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

bpy.ops.mesh.primitive_cube_add()
c = bpy.context.active_object # First cube

bpy.ops.mesh.primitive_cube_add()
c1 = bpy.context.active_object # Second cube

# Set Initial Keyframes (Frame 1)


c.location = (5, 5, 5)
c1.location = (-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)


c.location = (-5, -5, -5)
c1.location = (5, 5, 5)
c.rotation_euler.x = math.radians(180)
c1.rotation_euler.x = math.radians(-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)
c.location = (5, 5, 5)
c1.location = (-5, -5, -5)
c.rotation_euler.x = math.radians(360)
c1.rotation_euler.x = math.radians(-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)

You might also like