Beginner Blender Python Tutorial: Rolling Cube (Part 1)

preview_player
Показать описание

This video tutorial for Blender Python beginners will go over a script that creates a rolling cube animation.

Final code:

Beginner Blender Python Playlist:

Beginner Blender Python Exercise: Repeating code with for loops

Beginner Blender Python Exercise: Introduction to If Statements (part 1)

00:00 - Intro
00:29 - Set up workspace
00:50 - Go over the steps
01:42 - Add a cube
03:36 - Add the empties and move them
07:02 - Parent the empties
15:27 - Animate the rotation
20:41 - Challenge #1
22:30 - Challenge #2
20:01 - Outro

Outro Music
Geographer - Easy Shake

#blender #python #scripting
Рекомендации по теме
Комментарии
Автор

thanks a lot for the tutorial! very helpfull

miname
Автор

Nice tutorial! I know it's not going to be perfect but still tried to mimic this with rotation matrix, and I encountered two problems: 1. angles over 180 are wrapped to negative; 2. somehow the first section of the animation doesn't quite work if the cube is not at (0, 0, 0). Here is my code:

import bpy
from math import radians, pow
from mathutils import Matrix, Vector
C = bpy.context

def keyframe_insert_LocRot(obj, frame):
"""Adding keyframe for location and rotation"""
obj.keyframe_insert("location", frame=frame)
obj.keyframe_insert("rotation_euler", frame=frame)

def pivot_rot(obj, pivot_point, angle, axis):
"""Rotate pivoting an arbitrary point"""
#make rotation translation matrix
trans_rot_mat = (
@
Matrix.Rotation(radians(angle), 4, axis) @

)
#rotate object
obj.matrix_world = trans_rot_mat @ obj.matrix_world

#make cube

base_cube = C.active_object
base_cube.location = (0, 0, 0)
#add keyframe
keyframe_insert_LocRot(base_cube, 1)
# #roll it
roll_count = 8
frame_duration = (C.scene.frame_end -
for i in range(roll_count):
if i%2 == 0:
pivot_rot(base_cube, base_cube.location + Vector((1, 0, -1)), 45, "Y")
else:
pivot_rot(base_cube, base_cube.location + Vector((0, 0, -pow(2, .5))), 45, "Y")
keyframe_insert_LocRot(base_cube, (i+1)*frame_duration)

wjyzxcv
Автор

why did you not make the emptys to children each other?

edit:

I get it now, although an object can have many children, if an object is already has a parent, impossible
But if the first empty object's parent is the last empty object, then it wouldn't have stayed in its location

...
empties = []
prevEmpty = None
for location in emptyLocations:
bpy.ops.object.empty_add()
empty = bpy.context.active_object
empty.location = location
if prevEmpty:
empty.parent = prevEmpty
empty.matrix_parent_inverse =
prevEmpty = empty
empties.append(empty)
print([empty.parent for empty in empties])
...

[None, bpy.data.objects['Empty'], bpy.data.objects['Empty.001'],


edit: looks like there's a limitation to assigning parents, you can't create a cycle

imeatingboiledeggrightnow