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")
?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With