Manim动画相关。

1. 展示创建过程

展示图形的创建过程,使用Create

# Incrementally show a VMobject
class Create(mobject=None, *args, use_override=True, **kwargs)

self.play(Create(Square()))

还可以展示手写和擦除的过程,使用WriteUnwrite,两个都支持参数reverse=True/False,是否反向写或擦除。

# Simulate hand-writing a Text or hand-drawing a VMobject.
class Write(mobject=None, *args, use_override=True, **kwargs)
class Unwrite(mobject=None, *args, use_override=True, **kwargs)

self.play(Write(Text("Hello", font_size=144), reverse=True))

再用manim.scene.scene.Scene.play播放设置的动画。

# Plays an animation in this scene.
play(*args, subcaption=None, subcaption_duration=None, subcaption_offset=0, **kwargs)

1.2 移动对象

Mobject指Mathematical Object,是所有可显示在屏幕上基类。

Mathematical Object: base class for objects that can be displayed on screen.

Moject包含很多方法,可以来移动对象。

# Shift by the given vectors (numpy.ndarray).
shift(*vectors)        # e.g., shift(UP)

to_corner(...)        # UP, DOWN, LEFT, RIGHT
to_edge(...)        # UL, UR, DL, DR

# Move center of the Mobject to certain coordinate.
move_to(point_or_mobject, aligned_edge=array([0., 0., 0.]), coor_mask=array([1, 1, 1]))

1.3 组合若干对象

使用VGroup将几个对象组合在一起,

# A group of vectorized mobjects.
class VGroup(*vmobjects, **kwargs)

vgroup = VGroup(f1, f2, f3, f4).arrange(6 * RIGHT)

2. 播放对象

2.1 play

调用场景的play函数,播放设置的动画。

# Plays an animation in this scene.
play(*args, subcaption=None, subcaption_duration=None, subcaption_offset=0, **kwargs)

# Example
self.play(Write(text_solar_term.next_to(text_notes, DOWN)))

2.2 Mobject装饰器animate

Manim将Mobject类中的animate方法通过Python内置的@property装饰器,将其变成属性调用,Mobject property animate

Used to animate the application of any method of self. Any method called on animate is converted to an animation of applying that method on the mobject itself.

举例:

  • square.set_fill(WHITE) sets the fill color of a square, while
  • sqaure.animate.set_fill(WHITE) animates this action.
self.play(my_mobject.animate.shift(RIGHT).rotate(PI))
self.play(my_mobject.animate.shift(RIGHT), my_mobject.animate.rotate(PI))

self.play(
    mobject1.animate(run_time=2).rotate(PI),
    mobject2.animate(rate_func=there_and_back).shift(RIGHT),
)

2.3 暂停动画

如让动画停上一小段时间,等待音乐播放完。使用场景Scene中的wait函数,举例

self.wait(0.2)    # 动画停留0.2秒

还可以使用Scene.wait_until

wait_until(stop_condition, max_time=60)

参考资料:

[1] 使用@property - 廖雪峰的官方网站

[2] Reference Manual - Manim Community

本文系Spark & Shine原创,转载需注明出处本文最近一次修改时间 2022-02-27 09:55

results matching ""

    No results matching ""