头文件引入

导入manim命名空间

from manim import *

manim基本结构

这是一个最基本的manim结构,格式:

from manim import *

class 类的名字(Scene):

    def construction(self):

类名叫做BaseFrame,传入一个场景Scene,并且包含一个construct()方法,传入self对象。self.wait()就是播放一段静默的动画(默认1秒)

class BaseFrame(Scene):
def construct(self):
self.wait()

例1-创建一个Circle对象并显示

类名:CreateCircle

涉及的新内容:Circle() set_fill() Create()

首先创建一个Circle()对象:circle

然后设置circle对象的颜色与透明度,

然后调用self.play()方法,传入animate类型的参数,即可渲染出一段动画。

class CreateCircle(Scene):
def construct(self):
circle = Circle() # 创建了一个Circle对象:circle
circle.set_fill(PINK, opacity=0.5) # set the color and transparency
self.play(Create(circle)) # show the circle on screen

编译

终端(控制台/PowerShell等)cd到.py文件所在目录,

命令格式manim -pql XXX.py ClassName

XXX.py 填入你编写的文件名

ClassName 填入类名

例如,你的文件名是sc1.py,你想渲染里面的CreateCircle类,

即:manim -pql sc1.py CreateCircle


例2-正方形变圆形

类名:SquareToCircle

涉及的新内容:Square() rotate() Transform() FadeOut()

创建一个圆、一个正方形,将正方形旋转四分之PI的角度。

此时circle和square都没有被创建出来。

调用self.play()将正方形展示出来,通过Transform方法将square对象的形状(包括位置)过渡到circle。

需要注意的是,此时的square对象仍是他自己,主体对象没有改变。circle对象相当于提供了一个模版,此时circle没有被生成出来!

最后通过FadeOut()动画方法淡出square对象(而不是circle,因为circle根本就没有在场景中,只是square变成了circle的样子罢了)

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 self.play(Create(square)) # animate the creation of the square
self.play(Transform(square, circle)) # interpolate the square into the circle
self.play(FadeOut(square)) # fade out animation


例3-同时生成两个图案

类名:SquareAndCircle

涉及的新内容:next_to()

前几行代码准备了一个圆形circle和一个正方形square,尚未放到场景中。

调用square.next_to(circle, RIGHT, buff=0.5),将square对象挪到circle对象的RIGHT side,间隔0.5个单位。

另外注意,manim的坐标是以屏幕为中心的笛卡尔直角坐标。

我们可以在self.play()方法中置入多组动画方法参数,同时执行动画。

本案例中的:self.play(Create(circle), Create(square))

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
self.play(Create(circle), Create(square)) # show the shapes on screen


例4-正方形变圆形播放动画

类名:AnimatedSquareToCircle

涉及的新内容:.animate.  ReplacementTransform()

class AnimatedSquareToCircle(Scene):
def construct(self):
circle = Circle() # create a circle
square = Square() # create a square #Play_Scripts_Start
self.play(Create(square)) # show the square on screen
self.play(square.animate.rotate(PI / 4)) # 旋转图形,参数是弧度制
self.play(
ReplacementTransform(square, circle)
) # transform the square into a circle
self.play(
circle.animate.set_fill(PINK, opacity=0.5)
) # color the circle on screen
#Play_Scripts_End

【manim】学习路径2-构建一些基础的图形,场景的更多相关文章

  1. Android学习路径(两)项目文件本身使用场景和文件演示

    ios讨论群1群:135718460  1.src文件:java源码存放文件夹 2.gen 文件:自己主动生成全部由android开发工具自己主动生成的文件,文件夹中最重要的就是R.java文件,这个 ...

  2. (大数据工程师学习路径)第一步 Linux 基础入门----正则表达式基础

    介绍 虽然我们这一节的标题是正则表达式,但实际这一节只是介绍grep,sed,awk这三个命令,而正则表达式作为这三个命令的一种使用方式(命令输出中可以包含正则表达式).正则表达式本身的内容很多,要把 ...

  3. Go学习路径--相关基础

    现在开始接触Go一段时间了,基本路径就是看基础学习材料,开始写项目,有问题找解决问题的方法.这里记录一下学习过程. go相关文章 Golang适合高并发场景的原因分析 go build 不同系统下的可 ...

  4. python爬虫 | 一条高效的学习路径

    数据是创造和决策的原材料,高质量的数据都价值不菲.而利用爬虫,我们可以获取大量的价值数据,经分析可以发挥巨大的价值,比如: 豆瓣.知乎:爬取优质答案,筛选出各话题下热门内容,探索用户的舆论导向. 淘宝 ...

  5. React Native小白入门学习路径——五

    React Native小白入门学习路径--五 序 "哦天呐!" 这句话成了我在实验室的口头禅, 老师可能觉得我们都是大神吧,都还在看着基础就给布置了那么多任务:写一个RN的TDD ...

  6. 亲爱的,我是一条Linux运维技术学习路径呀。

    根据我的经验,人在年轻时,最头疼的一件事就是决定自己这一生要做什么.在这方面,我倒没有什么具体的建议:干什么都可以,但最好不要写小说,这是和我抢饭碗.总而言之,干什么都是好的:但要干出个样子来,这才是 ...

  7. [ Linux运维学习 ] 路径及实战项目合集

    我们知道运维工程师(Operations)最基本的职责就是负责服务的稳定性并确保整个服务的高可用性,同时不断优化系统架构.提升部署效率.优化资源利用率,确保服务可以7*24H不间断地为用户提供服务. ...

  8. SSM(spring mvc+spring+mybatis)学习路径——1-1、spring入门篇

    目录 1-1 Spring入门篇 专题一.IOC 接口及面向接口编程 什么是IOC Spring的Bean配置 Bean的初始化 Spring的常用注入方式 专题二.Bean Bean配置项 Bean ...

  9. 学习 shell脚本之前的基础知识

    转载自:http://www.92csz.com/study/linux/12.htm  学习 shell脚本之前的基础知识 日常的linux系统管理工作中必不可少的就是shell脚本,如果不会写sh ...

随机推荐

  1. 接口开发-restful

    数据库表设计 1 --员工表 2 create table Employee 3 ( 4 id NUMBER primary key, 5 employeeID NUMBER not null, 6 ...

  2. php公立转农历

    <?php function nongli($riqi) { //优化修改 20160807 FXL $nian=date('Y',strtotime($riqi)); $yue=date('m ...

  3. R语言读取matlab中数据

    1. 在matlab中将数据保存到*.mat 文件夹 save("data.mat","data","label")#将data和label ...

  4. SQLite数据库损坏及其修复探究

    数据库如何发生损坏   SQLite 数据库具有很强的抗损坏能力.在执行事务时如果发生应用程序崩溃.操作系统崩溃甚至电源故障,那么在下次访问数据库文件时,会自动回滚部分写入的事务.恢复过程是全自动的, ...

  5. opencv-python保存视频

    import cv2 class WVideoManager: def __init__(self, write_path: str, width: int, height: int, FPS: in ...

  6. pyenv安装及使用教程

    pyenv安装及使用教程 pyenv 安装 git clone https://github.com/pyenv/pyenv.git ~/.pyenv # 编辑 bashrc vim ~/.bashr ...

  7. go: 如何编写一个正确的udp服务端

    udp的服务端有一个大坑,即如果收包不及时,在系统缓冲写满后,将大量丢包. 在网上通常的示例中,一般在for循环中执行操作逻辑.这在生产环境将是一个隐患.是的,俺就翻车了. go强大简易的并发能力可以 ...

  8. Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现

    前文,我们分析了Spring IOC的初始化过程和Bean的生命周期等,而Spring AOP也是基于IOC的Bean加载来实现的.本文主要介绍Spring AOP原理解析的切面实现过程(将切面类的所 ...

  9. # Vue3 toRef 和 toRefs 函数

    Vue3 toRef 和 toRefs 函数 上一篇博文介绍了 vue3 里面的 ref 函数和 reactive 函数,实现响应式数据,今天主要来说一下 toRef 函数和 toRefs 函数的基本 ...

  10. Calendar类介绍_获取对象的方式和Calendar类的常用成员方法

    java.util.Calendar类:日历类 Calendar类是一个抽象类,里边提供了很多操作日历字段的方法(YEAR.MONTH.DAY_OF_MONTH.HOUR ) Calendar类无法直 ...