MANIM学习记录01
MAINM——使用mobject的坐标
Mobjects 包含一些定义它们边界的点。这些点可以独立地被用于添加其他的Mobject。例如:用
示例代码:
from manim import *
class MobjectExample(Scene):
def construct(self):
#创建了四个点
p1= [-7,-3.5166,0]
p2= [7,-3.5,0]
p3= [7,3.5,0]
p4= [-7,3.5,0]
#创建名为‘a’的mobject
a = Line(p1,p2).append_points(Line(p3,p4).get_points())
#获取‘a’上的几个特殊点
point_start= a.get_start()
point_end = a.get_end()
point_center = a.get_center()
#np.round (取约量,小数位数)
#np.tolist 写成列表形式
self.add(Text(f"a.get_start() = {np.round(point_start,3).tolist()}").scale(0.55).to_edge(UR).set_color(YELLOW))
self.add(Text(f"a.get_end() = {np.round(point_end,2).tolist()}").scale(0.5).next_to(self.mobjects[-1],DOWN).set_color(RED))
self.add(Text(f"a.get_center() = {np.round(point_center,2).tolist()}").scale(0.4).next_to(self.mobjects[-1],DOWN).set_color(BLUE))
#添加点到场景中 add(Dot())
self.add(Dot(a.get_start()).set_color(YELLOW).scale(1.5))
self.add(Dot(a.get_end()).set_color(RED).scale(1.5))
self.add(Dot(a.get_top()).set_color(GREEN_A).scale(2.5))
self.add(Dot(a.get_bottom()).set_color(GREEN_D).scale(2.5))
self.add(Dot(a.get_center()).set_color(BLUE).scale(2.5))
self.add(Dot(a.point_from_proportion(0.5)).set_color(ORANGE).scale(2))
self.add(Dot(a.point_from_proportion(0.5001)).set_color(ORANGE).scale(2))
self.add(*[Dot(x) for x in a.get_points()])
self.add(a)
from manim import *
class ExampleRotation(Scene):
def construct(self):
#切换相机背景
self.camera.background_color = WHITE
#创建图像
m1a = Square().set_color(RED).shift(LEFT)
m1b = Circle().set_color(RED).shift(LEFT)
m2a= Square().set_color(BLUE).shift(RIGHT)
m2b= Circle().set_color(BLUE).shift(RIGHT)
#逆时针旋转点的实现
#np.roll (元组,滚动长度,轴的维度)??
# axis=0 垂直滚动 axis=1 水平滚动
points = m2a.points
points = np.roll(points, int(len(points)/4), axis=0)#不明所以,反正很神奇就对了
m2a.points = points
self.play(Transform(m1a,m1b),Transform(m2a,m2b), run_time=1)
MANIM的配置
manim提供了许多配置系统。这使得manim可以适用于不同的使用情形。在场景渲染过程中,有许多配置选项可以配置。
通过The ManimConfig class(manim配置类)配置
配置manim最直接的方法是通过全局config对象,它是ManimConfig的一个实例。此类的每个属性都是一个配置选项,可以使用标准属性语法或类似dict的语法进行访问(??):
>>> from manim import *
>>> config.background_color = WHITE#更受欢迎
>>> config["background_color"] = WHITE#具有向后兼容性
#Most classes, including Camera, Mobject, and Animation, read some of their default configuration from the global config.
>>> Camera({}).background_color
<Color white>
>>> config.background_color = RED # 0xfc6255
>>> Camera({}).background_color
<Color #fc6255>
#ManimConfig 被设计成保持内部一致
#例如,修改frame_y_radius 将会影响到 frame_height
>>> config.frame_height
8.0
>>> config.frame_y_radius = 5.0
>>> config.frame_height
10.0
#下面的示例图解表明了在我们的文档中所提供的示例的视频分辨率,并带有参考框架
from manim import *
class ShowScreenResolution(Scene):
def construct(self):
pixel_height = config["pixel_height"] # 1080 is default
pixel_width = config["pixel_width"] # 1920 is default
frame_width = config["frame_width"]
frame_height = config["frame_height"]
self.add(Dot())
d1 = Line(frame_width * LEFT / 2, frame_width * RIGHT / 2).to_edge(DOWN)
self.add(d1)
self.add(Text(str(pixel_width)).next_to(d1, UP))
d2 = Line(frame_height * UP / 2, frame_height * DOWN / 2).to_edge(LEFT)
self.add(d2)
self.add(Text(str(pixel_height)).next_to(d2, RIGHT))
Command—line arguments (命令行参数)
通常,manim 是从命令行通过执行
manim <file.py> SceneName
这要求 manim 找到SceneName在文件 <file.py> 中调用的 Scene 类并渲染它。还可以使用标志-ql、-qm、-qh、 或 分别-qk为低、中、高和 4k 质量指定渲染质量。
manim -ql <file.py> SceneName
这些标志设置的配置选项的值config.pixel_width, config.pixel_height,config.frame_rate,和config.quality。
另一个常见的标志是-p(“preview”),它使 manim 在完成渲染后立即显示渲染的视频。
渲染高质量视频时,为了看见最终效果可以使用-s 来输出视频最后一帧。
-i 输出为gif
-o “名字” 输出设定的视频名称
-c 颜色 改变视频背景颜色
-n 只渲染视频的前十帧
通过配置文件配置
使用命令行操作时,往往同时会使用到多个命令行标志(-s -qh etc.)这就使得短时间内频繁的调整你写的脚本成为一件麻烦的事情,比如当你需要对你的脚本做一些增量的微调时。出于这个原因,manim可以使用配置文件进行配置。配置文件需要以 .cfg 后缀结尾。
文件夹内配置文件
为了使用配置文件,你需要创建一个与你写好的场景代码处于同一个目录下的配置文件,并且这个文件必须命名为 manim.cfg,因为manim现今只支持这个名字的配置文件,不然会配置无效。
配置文件需要以[CLI]开头。
示例:
[CLI]
# my config file
output_file = myscene
background_color = RED
用户范围配置文件
你也可以使用manim中的一个特殊的配置文件,这个文件将适用于你的所有项目中的所有场景,不管是不是一个文件夹的。
注意不要在这个配置文件的目录下储存编写的代码文件,这可能会导致manim不能正常工作,因为这样的行为没有被manim定义。
配置文件的优先级
文件夹内配置文件优先级高于用户范围配置文件。
这两个文件manim都会读取,但有两个文件有冲突时,以前者为准。
尽管如此,命令行标志符的优先级仍然是高于两者的。
另外,其实还有一个库函数范围的配置文件,这个的优先级是最低的。
这个配置文件,普通的使用者最好不要去修改,参与贡献的人也要得到manim开发团队的明确确认才能修改。
总结:
优先级从高到低
1.代码层面的配置
2.命令行标志符配置
3.文件夹内配置文件
4.用户范围配置文件
5.库函数配置文件
所有配置选项的列表
['aspect_ratio', 'assets_dir', 'background_color', 'background_opacity',
'bottom', 'custom_folders', 'disable_caching', 'dry_run',
'ffmpeg_loglevel', 'flush_cache', 'frame_height', 'frame_rate',
'frame_size', 'frame_width', 'frame_x_radius', 'frame_y_radius',
'from_animation_number', 'images_dir', 'input_file', 'left_side',
'log_dir', 'log_to_file', 'max_files_cached', 'media_dir', 'media_width',
'movie_file_extension', 'notify_outdated_version', 'output_file', 'partial_movie_dir',
'pixel_height', 'pixel_width', 'plugins', 'png_mode', 'preview',
'progress_bar', 'quality', 'right_side', 'save_as_gif', 'save_last_frame',
'save_pngs', 'scene_names', 'show_in_file_browser', 'sound', 'tex_dir',
'tex_template', 'tex_template_file', 'text_dir', 'top', 'transparent',
'upto_animation_number', 'use_opengl_renderer', 'use_webgl_renderer',
'verbosity', 'video_dir', 'webgl_renderer_path', 'write_all',
'write_to_movie']

浙公网安备 33010602011771号