AnimationContext

Qualified name: algan.animation\_timeline.animation\_contexts.AnimationContext

class AnimationContext(runtime=None, runtime_per_part=None, equalize_runtimes=None, lag_ratio=None, priority_level=None, easing=None, composed_easing=None, combine_easing=False, record_funcs=None, record_attr_modifications=None, prev_context=None, spawn_at_end=None, new_animation=False, finished=False, new_mobs=None, child_contexts=None, kwargs=None, animation_manager=None)[source]

Bases: object

Base class for the with blocks that control animation timing.

A context decides when the animations recorded inside it happen: together, one after another, overlapping, or not at all. Use the subclasses rather than this class directly: Sync, Seq, Lag, Off, Audio, Speech.

Contexts nest, and a nested context inherits every parameter left as None from its parent, overriding only what it sets. On exit, a context with a runtime retroactively rescales all the timestamps recorded inside it, which is how you give a whole block a fixed runtime without timing its parts.

Parameters:
  • runtime (float | None) – Total runtime of this context, in seconds; the animations inside are rescaled to fit. Must be finite and zero or more. Defaults to None, meaning the runtime follows from the animations themselves.

  • runtime_per_part (float | None) – Runtime of each individual animation inside, in seconds. Must be finite and zero or more. Defaults to None, meaning inherit from the parent context (1.0 at the top level). runtime overrides this.

  • equalize_runtimes (bool | None) – Whether to stretch every animation inside to the runtime of the longest one. Defaults to None (inherited; effectively False).

  • lag_ratio (float | None) – Fraction of one animation’s runtime to wait before starting the next: 0 plays them together, 1 strictly in sequence, in between overlaps them. Defaults to None, meaning inherit from the parent.

  • priority_level (float | None) – Priority of this context. A context can only be overridden by one of equal or higher priority, which is what lets Off resist an enclosing runtime. Defaults to None, meaning inherit (0 at the top level).

  • easing (collections.abc.Callable[[], float] | None) – Easing function mapping progress in [0, 1] to adjusted progress, e.g. easings.identity. Replaces the parent’s. Defaults to None, meaning inherit (easings.smooth at the top level).

  • composed_easing (collections.abc.Callable[[], float] | None) – Easing function to compose with the parent’s rather than replace it. Defaults to None. See ComposedEasing.

  • combine_easing (bool) – Whether component rate functions are combined into the context’s own. Defaults to False.

  • record_funcs (bool | None) – Whether animated functions inside are recorded on the Scene timeline. Defaults to None, meaning inherit (True at the top level; Off sets it False).

  • record_attr_modifications (bool | None) – Whether attribute changes inside are recorded on the Scene timeline. Defaults to None, meaning inherit (True at the top level).

  • prev_context (algan.animation_timeline.animation_contexts.AnimationContext | None) – The context this one was created inside. Defaults to None; it is filled in on entry.

  • spawn_at_end (bool | None) – Whether Mobs created inside are held back and spawned together when the block ends. Defaults to None, meaning inherit (False at the top level).

  • animation_manager (Any) – The AnimationManager to record against. Defaults to None, meaning the active Scene’s – pass one explicitly when authoring a Scene that is not currently active.

  • new_animation (bool | None)

  • finished (bool)

  • new_mobs (list | None)

  • child_contexts (list | None)

  • kwargs (Any)

Raises:
  • .AlganConfigurationError – If runtime or runtime_per_part is not finite and non-negative, or a parameter is spelled the way Manim or an older Algan spelled it (duration, run_time, rate_func).

  • .ContextReuseError – If the same context object is entered by a second with block.

Methods

add_child_context

Register a nested context, so this one's rescaling reaches it.

add_exit_callback

Register work to run once this block has finished, at its end time.

add_mob

Record that a Mob was created in this context.

get_current_end_time

Get the time an animation started now would finish, in seconds.

get_current_time

Get the current authoring time, in seconds, after any rescaling.

get_descendants

Get this context and every context nested inside it, flattened.

get_end_time

Get the time this context ends, in seconds, after any rescaling.

get_timespan

Get this context's timespan object.

increment_times

Advance the cursor after recording one animation.

on_create

Run spawn hooks for this context and every enclosing one.

on_create_extra

Hook for behaviour to add whenever a Mob spawns in this context.

on_destroy

Run despawn hooks for this context and every enclosing one.

on_destroy_extra

Hook for behaviour to add whenever a Mob despawns in this context.

on_init

Run construction hooks for this context and every enclosing one.

on_init_extra

Hook for behaviour to add whenever a Mob is constructed in this context.

rewind

Move the authoring cursor backwards, so what follows is recorded earlier.

wait

Hold still, leaving a pause before the next animation.

Attributes

animation_manager

child_contexts

combine_easing

composed_easing

current_time

Where the authoring cursor sits within this context, in seconds.

easing

end_time

The furthest time anything recorded in this context reaches, in seconds.

equalize_runtimes

finished

kwargs

lag_ratio

new_animation

new_mobs

prev_context

priority_level

record_attr_modifications

record_funcs

runtime

runtime_per_part

spawn_at_end

add_child_context(c)[source]

Register a nested context, so this one’s rescaling reaches it.

Called when a context is entered inside this one.

Parameters:

c – The child AnimationContext.

add_exit_callback(callback)[source]

Register work to run once this block has finished, at its end time.

The callback runs after everything inside the block has been recorded and rescaled, with the authoring cursor placed at this context’s end, so anything the callback records lands after the whole block. Callbacks run in reverse registration order, so nested set-up unwinds the way it was applied.

This exists for changes that have to outlive the statement that asked for them: wave_color() refines a Mob’s sampling so its color wave renders smoothly, and can only drop that resolution again once nothing further will be recorded alongside the wave.

Parameters:

callback – Zero-argument callable.

add_mob(mob)[source]

Record that a Mob was created in this context.

Bubbles up to enclosing contexts too, which is how SlideShow knows what to clear at the end.

Parameters:

mob – The Mob that was created.

Returns:

This context, so calls can be chained.

Return type:

AnimationContext

property current_time

Where the authoring cursor sits within this context, in seconds.

The time the next animation recorded in this context will start at.

property end_time

The furthest time anything recorded in this context reaches, in seconds.

get_current_end_time()[source]

Get the time an animation started now would finish, in seconds.

Returns:

The current time plus one animation’s runtime.

Return type:

float

get_current_time()[source]

Get the current authoring time, in seconds, after any rescaling.

Returns:

The time the next recorded animation starts at.

Return type:

float

get_descendants(include_self=True)[source]

Get this context and every context nested inside it, flattened.

Parameters:

include_self (bool) – Whether this context is the first element. Defaults to True.

Returns:

This context (unless excluded) followed by all nested contexts.

Return type:

list[AnimationContext]

get_end_time()[source]

Get the time this context ends, in seconds, after any rescaling.

Returns:

End time on the Scene timeline.

Return type:

float

get_timespan()[source]

Get this context’s timespan object.

Returns:

The span holding this context’s start, cursor and end times.

Return type:

TimelineSpan

increment_times()[source]

Advance the cursor after recording one animation.

How much it moves is what distinguishes the contexts: lag_ratio of one animation’s runtime, so Sync (0) leaves the cursor put and Seq (1) moves it past the whole animation. Called by the animated_function machinery; you do not call it yourself.

on_create(animatable)[source]

Run spawn hooks for this context and every enclosing one.

Parameters:

animatable – The Mob being spawned.

on_create_extra(animatable)[source]

Hook for behaviour to add whenever a Mob spawns in this context.

Does nothing by default. SlideShow overrides it to pause after each spawn; override it in your own context subclass for similar effects.

Parameters:

animatable – The Mob being spawned.

Returns:

This context.

Return type:

AnimationContext

on_destroy(animatable)[source]

Run despawn hooks for this context and every enclosing one.

Parameters:

animatable – The Mob being despawned.

on_destroy_extra(animatable)[source]

Hook for behaviour to add whenever a Mob despawns in this context.

Does nothing by default.

Parameters:

animatable – The Mob being despawned.

Returns:

This context.

Return type:

AnimationContext

on_init(animatable)[source]

Run construction hooks for this context and every enclosing one.

Parameters:

animatable – The Mob being constructed.

on_init_extra(animatable)[source]

Hook for behaviour to add whenever a Mob is constructed in this context.

Does nothing by default; OnInit overrides it to run a function of your choosing.

Parameters:

animatable – The Mob being constructed.

Returns:

This context.

Return type:

AnimationContext

rewind(num_frames)[source]

Move the authoring cursor backwards, so what follows is recorded earlier.

Animation

Not animated: this moves the cursor, not any Mob. Animation recorded after this call overlaps what was recorded before it.

Parameters:

num_frames (float) – How far back to move the cursor, in the context’s own time units.

wait(t=None)[source]

Hold still, leaving a pause before the next animation.

Animation

Recorded on the timeline: it consumes video time and nothing else. As with any animation in this context, the cursor advances by lag_ratio of the wait, so a wait inside a Sync extends the block without pushing later animations back.

Parameters:

t (float | None) – How long to wait, in seconds. Must be zero or more. Defaults to None, meaning one animation’s runtime (runtime_per_part, 1 second by default).

Raises:

.AlganConfigurationError – If t is not a finite, non-negative number.