Adding animation keys to an object in Blender using Python API

Animation keys can be set on scene objects either manually (by pressing the “i” key) or using the Blender Python API, for this purpose the developers have provided the keyframe_insert() function.

As a simple example of using this function, we will set animation keys to scale the current active object sequentially along the X, Y and Z axes.

Current active object:

To set the initial animation key, call the keyframe_insert() function.

In the “data_path” parameter, we pass the text name of the object parameter that we want to animate, in our case – scale.

In the “frame” parameter, we pass the number of the animation frame on which we want to fix this animation key – we start from 1.

The object scale values ​​are written to the animation key from its current values.

For example, we want to animate the scale along the X axis from 1 (the current value) to 3 over 25 frames.

First, we need to change the current value of the scale along the X axis, so that the next time we call the keyframe_insert() function, a new value is written to the animation key.

And now we can call the keyframe_insert() function again to create a new animation key on the 25-th frame with a new scale value.

Playing the animation, we will see that the scale of the object is changing from 1 to 3 from the first to the twenty-fifth frame.

Let’s add another animation key to change the scale of the object along the Y axis from the 25-th to the 50-th frame.

To do this, we need to pre-change the scale value along the desired axis again and call the keyframe_insert() function, creating a new keyframe.

And the same scale change from the 50-th to the 75-th frame along the Z axis.

Now if we play the animation, we will see how our object “spreads out” first along the X axis, then along the Y axis, and then along the Z axis by 3 times.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments