是否可以通过编程方式(而不是通过命令行)运行manim



最初的问题发布在Reddit上(https://www.reddit.com/r/manim/comments/lw3xs7/is_it_possible_to_run_manim_programmatically_and/)。我在这里冒昧地问了一下。

假设我想编写一个简单的GUI应用程序,用户可以在其中输入LaTeX代码,点击";动画化";按钮,然后manim在背景中渲染一个mp4,最后,动画呈现给用户。

是否可以将人体模型作为一个模块来实现这一点?例如,在预先准备好的人体模型场景中插入用户输入,然后调用my_scene.run(output=tempfile.mp4)?或者我必须接受用户输入,将其写入一个新的scene.py文件并运行os.subprocess("manim", "scene.py", "MyScene", "-p")

它实际上非常简单。只需使用要渲染的"场景"对象的.render(self)方法即可。

from manim import *
# To open the movie after render.
from manim.utils.file_ops import open_file as open_media_file 

class DemoScene(Scene):
def construct(self, alg):
image1 = ImageMobject(np.uint8([[63, 0, 0, 0],
[0, 127, 0, 0],
[0, 0, 191, 0],
[0, 0, 0, 255]
]))
image1.height = 7
self.add(image1)
self.wait(1)

if __name__ == '__main__':
scene = DemoScene()
scene.render() # That's it!

# Here is the extra step if you want to also open 
# the movie file in the default video player 
# (there is a little different syntax to open an image)
open_media_file(scene.renderer.file_writer.movie_file_path)

最新更新