当前位置: 首页 > 工具软件 > Manim > 使用案例 >

Python Manim 库的使用

和季
2023-12-01

使用 Manim 库生成动画

  1. 前言
  2. 效果展示
  3. 源代码

1、前言

Hello World
  因为网上有很多资料可以参考,所以对 manim 的概念和环境搭建就不赘述了,考虑到没有 manim 环境的朋友,所以就在下面进行效果展示。

2、效果展示

动画
  点我

3、源代码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@Author: _Scale
@File name: vf.py
@Creative time: 2020/04/21  10:59
@Document description: 可以把该文件复制到用 Git 从 GitHub 上面拉下来的 manim 文件夹下,然后运行这个 vf.py 脚本
"""

from manimlib.imports import *


class VisualFeast(Scene):
    '''定义 VisualFeast 类,继承 manim 的 Scene'''
    def construct(self):
        '''构造 Visual Feast'''
        txt_list = ['P','Y','T','H','O','N']    # 需要的字符放在列表

        color_list = [BLUE,RED,PINK,WHITE,YELLOW,GREEN,BLACK]    # 需要的颜色放在列表

        # 初始化界面
        txt = TextMobject('I Live Python!')
        txt.set_color_by_gradient(RED_D, YELLOW, BLUE)    # 可设置颜色渐变
        txt.scale(3)
        txt_one = TextMobject('Python')
        txt_one.set_fill(BLACK, opacity=0.8)
        txt_one.scale(0.5)
        self.play(Write(txt), run_time=6)
        self.play(Transform(txt,txt_one))

        # 实例化对象
        txt_space = []
        txt_group = VGroup()

        # 展示一圈圈的字符
        for a in range(1,7):
            t = txt_list[a % 6]
            txt = TextMobject(t, color=color_list[1])    # 实例化字符
            txt.move_to(UP * 1)    # 向上移动一个单位
            txt.rotate(-PI / 3 * a, about_point=ORIGIN)    # PI 是常量
            self.play(Write(txt), run_time=0.1)    # run_time 设置运行时间
            txt_space.append(txt)
            txt_group = VGroup(txt_group, txt_space[a-1])
        for b in range(1,10):
            t = txt_list[b % 6]
            txt = TextMobject(t, color=color_list[3])
            txt.move_to(UP * 1.75)
            txt.rotate(-PI / 4.5 * b, about_point=ORIGIN)
            self.play(Write(txt), run_time=0.1)
            txt_space.append(txt)
            txt_group = VGroup(txt_group, txt_space[b-1])
        for c in range(1,13):
            t = txt_list[c % 6]
            txt = TextMobject(t, color=color_list[4])
            txt.move_to(UP * 2.5)
            txt.rotate(-PI / 6 * c, about_point=ORIGIN)
            self.play(Write(txt), run_time=0.1)
            txt_space.append(txt)
            txt_group = VGroup(txt_group, txt_space[c-1])
        for d in range(1,19):
            t = txt_list[d % 6]
            txt = TextMobject(t, color=color_list[0])
            txt.move_to(UP * 3.75)
            txt.rotate(-PI / 9 * d, about_point=ORIGIN)
            self.play(Write(txt), run_time=0.1)
            txt_space.append(txt)
            txt_group = VGroup(txt_group, txt_space[d-1])

        # 实例化对象
        square_one = Square(color=color_list[0])
        square_two = Square(color=color_list[4])
        circle_one = Circle(color=color_list[0])
        circle_two = Circle(color=color_list[4])
        annulus_one = Annulus(color=color_list[3])
        annulus_two = Annulus(color=color_list[0])
        annulus_three = Annulus(color=color_list[2])

        # 配置对象
        square_one.scale(4)
        circle_one.scale(4)
        square_two.scale(4)
        circle_two.scale(4)
        circle_one.flip(LEFT + RIGHT)
        circle_two.flip(UP)
        square_one.flip(DOWN + LEFT)
        square_two.flip(DOWN)

        # 生成
        self.play(ShowCreation(circle_one))
        self.play(Transform(circle_one,square_one))
        self.play(Transform(square_one,circle_two))
        self.play(Transform(square_one,circle_one))
        self.play(Transform(square_two,circle_two))

        # 改变初始字符颜色
        txt_one.set_fill(color_list[3], opacity=1)
        self.play(ShowCreation(txt_one, run_time=1))


        # 梅开二度
        for a in range(1,7):
            t = txt_list[a % 6]
            txt = TextMobject(t, color=color_list[4])    # 实例化字符
            txt.move_to(UP * 1)    # 向上移动一个单位
            txt.rotate(-PI / 3 * a, about_point=ORIGIN)    # PI 是常量
            self.play(Write(txt), run_time=0.05)    # run_time 设置运行时间
            txt_space.append(txt)
            txt_group = VGroup(txt_group, txt_space[a-1])
        for b in range(1,10):
            t = txt_list[b % 6]
            txt = TextMobject(t, color=color_list[0])
            txt.move_to(UP * 1.75)
            txt.rotate(-PI / 4.5 * b, about_point=ORIGIN)
            self.play(Write(txt), run_time=0.05)
            txt_space.append(txt)
            txt_group = VGroup(txt_group, txt_space[b-1])
        for c in range(1,13):
            t = txt_list[c % 6]
            txt = TextMobject(t, color=color_list[1])
            txt.move_to(UP * 2.5)
            txt.rotate(-PI / 6 * c, about_point=ORIGIN)
            self.play(Write(txt), run_time=0.05)
            txt_space.append(txt)
            txt_group = VGroup(txt_group, txt_space[c-1])
        for d in range(1,19):
            t = txt_list[d % 6]
            txt = TextMobject(t, color=color_list[3])
            txt.move_to(UP * 3.75)
            txt.rotate(-PI / 9 * d, about_point=ORIGIN)
            self.play(Write(txt), run_time=0.05)
            txt_space.append(txt)
            txt_group = VGroup(txt_group, txt_space[d-1])


        # 梅开三度
        square_one = Square(color=color_list[6])
        square_two = Square(color=color_list[6])
        circle_one = Circle(color=color_list[6])
        circle_two = Circle(color=color_list[6])

        # 配置对象
        square_one.scale(4)
        circle_one.scale(4)
        square_two.scale(4)
        circle_two.scale(4)

        # 生成
        self.play(ShowCreation(circle_one))
        self.play(Transform(circle_one,square_one))
        self.play(Transform(square_one,circle_two))
        self.play(Transform(circle_two,circle_one))
        self.play(Transform(circle_one,square_two))


        # 梅开四度
        for a in range(1,7):
            t = txt_list[a % 6]
            txt = TextMobject(t, color=color_list[6])    # 实例化字符
            txt.move_to(UP * 1)    # 向上移动一个单位
            txt.rotate(-PI / 3 * a, about_point=ORIGIN)    # PI 是常量
            self.play(Write(txt), run_time=0.01)    # run_time 设置运行时间
            txt_space.append(txt)
            txt_group = VGroup(txt_group, txt_space[a-1])
        for b in range(1,10):
            t = txt_list[b % 6]
            txt = TextMobject(t, color=color_list[6])
            txt.move_to(UP * 1.75)
            txt.rotate(-PI / 4.5 * b, about_point=ORIGIN)
            self.play(Write(txt), run_time=0.01)
            txt_space.append(txt)
            txt_group = VGroup(txt_group, txt_space[b-1])
        for c in range(1,13):
            t = txt_list[c % 6]
            txt = TextMobject(t, color=color_list[6])
            txt.move_to(UP * 2.5)
            txt.rotate(-PI / 6 * c, about_point=ORIGIN)
            self.play(Write(txt), run_time=0.01)
            txt_space.append(txt)
            txt_group = VGroup(txt_group, txt_space[c-1])
        for d in range(1,19):
            t = txt_list[d % 6]
            txt = TextMobject(t, color=color_list[6])
            txt.move_to(UP * 3.75)
            txt.rotate(-PI / 9 * d, about_point=ORIGIN)
            self.play(Write(txt), run_time=0.01)
            txt_space.append(txt)
            txt_group = VGroup(txt_group, txt_space[d-1])
        self.wait(3)


        # 改变初始字符颜色
        txt_one.set_fill(color_list[6], opacity=1)
        self.play(ShowCreation(txt_one, run_time=1))


        # 梅开五度
        txt_two = TextMobject('I Live Python!')
        txt_three = TextMobject('Python')
        txt_two.set_color_by_gradient(RED_D, YELLOW, BLUE)
        txt_three.set_fill(color_list[6], opacity=1)
        txt_two.scale(3)
        txt_three.scale(0.5)
        self.play(Write(txt_two, run_time=3))
        self.wait(3)
        self.play(Transform(txt_two, txt_three))


        # 结尾
        annulus_one.scale(1)
        annulus_two.scale(2)
        annulus_three.scale(4)
        annulus_one.set_fill(color_list[3])
        annulus_two.set_fill(color_list[0])
        annulus_three.set_fill(color_list[5])
        txt_three.set_fill(color_list[3], opacity=1)
        txt_three.scale(1)
        self.play(ShowCreation(annulus_one))
        self.play(ShowCreation(annulus_two))
        self.play(ShowCreation(annulus_three))
        self.play(ShowCreation(txt_three, run_time=0.1))
        self.wait(6)

# 脚本时执行,模块时不执行
if __name__ == '__main__':
    VisualFeast()


  感谢 CSDN,感谢朋友们的支持。

 类似资料: