Your first animation¶
Animate the rectangle from your first render. It moves from the left to the right over 1.5 seconds, then moves back toward the left over the remaining 1.5 seconds.
The steps below replace the rectangle's static position with keyframes. Keep the same project, background and rectangle, or use the complete script.
1. Move the rectangle to the right¶
Add Interpolation to the first import:
Replace card.transform.position = (0.25, 0.5) with these two keyframes:
card.transform.position.keyframe(0, (0.25, 0.5))
card.transform.position.keyframe(
1.5, (0.75, 0.5), interpolation=Interpolation.EASE_IN_OUT
)
At zero seconds the rectangle is 25% across the canvas. At 1.5 seconds it is
75% across. Its y coordinate stays at 0.5, so it moves horizontally. Vestra
calculates the position between the two keys.
EASE_IN_OUT makes the rectangle accelerate at the beginning and slow down at
the end. Interpolation belongs to the destination keyframe: the setting on
the key at 1.5 seconds controls the interval from 0 to 1.5 seconds. Without a
third keyframe, the rectangle would stay on the right for the rest of the video.
2. Add the return movement¶
Add a third keyframe after the first two:
This defines a return to x = 0.25 at second 3. The time is measured from the
start of the layer, so the return interval lasts 1.5 seconds, not three.
Its destination also uses ease in/out interpolation.
The end timestamp is not a rendered frame
At 30 fps, a three-second video contains 90 frames at times 0, 1/30,
…, 89/30 seconds. The last frame is at about 2.967 seconds, just
before the third keyframe. The rectangle is almost back at its initial
position; the final frame is not an exact duplicate of the first.
The complete script¶
Save this as first_animation.py in your Python project, or
download the script.
It creates its own project from scratch.
"""The README quickstart; no external images, fonts or audio required."""
from vestra import Interpolation, Project
from vestra.sources import Color, Rectangle
project = Project(
size=(640, 360),
fps=30,
duration=3,
)
project.root.add(Color("#101c24"))
card = project.root.add(
Rectangle(width=180, height=140, corner_radius=16, fill="#b8e3bc")
)
# Times are seconds since the card starts; the destination sets the easing.
card.transform.position.keyframe(0, (0.25, 0.5))
card.transform.position.keyframe(
1.5, (0.75, 0.5), interpolation=Interpolation.EASE_IN_OUT
)
card.transform.position.keyframe(
3, (0.25, 0.5), interpolation=Interpolation.EASE_IN_OUT
)
if __name__ == "__main__":
project.render("quickstart.mp4", backend="cpu", overwrite=True)
The script replaces quickstart.mp4 on a rerun because it uses
overwrite=True. Change the output filename to keep an earlier render.
Run it in your project environment:
Open quickstart.mp4 in the current directory. The render is 640 × 360 at
30 fps, lasts three seconds and has no audio.
Change the timing¶
Move the middle keyframe from 1.5 to 1. The rectangle now takes one second
to move right and two seconds to return. The overall duration remains three
seconds. Keep keyframe times in increasing order when adjusting them.
Continue to animation and keyframes
The examples gallery includes larger projects with effects, compositing and audio-reactive motion.