본문 바로가기

Programming/Manim Lectures

[03-3] Explain about "Hello Manim" code

반응형

In this page, I will explain about 'Hello Manim' code which is used in the previous page.

 

from manimlib.imports import *

class HelloManim(Scene):
    def construct(self):
        text=TextMobject("Hello Manim")
        self.play(Write(text))
        self.wait()

from manimlib.imports import *

All of the packages used by Manim are declared in the manimlib/imports.py file.

So, if you insert the phrase 'from manimlib.imports import' in the code, you can use all the modules and classes used in Madam.

 

A part of manimlib/imports.py file

It is a very convenient coding tip to import all the necessary modules in the imports.py file and import this file only once in the code.

class HelloManim(Scene):
    def construct(self):

To create a movie by Manim, you have to create a class that inherits a Scene or Scene's subclass.

The construct acts like the main function of the movie execution. In other words, this construct method will be called automatically when the Scene class runs.


        text=TextMobject("Hello Manim")
        self.play(Write(text))
        self.wait()

We can define about Manim's coding structure as 'Create an object and display it with some animation effects in the coordinates of the screen'.

The above code creates a TextMobject object that can contain a LaTex text, and run 'Write' animation with the text on the screen.

 

Use 'play' method when running animation on an object. The play method is defined in the Scene class and the HelloManim class inherits Scene class it's possible to call play method as self.play(Write(text))

 

Play method can be called with 'run_time' argument to specify the playing time duration but if it is called without 'run_time' argument then the default running time will be 1 second.

 

The last sentence of 'self.wait()' is waiting function to keep the screen frozen during one second. Therefore, the written text of 'Hello Manim' will be remained for 1 second.

 

The total running time is 2 second. One second is for writing the text, the remain one second is waiting time.

 

When you make a video like this, you can estimate exactly how many seconds the video will be produced by the program, and it is possible for the developer to increase or decrease the time by simply adjusting the value of run_time.
For example, in the example above, if you want to increase the time that Hello Manim text is written on the screen to 5 seconds, you only need to change the value of run_time like self.play(Write(text), run_time = 5).

 


How to run the code

We used below command to create video from the hello.py file.

 

  C:/now/manim/python -m manim src/hello.py HelloManim -pl

 

-m : this option is used to run a code as a module not as script by the Python interpreter. 
     Therefore, in here, the manim.py file will be run as a module
     
manim: means manim.py. When the '-m' option is used, the file's extension is not used.

-pl: Preview and low quality resolution.
     Low quality means that the generated video resolution will be 480 lines per frame and 
     15 frames per one second
     
     Preview means that automatically play the video after createion.
     

 

'-pm' option makes the video as the medium quality resolution (780p x 30 frames)

You can find description for all arguments in here.

 

The video file will be created in the 'C:/now/manim/media/videos/hello/40p15' directory.

  


Next: [03-4] Watch samples (PyCham)

 

[03-4] Watch samples (PyCham)

There is an example file in the downloaded manim package. C:/now/manim/example_scenes.py Here is sample code to create multiple videos. In this page, I will show you how to run this example_scenes.p..

infograph.tistory.com

Go To: [99] Table of Contents

반응형

'Programming > Manim Lectures' 카테고리의 다른 글

[03-5] Manim execution arguments  (0) 2020.06.01
[03-4] Watch samples (PyCham)  (1) 2020.05.31
[03-2] Create "Hello Manim" (PyCham)  (0) 2020.05.31
[03-1] Create "Hello Manim" (notepad++)  (0) 2020.05.31
[03]Hello Manim  (0) 2020.05.31