Lab.2_Computer Graphics
Lab.2_Computer Graphics
Graphics
Lab.2
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
Note:
That 45 at Blender think its 45 rad which equal 2578° instead of 45°
𝝅
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)
import bpy
import math
bpy.ops.mesh.primitive_cube_add()
cube = bpy.context.active_object
import bpy
import math
# Create a cube
bpy.ops.mesh.primitive_cube_add()
cube = bpy.context.active_object
import bpy
import math
bpy.ops.mesh.primitive_cube_add()
cube = bpy.context.active_object
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
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
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)
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)