【Manim】1.快速入门教程

from manim import *
# 这是使用 Manim 的推荐方式,因为单个脚本通常使用 Manim 命名空间中的多个名称。在这串代码中使用了 Scene、Circle、PINK和Create

# 建立圆的动画
class CreateCircle(Scene):
    def construct(self):
# 大多数时候,编写动画脚本的代码完全包含在类construct()的方法中Scene。在里面construct(),您可以创建对象,将它们显示在屏幕上,并为它们设置动画。
        circle = Circle()  # create a circle
        circle.set_fill(PINK, opacity=0.5)  # set the color and transparency
        # 这两行创建一个圆圈并设置其颜色和不透明度
        self.play(Create(circle))  # show the circle on screen
        # 使用动画Create在屏幕上显示圆圈

# 所有动画必须驻留在construct()的类的方法Scene中。其他代码(例如辅助函数或数学函数)可能驻留在类之外。

# manim -pql scene.py CreateCircle 这是运行的代码 其中 CreateCircle表示你要预览的类,所以这个代码只运行这个demo文件里面的对应的这个类的代码
# 标志-p 告诉 manim 在渲染场景后播放场景,而-ql 标志告诉 manim 以低质量渲染场景
# 视频以 480 分辨率、每秒 15 帧的速度生成的。因此,输出可以在里面找到 media/videos/scene/480p15
# 若变成:manim -pqh scene.py SquareToCircle
# 标志 -ql(低质量)已被-qh 标志取代,高品质。Manim 将花费相当长的时间来渲染此文件,并且一旦完成就会播放它。这时候Manim 新建了一个文件夹media/videos/1080p60,对应高分辨率和每秒 60 帧。


# 正方形转圆动画:Transform
class SquareToCircle(Scene):
    def construct(self):
        circle = Circle()  # create a circle
        circle.set_fill(PINK, opacity=0.5)  # set color and transparency

        square = Square()  # create a square
        square.rotate(PI / 4)  # rotate a certain amount # 旋转90度

        self.play(Create(square))  # animate the creation of the square # 创建一个正方形动画
        self.play(Transform(square, circle))  # interpolate the square into the circle # 利用Tranform命令将正方形变圆
        self.play(FadeOut(square))  # fade out animation # 淡出

# 设置多个物体动画:next_to
class SquareAndCircle(Scene):
    def construct(self):
        circle = Circle()  # create a circle
        circle.set_fill(PINK, opacity=0.5)  # set the color and transparency

        square = Square()  # create a square
        square.set_fill(BLUE, opacity=0.5)  # set the color and transparency

        square.next_to(circle, RIGHT, buff=0.5)  # set the position
        # 我们首先通过传递该方法的第一个参数来指定粉红色圆圈作为正方形的参考点circle。第二个参数用于指定Mobject相对于参考点的放置方向。
        # 在本例中,我们将方向设置为RIGHT,告诉 Manim 将正方形放置在圆的右侧。最后,buff=0.5在两个对象之间应用一个小距离缓冲区。
        # 尝试改为RIGHT、LEFT、UP或DOWN,看看它如何改变正方形的位置。
        # 使用定位方法,您可以渲染具有多个场景Mobject,使用坐标设置它们在场景中的位置或相对于彼此定位它们
        self.play(Create(circle), Create(square))  # show the shapes on screen
        self.wait()

# 使用.animate方法
    def construct(self):
        circle = Circle()  # create a circle
        square = Square()  # create a square

        self.play(Create(square))  # show the square on screen
        self.play(square.animate.rotate(PI / 4))  # rotate the square
        # 当使用了.animate方法,则可以在动画过程中来对物体属性进行修改,而不需要在外面修改
        self.play(ReplacementTransform(square, circle))  # transform the square into a circle
        # ReplacementTransform和Transform是一样的
        self.play(circle.animate.set_fill(PINK, opacity=0.5))  # color the circle on screen
    # 尽管最终结果与SquareToCircle结果相同,但是.animate显示rotate并动态set_fill应用于Mobject,而不是使用已应用的更改来创建它们

# 使用shift方法
class DifferentRotations(Scene):
    def construct(self):
        left_square = Square(color=BLUE, fill_opacity=0.7).shift(2 * LEFT)
        # fill_opacity表示的是内部正方形的填充
        right_square = Square(color=GREEN, fill_opacity=0.7).shift(2 * RIGHT)
        # .shift是可以用于定义其位置的
        self.play(left_square.animate.rotate(PI), Rotate(right_square, angle=PI), run_time=5)
        # run_time表示的是视频的时间
        self.wait()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Suhence

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值