Skip to content

Instantly share code, notes, and snippets.

@behreajj
Last active May 19, 2021 16:26
Show Gist options
  • Save behreajj/d066d8c36091547cb4d632a3780e7027 to your computer and use it in GitHub Desktop.
Save behreajj/d066d8c36091547cb4d632a3780e7027 to your computer and use it in GitHub Desktop.
Cube Scale Animation
# Use Vector.Fill to create uniform scales.
scales = [
Vector.Fill(3, 1.0),
Vector((0.25, 0.75, 0.5)),
Vector.Fill(3, 2 ** 0.5),
Vector((0.75, 0.5, 0.25)),
Vector.Fill(3, 2 ** -0.5),
Vector((0.5, 0.25, 0.75)),
Vector.Fill(3, 1.0)
]
# Create cube data.
bm = bmesh.new()
bmesh.ops.create_cube(bm, size=1.0)
cb_001_dat = bpy.data.meshes.new("Cube.001")
cb_002_dat = bpy.data.meshes.new("Cube.002")
cb_003_dat = bpy.data.meshes.new("Cube.003")
bm.to_mesh(cb_001_dat)
bm.to_mesh(cb_002_dat)
bm.to_mesh(cb_003_dat)
bm.free()
# Create materials.
mat_001 = bpy.data.materials.new(name="Material.001")
mat_001.diffuse_color = (1.0, 0.125, 0.125, 1.0)
cb_001_dat.materials.append(mat_001)
mat_002 = bpy.data.materials.new(name="Material.002")
mat_002.diffuse_color = (0.125, 1.0, 0.125, 1.0)
cb_002_dat.materials.append(mat_002)
mat_003 = bpy.data.materials.new(name="Material.003")
mat_003.diffuse_color = (0.125, 0.125, 1.0, 1.0)
cb_003_dat.materials.append(mat_003)
# Create cube objects.
objects = bpy.data.objects
cube_001 = objects.new(cb_001_dat.name, cb_001_dat)
cube_002 = objects.new(cb_002_dat.name, cb_002_dat)
cube_003 = objects.new(cb_003_dat.name, cb_003_dat)
# Offset cubes.
cube_002.location = (-2.25, 0.0, 0.0)
cube_003.location = (2.25, 0.0, 0.0)
# Link to the scene.
collection = bpy.context.collection
collection.objects.link(cube_001)
collection.objects.link(cube_002)
collection.objects.link(cube_003)
# Cache the scene start and end frames.
scene = bpy.context.scene
frame_start = scene.frame_start
frame_end = scene.frame_end
frame_range = range(frame_start, frame_end, 1)
# Set all frames to keyframes.
for frame in frame_range:
scene.frame_set(frame)
prc_01 = (frame - frame_start) / (frame_end - frame_start)
scale_001 = lerp_vec_array(scales, prc_01, smooth_step)
cube_001.scale = scale_001
cube_001.keyframe_insert(data_path="scale")
prc_02 = smoother_step(prc_01)
scale_002 = lerp_vec_array(scales, prc_02, linear_step)
cube_002.scale = scale_002
cube_002.keyframe_insert(data_path="scale")
prc_03 = 0.5 + 0.5 * math.sin((prc_01 - 0.5) * math.pi)
scale_003 = lerp_vec_array(scales, prc_03, smoother_step)
cube_003.scale = scale_003
cube_003.keyframe_insert(data_path="scale")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment