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

标签 python manim

最初的问题发布在 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") ?

最佳答案

其实很简单。只需使用 .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)

关于python - 是否可以以编程方式(而不是从命令行)运行 manim?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66642657/

相关文章:

arrays - 有没有更好的方法来可视化数组?

manim - 如何同时为一个几何体应用多个动画

python - 连接到远程服务器时 MySQLdb 的安全性?

python - 不同的 ValueTracker 在 Manim 中返回相同的值

python - 将 manim TextMobjects 分组为一个可设置动画的对象?

python-3.x - 无法为 python 安装此 manim 库

python - 如何在特定值下对 kdeplot 进行着色

python - 有效地计算唯一元素的数量 - NumPy/Python

python - 使用 scapy 发送 TCP 握手,但在 wireshark 中序列号错误?

python - 从 Json 字符串创建 Python 对象