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:
1 2 3 |
active_object = bpy.context.object # bpy.data.objects['Cube'] |
To set the initial animation key, call the keyframe_insert() function.
1 |
active_object.keyframe_insert(data_path='scale', frame=1) |
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.
1 |
active_object.scale.x *=3 |
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.
1 |
active_object.keyframe_insert(data_path='scale', frame=25) |
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.
1 2 |
active_object.scale.y *=3 active_object.keyframe_insert(data_path='scale', frame=50) |
And the same scale change from the 50-th to the 75-th frame along the Z axis.
1 2 |
active_object.scale.z *=3 active_object.keyframe_insert(data_path='scale', frame=75) |
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.
- ВКонтакте
- LiveJournal
- РћРТвЂВВВВВВВВнокласснРСвЂВВВВВВВВРєРСвЂВВВВВВВВ
- Telegram
- Viber
- Evernote
- Skype