timeline

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

基类:PausePointsMixin, AudiosAndSubtitlesMixin, DebugMixin, TimelineCore

继承该类并实现 construct() 方法,以实现动画的构建逻辑

调用 build() 可以得到构建完成的 Timeline 对象


该类由若干基类实现功能,你可按照以下的分类跳转以参考对应的文档:


abstractmethod construct() → None

继承该方法以实现动画的构建逻辑

build(*, quiet=False, hide_subtitles=False, show_debug_notice=False) → BuiltTimeline

构建动画并返回

CONFIG: Config | None = None

在子类中定义该变量可以起到设置配置的作用,例如:

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

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

关于可使用的配置,另见 Config

config_context()

在 build() 方法中会发生第一次 with 调用, 将当前的 config_ctx_var 上下文,与自己及各个父类的 CONFIG , 合并为该 Timeline 对象的 self._config_context

如果是之后的 with 调用,则会让内部代码块恢复 self._config_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

调用该方法可以得到当前正在构建的 Timeline 对象

默认情况下,在 construct() 方法外调用时,会抛出 TimelineLookupError 错误; 可以使用 raise_exc=False 禁用,在同样的情况下会返回 None 而非抛出错误

参数:

raise_exc -- 在不可用时,是否抛出错误

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

基类:object

exception GuiCommandInterrupt(command: GuiCommand)

基类:Exception

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

基类:object

运行 Timeline.build() 后返回的实例

property cfg: Config | ConfigGetter

可以使用该属性获取 Timeline 构建时的上下文中的配置

可用于在简单场景中避免使用 config_context() ,例如:

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

提取特定帧的音频流

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

得到当前的 CameraInfo 信息

这里“当前”的含义是,上次调用 render_all() 的 global_t 时刻;或者也可以传入 as_time 指定

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

渲染所有可见物件

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

使用该方法可以在一个 Timeline 中插入另一个 Timeline

例如:

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)

这个例子中,在 Test 中插入了 Sub1 和 Sub2

额外参数:

  • delay: 延迟多少秒开始该 Timeline 的播放

  • first_frame_duration: 第一帧持续多少秒

  • keep_last_frame: 是否在 Timeline 结束后仍然保留最后一帧的显示

to_playback_control_item(**kwargs) → TimelinePlaybackControlItem

使用该方法可以在一个 Timeline 中插入另一个 Timeline

并且与 Video 类似,可以使用 start、stop 以及 seek 控制播放进度

例:

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)

警告

在默认情况下未开始播放,需要使用 start 以开始播放

额外参数:

  • keep_last_frame: 是否在 Timeline 结束后仍然保留最后一帧的显示

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

基类:Item

详见 BuiltTimeline.to_item()

class TIRenderer

基类:Renderer

render(item: TimelineItem)
renderer_cls

TIRenderer 的别名

start() → Self

从当前时刻开始播放子时间轴,在此时刻之前保持显示第一帧

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

基类:object

start(*, speed: float = 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)

基类:PlaybackControl, Item

详见 BuiltTimeline.to_playback_control_item()

class TPCIRenderer

基类:Renderer

render(item: TimelinePlaybackControlItem)
renderer_cls

TPCIRenderer 的别名