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

2024-04-19 18:23:01 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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


Tags: run用户pyhttpscomiswww动画
1条回答
网友
1楼 · 发布于 2024-04-19 18:23:01

其实很简单。只需使用要渲染的场景对象的.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)

相关问题 更多 >