10

The original question was posted on Reddit (https://www.reddit.com/r/manim/comments/lw3xs7/is_it_possible_to_run_manim_programmatically_and/). I took the liberty to ask it here.

Say I want to code a simple GUI application, where a user can enter LaTeX code, click on an "animate" button, then manim renders an mp4 in the background, and finally, the animation is presented to the user.

Is it possible to use manim as a module to achieve this? For example by inserting the user input in a previously prepared manim scene, then calling something like my_scene.run(output=tempfile.mp4)? Or would I have to take the user input, write it into a new scene.py file and run os.subprocess("manim", "scene.py", "MyScene", "-p")?

Jared Smith
  • 19,721
  • 5
  • 45
  • 83
Adam Ryczkowski
  • 7,592
  • 13
  • 42
  • 68
  • 1
    The "manim" command is just a Python script, so clearly the answer is yes. You can poke through the source to find their `__main__.py` file, and just do what they do. MAYBE there might be problems where it assumes a fresh start each time, but these are bright people, so I suspect it can handle it. – Tim Roberts Mar 15 '21 at 17:36

1 Answers1

11

It is actually very simple. Just use the .render(self) method of the Scene object you want to render.

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)

Adam Ryczkowski
  • 7,592
  • 13
  • 42
  • 68