Animating Blender properties
Setting a keyframe
Animating an object in Blender comes down to setting some keyframes to the properties of that object. The function set_keyframe() is designed for this purpose.
In this example the location of a tetrahedron is animated:
import ddg
from ddg.visualization.blender.animation import set_keyframe
from ddg.datastructures.halfedge.surface_generator import tetrahedron
bobj = ddg.to_blender_object(tetrahedron())
fps = 24
set_keyframe(bobj, 0 * fps, "location", (0, 0, 0))
set_keyframe(bobj, 1 * fps, "location", (0, 0, 1))
set_keyframe(bobj, 2 * fps, "location", (0, 0, 0))
Additionaly, one can add animation to the rotation property of the tetrahedron in a similar way:
import numpy as np
set_keyframe(bobj, 0, "rotation_euler", (0, 0, 0))
set_keyframe(bobj, 2 * fps, "rotation_euler", (0, 0, 2 * np.pi))
Clearing the animation data
To clear the animation data that was set on the properties of an object, one can run:
from ddg.visualization.blender.animation import clear_animation_data
clear_animation_data(bobj)
Setting multiple keyframes at the same time
When the animation becomes more complex one might want to set the values of a property in multiple frames at the same time. Running this code block would have the same effect as the two first code blocks:
from ddg.visualization.blender.animation import animate_property
animate_property(
bobj, "location", [i * fps for i in range(3)], [(0, 0, i % 2) for i in range(3)]
)
animate_property(
bobj,
"rotation_euler",
[2 * i * fps for i in range(2)],
[(0, 0, 2 * np.pi * (i % 2)) for i in range(2)],
)
For even more complex animations, one can set multiple frames to multiple properties. This code block sets the animation for the location and the scaling at the same time:
from ddg.visualization.blender.animation import animate_properties
clear_animation_data(bobj)
animate_properties(
bobj,
[i * fps for i in range(3)],
{
"location": [(0, 0, i % 2) for i in range(3)],
"scale": [(1 + (i % 2), 1, 1) for i in range(3)],
},
)