timeline

class janim.timeline.timeline.Timeline(*args, **kwargs)

Bases: PausePointsMixin, AudiosAndSubtitlesMixin, DebugMixin, TimelineCore

Inherit this class and implement the construct() method to implement the logic of building animations

Call build() to get the constructed Timeline object


This class implements its functionality through several base classes. You can refer to the following categories to find the corresponding documentation:


abstractmethod construct() None

Implement this method to build the animation logic

build_indent_ctx: ContextVar[int] = <ContextVar name='Timeline.build_indent_ctx'>
build(*, quiet=False, hide_subtitles=False, show_debug_notice=False) BuiltTimeline

Build the animation and return it

CONFIG: Config | None = None

Defining this variable in a subclass can serve to set configuration, for example:

class Example(Timeline):
    CONFIG = Config(
        font=['Consolas', 'LXGW WenKai Lite']
    )

    def construct(self) -> None:
        ...

For available configurations, see also Config

config_context()

The first with call occurs in the build() method, where the current config_ctx_var context is merged with the CONFIG of itself and each of its parent classes to form this Timeline object’s self._config_context

For subsequent with calls, the inner code block will restore the state of self._config_context to ensure consistency within the relevant context

ctx_var: ContextVar[Timeline | None] = <ContextVar name='Timeline.ctx_var'>
static get_context(raise_exc: Literal[True] = True) Timeline
static get_context(raise_exc: Literal[False]) Timeline | None

Call this method to get the current Timeline object being constructing

By default, calling this method outside the construct() method will raise a TimelineLookupError error. This can be disabled by setting raise_exc=False; in the same situation, it will return None instead of raising an error

Parameters:

raise_exc – Whether to raise an error when unavailable

class GuiCommand(global_t: float, text: str, frame: FrameType)

Bases: object

exception GuiCommandInterrupt(command: GuiCommand)

Bases: Exception

class janim.timeline.timeline.BuiltTimeline(timeline: Timeline)

Bases: object

Instance returned after running Timeline.build()

property cfg: Config | ConfigGetter

This property can be used to obtain the configuration from the context in which the Timeline was built

It can be used to avoid using config_context() in simple scenarios, for example:

built.cfg.preview_fps
property frame_count: int
get_audio_samples_of_frame(fps: int, framerate: int, frame: int, *, count: int = 1) ndarray

Extract the audio stream of a specific frame

get_audio_samples_between(framerate: int, begin: float, end: float) ndarray
current_camera_info(*, as_time: float | None = None) CameraInfo

Get the current CameraInfo information

Here, “current” means the global_t moment from the last call to render_all(); you can also specify it by passing as_time

render_all(ctx: Context, global_t: float, *, camera: Camera | None = None) bool

Render all visible items

capture(global_t: float, *, transparent: bool = True, ctx: Context | None = None) Image
to_item(**kwargs) TimelineItem

Use this method to insert another Timeline into a Timeline

For example:

class Sub1(Timeline):
    def construct(self):
        text = Text('text from Sub1')
        text.points.shift(UP)
        self.play(
            Rotate(text, TAU, about_point=LEFT * 2),
            duration=4
        )


class Sub2(Timeline):
    def construct(self):
        text = Text('text from Sub2')
        text.points.shift(DOWN)
        self.play(
            Rotate(text, TAU, about_point=RIGHT * 2),
            duration=4
        )


class Test(Timeline):
    def construct(self):
        tl1 = Sub1().build().to_item().show()
        tl2 = Sub2().build().to_item().show()
        self.forward_to(tl2.end)

In this example, Sub1 and Sub2 are inserted into Test

Additional parameters:

  • delay: How many seconds to delay before starting playback of this Timeline

  • first_frame_duration: How many seconds the first frame lasts

  • keep_last_frame: Whether to keep displaying the last frame after the Timeline ends

to_playback_control_item(**kwargs) TimelinePlaybackControlItem

Use this method to insert another Timeline into a Timeline

And similar to Video, you can use start, stop, and seek to control playback progress

Example:

class Sub(Timeline):
    def construct(self):
        self.play(
            ItemUpdater(
                None,
                lambda p: Text(f'{p.global_t:.2f}')
            ),
            duration=8
        )


class Test(Timeline):
    def construct(self):
        sub = Sub().build().to_playback_control_item().show()
        sub.start()
        self.forward(2)
        sub.start(speed=0.1)
        self.forward(2)
        sub.seek(0).start(speed=4)
        self.forward(2)

Warning

By default, playback is not started; you need to use start to begin playback

Additional parameters:

  • keep_last_frame: Whether to keep displaying the last frame after the Timeline ends

class janim.timeline.timeline.TimelineItem(built: BuiltTimeline, *, delay: float = 0, first_frame_duration: float = 0, keep_last_frame: bool = False, **kwargs)

Bases: Item

See BuiltTimeline.to_item() for details

class TIRenderer

Bases: Renderer

render(item: TimelineItem)
renderer_cls

alias of TIRenderer

start() Self

Start playing the sub-timeline from the current moment, keeping the first frame displayed before this moment

property end: float
class janim.timeline.timeline.PlaybackControl(*args, loop: bool = False, **kwargs)

Bases: object

start(*, speed: int = 1) Self
stop() Self
seek(t: float) Self
compute_time(t: float, total: float | None = None) float
class janim.timeline.timeline.TimelinePlaybackControlItem(built: BuiltTimeline, *, keep_last_frame: bool = False, **kwargs)

Bases: PlaybackControl, Item

See BuiltTimeline.to_playback_control_item() for details

class TPCIRenderer

Bases: Renderer

render(item: TimelinePlaybackControlItem)
renderer_cls

alias of TPCIRenderer