file_path
stringlengths 29
93
| content
stringlengths 0
117k
|
---|---|
manim_ManimCommunity/docs/source/_templates/autosummary/class.rst
|
{{ name | escape | underline}}
Qualified name: ``{{ fullname | escape }}``
.. currentmodule:: {{ module }}
.. autoclass:: {{ objname }}
:show-inheritance:
:members:
:private-members:
{% block methods %}
{%- if methods %}
.. rubric:: {{ _('Methods') }}
.. autosummary::
:nosignatures:
{% for item in methods if item != '__init__' and item not in inherited_members %}
~{{ name }}.{{ item }}
{%- endfor %}
{%- endif %}
{%- endblock %}
{% block attributes %}
{%- if attributes %}
.. rubric:: {{ _('Attributes') }}
.. autosummary::
{% for item in attributes %}
~{{ name }}.{{ item }}
{%- endfor %}
{%- endif %}
{% endblock %}
|
manim_ManimCommunity/docs/source/_templates/autosummary/module.rst
|
{{ name | escape | underline }}
.. currentmodule:: {{ fullname }}
.. automodule:: {{ fullname }}
{# SEE manim.utils.docbuild.autoaliasattr_directive #}
{# FOR INFORMATION ABOUT THE CUSTOM autoaliasattr DIRECTIVE! #}
.. autoaliasattr:: {{ fullname }}
{% block classes %}
{% if classes %}
.. rubric:: Classes
.. autosummary::
:toctree: .
:nosignatures:
{% for class in classes %}
{{ class }}
{% endfor %}
{% endif %}
{% endblock %}
{% block functions %}
{% if functions %}
.. rubric:: {{ _('Functions') }}
{% for item in functions %}
.. autofunction:: {{ item }}
{%- endfor %}
{% endif %}
{% endblock %}
{% block exceptions %}
{% if exceptions %}
.. rubric:: {{ _('Exceptions') }}
.. autosummary::
{% for item in exceptions %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}
{% block modules %}
{% if modules %}
.. rubric:: Modules
.. autosummary::
:toctree:
:recursive:
{% for item in modules %}
{{ item }}
{%- endfor %}
{% endif %}
{% endblock %}
|
manim_ManimCommunity/docs/source/guides/configuration.rst
|
Configuration
#############
Manim provides an extensive configuration system that allows it to adapt to
many different use cases. There are many configuration options that can be
configured at different times during the scene rendering process. Each option
can be configured programmatically via `the ManimConfig class`_, at the time
of command invocation via `command-line arguments`_, or at the time the library
is first imported via `the config files`_.
The most common, simplest, and recommended way to configure Manim is
via the command-line interface (CLI), which is described directly below.
Command-line arguments
**********************
By far the most commonly used command in the CLI is the ``render`` command,
which is used to render scene(s) to an output file.
It is used with the following arguments:
.. program-output:: manim render --help
:ellipsis: 9
However, since Manim defaults to the :code:`render` command whenever no command
is specified, the following form is far more common and can be used instead:
.. code-block:: bash
manim [OPTIONS] FILE [SCENES]
An example of using the above form is:
.. code-block:: bash
manim -qm file.py SceneOne
This asks Manim to search for a Scene class called :code:`SceneOne` inside the
file ``file.py`` and render it with medium quality (specified by the ``-qm`` flag).
Another frequently used flag is ``-p`` ("preview"), which makes manim
open the rendered video after it's done rendering.
.. note:: The ``-p`` flag does not change any properties of the global
``config`` dict. The ``-p`` flag is only a command-line convenience.
Advanced examples
=================
To render a scene in high quality, but only output the last frame of the scene
instead of the whole video, you can execute
.. code-block:: bash
manim -sqh <file.py> SceneName
The following example specifies the output file name (with the :code:`-o`
flag), renders only the first ten animations (:code:`-n` flag) with a white
background (:code:`-c` flag), and saves the animation as a ``.gif`` instead of as a
``.mp4`` file (``--format=gif`` flag). It uses the default quality and does not try to
open the file after it is rendered.
.. code-block:: bash
manim -o myscene --format=gif -n 0,10 -c WHITE <file.py> SceneName
A list of all CLI flags
========================
.. command-output:: manim --help
.. command-output:: manim render --help
.. command-output:: manim cfg --help
.. command-output:: manim plugins --help
The ManimConfig class
*********************
The most direct way of configuring Manim is through the global ``config`` object,
which is an instance of :class:`.ManimConfig`. Each property of this class is
a config option that can be accessed either with standard attribute syntax or
with dict-like syntax:
.. code-block:: pycon
>>> from manim import *
>>> config.background_color = WHITE
>>> config["background_color"] = WHITE
.. note:: The former is preferred; the latter is provided for backwards
compatibility.
Most classes, including :class:`.Camera`, :class:`.Mobject`, and
:class:`.Animation`, read some of their default configuration from the global
``config``.
.. code-block:: pycon
>>> Camera({}).background_color
<Color white>
>>> config.background_color = RED # 0xfc6255
>>> Camera({}).background_color
<Color #fc6255>
:class:`.ManimConfig` is designed to keep internal consistency. For example,
setting ``frame_y_radius`` will affect ``frame_height``:
.. code-block:: pycon
>>> config.frame_height
8.0
>>> config.frame_y_radius = 5.0
>>> config.frame_height
10.0
The global ``config`` object is meant to be the single source of truth for all
config options. All of the other ways of setting config options ultimately
change the values of the global ``config`` object.
The following example illustrates the video resolution chosen for examples
rendered in our documentation with a reference frame.
.. manim:: ShowScreenResolution
:save_last_frame:
class ShowScreenResolution(Scene):
def construct(self):
pixel_height = config["pixel_height"] # 1080 is default
pixel_width = config["pixel_width"] # 1920 is default
frame_width = config["frame_width"]
frame_height = config["frame_height"]
self.add(Dot())
d1 = Line(frame_width * LEFT / 2, frame_width * RIGHT / 2).to_edge(DOWN)
self.add(d1)
self.add(Text(str(pixel_width)).next_to(d1, UP))
d2 = Line(frame_height * UP / 2, frame_height * DOWN / 2).to_edge(LEFT)
self.add(d2)
self.add(Text(str(pixel_height)).next_to(d2, RIGHT))
The config files
****************
As the last example shows, executing Manim from the command line may involve
using many flags simultaneously. This may become a nuisance if you must
execute the same script many times in a short time period, for example, when
making small incremental tweaks to your scene script. For this reason, Manim
can also be configured using a configuration file. A configuration file is a
file ending with the suffix ``.cfg``.
To use a local configuration file when rendering your scene, you must create a
file with the name ``manim.cfg`` in the same directory as your scene code.
.. warning:: The config file **must** be named ``manim.cfg``. Currently, Manim
does not support config files with any other name.
The config file must start with the section header ``[CLI]``. The
configuration options under this header have the same name as the CLI flags
and serve the same purpose. Take, for example, the following config file.
.. code-block:: ini
[CLI]
# my config file
output_file = myscene
save_as_gif = True
background_color = WHITE
Config files are parsed with the standard python library ``configparser``. In
particular, they will ignore any line that starts with a pound symbol ``#``.
Now, executing the following command
.. code-block:: bash
manim -o myscene -i -c WHITE <file.py> SceneName
is equivalent to executing the following command, provided that ``manim.cfg``
is in the same directory as <file.py>,
.. code-block:: bash
manim <file.py> SceneName
.. tip:: The names of the configuration options admissible in config files are
exactly the same as the **long names** of the corresponding command-
line flags. For example, the ``-c`` and ``--background_color`` flags
are interchangeable, but the config file only accepts
:code:`background_color` as an admissible option.
Since config files are meant to replace CLI flags, all CLI flags can be set via
a config file. Moreover, any config option can be set via a config file,
whether or not it has an associated CLI flag. See the bottom of this document
for a list of all CLI flags and config options.
Manim will look for a ``manim.cfg`` config file in the same directory as the
file being rendered, and **not** in the directory of execution. For example,
.. code-block:: bash
manim -o myscene -i -c WHITE <path/to/file.py> SceneName
will use the config file found in ``path/to/file.py``, if any. It will **not**
use the config file found in the current working directory, even if it exists.
In this way, the user may keep different config files for different scenes or
projects, and execute them with the right configuration from anywhere in the
system.
The file described here is called the **folder-wide** config file because it
affects all scene scripts found in the same folder.
The user config file
====================
As explained in the previous section, a :code:`manim.cfg` config file only
affects the scene scripts in its same folder. However, the user may also
create a special config file that will apply to all scenes rendered by that
user. This is referred to as the **user-wide** config file, and it will apply
regardless of where Manim is executed from, and regardless of where the scene
script is stored.
The user-wide config file lives in a special folder, depending on the operating
system.
* Windows: :code:`UserDirectory`/AppData/Roaming/Manim/manim.cfg
* MacOS: :code:`UserDirectory`/.config/manim/manim.cfg
* Linux: :code:`UserDirectory`/.config/manim/manim.cfg
Here, :code:`UserDirectory` is the user's home folder.
.. note:: A user may have many **folder-wide** config files, one per folder,
but only one **user-wide** config file. Different users in the same
computer may each have their own user-wide config file.
.. warning:: Do not store scene scripts in the same folder as the user-wide
config file. In this case, the behavior is undefined.
Whenever you use Manim from anywhere in the system, Manim will look for a
user-wide config file and read its configuration.
Cascading config files
======================
What happens if you execute Manim and it finds both a folder-wide config file
and a user-wide config file? Manim will read both files, but if they are
incompatible, **the folder-wide file takes precedence**.
For example, take the following user-wide config file
.. code-block:: ini
# user-wide
[CLI]
output_file = myscene
save_as_gif = True
background_color = WHITE
and the following folder-wide file
.. code-block:: ini
# folder-wide
[CLI]
save_as_gif = False
Then, executing :code:`manim <file.py> SceneName` will be equivalent to not
using any config files and executing
.. code-block:: bash
manim -o myscene -c WHITE <file.py> SceneName
Any command-line flags have precedence over any config file. For example,
using the previous two config files and executing :code:`manim -c RED
<file.py> SceneName` is equivalent to not using any config files and
executing
.. code-block:: bash
manim -o myscene -c RED <file.py> SceneName
There is also a **library-wide** config file that determines Manim's default
behavior and applies to every user of the library. It has the least
precedence, so any config options in the user-wide and any folder-wide files
will override the library-wide file. This is referred to as the *cascading*
config file system.
.. warning:: **The user should not try to modify the library-wide file**.
Contributors should receive explicit confirmation from the core
developer team before modifying it.
Order of operations
*******************
.. raw:: html
<div class="mxgraph" style="max-width:100%;border:1px solid transparent;" data-mxgraph="{"highlight":"#0000ff","nav":true,"resize":true,"toolbar":"zoom layers lightbox","edit":"_blank","url":"https://drive.google.com/uc?id=1WYVKKoRbXrumHEcyQKQ9s1yCnBvfU2Ui&export=download"}"></div>
<script type="text/javascript" src="https://viewer.diagrams.net/embed2.js?&fetch=https%3A%2F%2Fdrive.google.com%2Fuc%3Fid%3D1WYVKKoRbXrumHEcyQKQ9s1yCnBvfU2Ui%26export%3Ddownload"></script>
With so many different ways of configuring Manim, it can be difficult to know
when each config option is being set. In fact, this will depend on how Manim
is being used.
If Manim is imported from a module, then the configuration system will follow
these steps:
1. The library-wide config file is loaded.
2. The user-wide and folder-wide files are loaded if they exist.
3. All files found in the previous two steps are parsed in a single
:class:`ConfigParser` object, called ``parser``. This is where *cascading*
happens.
4. :class:`logging.Logger` is instantiated to create Manim's global ``logger``
object. It is configured using the "logger" section of the parser,
i.e. ``parser['logger']``.
5. :class:`ManimConfig` is instantiated to create the global ``config`` object.
6. The ``parser`` from step 3 is fed into the ``config`` from step 5 via
:meth:`ManimConfig.digest_parser`.
7. Both ``logger`` and ``config`` are exposed to the user.
If Manim is being invoked from the command line, all of the previous steps
happen, and are complemented by:
8. The CLI flags are parsed and fed into ``config`` via
:meth:`~ManimConfig.digest_args`.
9. If the ``--config_file`` flag was used, a new :class:`ConfigParser` object
is created with the contents of the library-wide file, the user-wide file if
it exists, and the file passed via ``--config_file``. In this case, the
folder-wide file, if it exists, is ignored.
10. The new parser is fed into ``config``.
11. The rest of the CLI flags are processed.
To summarize, the order of precedence for configuration options, from lowest to
highest precedence is:
1. Library-wide config file,
2. user-wide config file, if it exists,
3. folder-wide config file, if it exists OR custom config file, if passed via
``--config_file``,
4. other CLI flags, and
5. any programmatic changes made after the config system is set.
A list of all config options
****************************
.. code::
['aspect_ratio', 'assets_dir', 'background_color', 'background_opacity',
'bottom', 'custom_folders', 'disable_caching', 'dry_run',
'ffmpeg_loglevel', 'flush_cache', 'frame_height', 'frame_rate',
'frame_size', 'frame_width', 'frame_x_radius', 'frame_y_radius',
'from_animation_number', `fullscreen`, 'images_dir', 'input_file', 'left_side',
'log_dir', 'log_to_file', 'max_files_cached', 'media_dir', 'media_width',
'movie_file_extension', 'notify_outdated_version', 'output_file', 'partial_movie_dir',
'pixel_height', 'pixel_width', 'plugins', 'preview',
'progress_bar', 'quality', 'right_side', 'save_as_gif', 'save_last_frame',
'save_pngs', 'scene_names', 'show_in_file_browser', 'sound', 'tex_dir',
'tex_template', 'tex_template_file', 'text_dir', 'top', 'transparent',
'upto_animation_number', 'use_opengl_renderer', 'verbosity', 'video_dir',
'window_position', 'window_monitor', 'window_size', 'write_all', 'write_to_movie',
'enable_wireframe', 'force_window']
Accessing CLI command options
*****************************
Entering ``manim``, or ``manim --help``, will open the main help page.
.. code::
Usage: manim [OPTIONS] COMMAND [ARGS]...
Animation engine for explanatory math videos.
Options:
--version Show version and exit.
--help Show this message and exit.
Commands:
cfg Manages Manim configuration files.
init Sets up a new project in current working directory with default
settings.
It copies files from templates directory and pastes them in the
current working dir.
new Create a new project or insert a new scene.
plugins Manages Manim plugins.
render Render SCENE(S) from the input FILE.
See 'manim <command>' to read about a specific subcommand.
Made with <3 by Manim Community developers.
Each of the subcommands has its own help page which can be accessed similarly:
.. code::
manim render
manim render --help
|
manim_ManimCommunity/docs/source/guides/index.rst
|
Thematic Guides
===============
.. toctree::
:caption: Table of Contents
:maxdepth: 2
:glob:
configuration
deep_dive
using_text
add_voiceovers
|
manim_ManimCommunity/docs/source/guides/deep_dive.rst
|
A deep dive into Manim's internals
==================================
**Author:** `Benjamin Hackl <https://benjamin-hackl.at>`__
.. admonition:: Disclaimer
This guide reflects the state of the library as of version ``v0.16.0``
and primarily treats the Cairo renderer. The situation in the latest
version of Manim might be different; in case of substantial deviations
we will add a note below.
Introduction
------------
Manim can be a wonderful library, if it behaves the way you would like it to,
and/or the way you expect it to. Unfortunately, this is not always the case
(as you probably know if you have played with some manimations yourself already).
To understand where things *go wrong*, digging through the library's source code
is sometimes the only option -- but in order to do that, you need to know where
to start digging.
This article is intended as some sort of life line through the render process.
We aim to give an appropriate amount of detail describing what happens when
Manim reads your scene code and produces the corresponding animation. Throughout
this article, we will focus on the following toy example::
from manim import *
class ToyExample(Scene):
def construct(self):
orange_square = Square(color=ORANGE, fill_opacity=0.5)
blue_circle = Circle(color=BLUE, fill_opacity=0.5)
self.add(orange_square)
self.play(ReplacementTransform(orange_square, blue_circle, run_time=3))
small_dot = Dot()
small_dot.add_updater(lambda mob: mob.next_to(blue_circle, DOWN))
self.play(Create(small_dot))
self.play(blue_circle.animate.shift(RIGHT))
self.wait()
self.play(FadeOut(blue_circle, small_dot))
Before we go into details or even look at the rendered output of this scene,
let us first describe verbally what happens in this *manimation*. In the first
three lines of the ``construct`` method, a :class:`.Square` and a :class:`.Circle`
are initialized, then the square is added to the scene. The first frame of the
rendered output should thus show an orange square.
Then the actual animations happen: the square first transforms into a circle,
then a :class:`.Dot` is created (Where do you guess the dot is located when
it is first added to the scene? Answering this already requires detailed
knowledge about the render process.). The dot has an updater attached to it, and
as the circle moves right, the dot moves with it. In the end, all mobjects are
faded out.
Actually rendering the code yields the following video:
.. manim:: ToyExample
:hide_source:
class ToyExample(Scene):
def construct(self):
orange_square = Square(color=ORANGE, fill_opacity=0.5)
blue_circle = Circle(color=BLUE, fill_opacity=0.5)
self.add(orange_square)
self.play(ReplacementTransform(orange_square, blue_circle, run_time=3))
small_dot = Dot()
small_dot.add_updater(lambda mob: mob.next_to(blue_circle, DOWN))
self.play(Create(small_dot))
self.play(blue_circle.animate.shift(RIGHT))
self.wait()
self.play(FadeOut(blue_circle, small_dot))
For this example, the output (fortunately) coincides with our expectations.
Overview
--------
Because there is a lot of information in this article, here is a brief overview
discussing the contents of the following chapters on a very high level.
- `Preliminaries`_: In this chapter we unravel all the steps that take place
to prepare a scene for rendering; right until the point where the user-overridden
``construct`` method is ran. This includes a brief discussion on using Manim's CLI
versus other means of rendering (e.g., via Jupyter notebooks, or in your Python
script by calling the :meth:`.Scene.render` method yourself).
- `Mobject Initialization`_: For the second chapter we dive into creating and handling
Mobjects, the basic elements that should be displayed in our scene.
We discuss the :class:`.Mobject` base class, how there are essentially
three different types of Mobjects, and then discuss the most important of them,
vectorized Mobjects. In particular, we describe the internal point data structure
that governs how the mechanism responsible for drawing the vectorized Mobject
to the screen sets the corresponding Bézier curves. We conclude the chapter
with a tour into :meth:`.Scene.add`, the bookkeeping mechanism controlling which
mobjects should be rendered.
- `Animations and the Render Loop`_: And finally, in the last chapter we walk
through the instantiation of :class:`.Animation` objects (the blueprints that
hold information on how Mobjects should be modified when the render loop runs),
followed by a investigation of the infamous :meth:`.Scene.play` call. We will
see that there are three relevant parts in a :meth:`.Scene.play` call;
a part in which the passed animations and keyword arguments are processed
and prepared, followed by the actual "render loop" in which the library
steps through a time line and renders frame by frame. The final part
does some post-processing to save a short video segment ("partial movie file")
and cleanup for the next call to :meth:`.Scene.play`. In the end, after all of
:meth:`.Scene.construct` has been run, the library combines the partial movie
files to one video.
And with that, let us get *in medias res*.
Preliminaries
-------------
Importing the library
^^^^^^^^^^^^^^^^^^^^^
Independent of how exactly you are telling your system
to render the scene, i.e., whether you run ``manim -qm -p file_name.py ToyExample``, or
whether you are rendering the scene directly from the Python script via a snippet
like
::
with tempconfig({"quality": "medium_quality", "preview": True}):
scene = ToyExample()
scene.render()
or whether you are rendering the code in a Jupyter notebook, you are still telling your
python interpreter to import the library. The usual pattern used to do this is
::
from manim import *
which (while being a debatable strategy in general) imports a lot of classes and
functions shipped with the library and makes them available in your global name space.
I explicitly avoided stating that it imports **all** classes and functions of the
library, because it does not do that: Manim makes use of the practice described
in `Section 6.4.1 of the Python tutorial <https://docs.python.org/3/tutorial/modules.html#importing-from-a-package>`__,
and all module members that should be exposed to the user upon running the ``*``-import
are explicitly declared in the ``__all__`` variable of the module.
Manim also uses this strategy internally: taking a peek at the file that is run when
the import is called, ``__init__.py`` (see
`here <https://github.com/ManimCommunity/manim/blob/main/manim/__init__.py>`__),
you will notice that most of the code in that module is concerned with importing
members from various different submodules, again using ``*``-imports.
.. hint::
If you would ever contribute a new submodule to Manim, the main
``__init__.py`` is where it would have to be listed in order to make its
members accessible to users after importing the library.
In that file, there is one particular import at the beginning of the file however,
namely::
from ._config import *
This initializes Manim's global configuration system, which is used in various places
throughout the library. After the library runs this line, the current configuration
options are set. The code in there takes care of reading the options in your ``.cfg``
files (all users have at least the global one that is shipped with the library)
as well as correctly handling command line arguments (if you used the CLI to render).
You can read more about the config system in the
:doc:`corresponding thematic guide </guides/configuration>`, and if you are interested in learning
more about the internals of the configuration system and how it is initialized,
follow the code flow starting in `the config module's init file
<https://github.com/ManimCommunity/manim/blob/main/manim/_config/__init__.py>`__.
Now that the library is imported, we can turn our attention to the next step:
reading your scene code (which is not particularly exciting, Python just creates
a new class ``ToyExample`` based on our code; Manim is virtually not involved
in that step, with the exception that ``ToyExample`` inherits from ``Scene``).
However, with the ``ToyExample`` class created and ready to go, there is a new
excellent question to answer: how is the code in our ``construct`` method
actually executed?
Scene instantiation and rendering
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The answer to this question depends on how exactly you are running the code.
To make things a bit clearer, let us first consider the case that you
have created a file ``toy_example.py`` which looks like this::
from manim import *
class ToyExample(Scene):
def construct(self):
orange_square = Square(color=ORANGE, fill_opacity=0.5)
blue_circle = Circle(color=BLUE, fill_opacity=0.5)
self.add(orange_square)
self.play(ReplacementTransform(orange_square, blue_circle, run_time=3))
small_dot = Dot()
small_dot.add_updater(lambda mob: mob.next_to(blue_circle, DOWN))
self.play(Create(small_dot))
self.play(blue_circle.animate.shift(RIGHT))
self.wait()
self.play(FadeOut(blue_circle, small_dot))
with tempconfig({"quality": "medium_quality", "preview": True}):
scene = ToyExample()
scene.render()
With such a file, the desired scene is rendered by simply running this Python
script via ``python toy_example.py``. Then, as described above, the library
is imported and Python has read and defined the ``ToyExample`` class (but,
read carefully: *no instance of this class has been created yet*).
At this point, the interpreter is about to enter the ``tempconfig`` context
manager. Even if you have not seen Manim's ``tempconfig`` before, its name
already suggests what it does: it creates a copy of the current state of the
configuration, applies the changes to the key-value pairs in the passed
dictionary, and upon leaving the context the original version of the
configuration is restored. TL;DR: it provides a fancy way of temporarily setting
configuration options.
Inside the context manager, two things happen: an actual ``ToyExample``-scene
object is instantiated, and the ``render`` method is called. Every way of using
Manim ultimately does something along of these lines, the library always instantiates
the scene object and then calls its ``render`` method. To illustrate that this
really is the case, let us briefly look at the two most common ways of rendering
scenes:
**Command Line Interface.** When using the CLI and running the command
``manim -qm -p toy_example.py ToyExample`` in your terminal, the actual
entry point is Manim's ``__main__.py`` file (located
`here <https://github.com/ManimCommunity/manim/blob/main/manim/__main__.py>`__.
Manim uses `Click <https://click.palletsprojects.com/en/8.0.x/>`__ to implement
the command line interface, and the corresponding code is located in Manim's
``cli`` module (https://github.com/ManimCommunity/manim/tree/main/manim/cli).
The corresponding code creating the scene class and calling its render method
is located `here <https://github.com/ManimCommunity/manim/blob/ac1ee9a683ce8b92233407351c681f7d71a4f2db/manim/cli/render/commands.py#L139-L141>`__.
**Jupyter notebooks.** In Jupyter notebooks, the communication with the library
is handled by the ``%%manim`` magic command, which is implemented in the
``manim.utils.ipython_magic`` module. There is
:meth:`some documentation <.ManimMagic.manim>` available for the magic command,
and the code creating the scene class and calling its render method is located
`here <https://github.com/ManimCommunity/manim/blob/ac1ee9a683ce8b92233407351c681f7d71a4f2db/manim/utils/ipython_magic.py#L137-L138>`__.
Now that we know that either way, a :class:`.Scene` object is created, let us investigate
what Manim does when that happens. When instantiating our scene object
::
scene = ToyExample()
the ``Scene.__init__`` method is called, given that we did not implement our own initialization
method. Inspecting the corresponding code (see
`here <https://github.com/ManimCommunity/manim/blob/main/manim/scene/scene.py>`__)
reveals that ``Scene.__init__`` first sets several attributes of the scene objects that do not
depend on any configuration options set in ``config``. Then the scene inspects the value of
``config.renderer``, and based on its value, either instantiates a ``CairoRenderer`` or an
``OpenGLRenderer`` object and assigns it to its ``renderer`` attribute.
The scene then asks its renderer to initialize the scene by calling
::
self.renderer.init_scene(self)
Inspecting both the default Cairo renderer and the OpenGL renderer shows that the ``init_scene``
method effectively makes the renderer instantiate a :class:`.SceneFileWriter` object, which
basically is Manim's interface to ``ffmpeg`` and actually writes the movie file. The Cairo
renderer (see the implementation `here <https://github.com/ManimCommunity/manim/blob/main/manim/renderer/cairo_renderer.py>`__) does not require any further initialization. The OpenGL renderer
does some additional setup to enable the realtime rendering preview window, which we do not go
into detail further here.
.. warning::
Currently, there is a lot of interplay between a scene and its renderer. This is a flaw
in Manim's current architecture, and we are working on reducing this interdependency to
achieve a less convoluted code flow.
After the renderer has been instantiated and initialized its file writer, the scene populates
further initial attributes (notable mention: the ``mobjects`` attribute which keeps track
of the mobjects that have been added to the scene). It is then done with its instantiation
and ready to be rendered.
The rest of this article is concerned with the last line in our toy example script::
scene.render()
This is where the actual magic happens.
Inspecting the `implementation of the render method <https://github.com/ManimCommunity/manim/blob/df1a60421ea1119cbbbd143ef288d294851baaac/manim/scene/scene.py#L211>`__
reveals that there are several hooks that can be used for pre- or postprocessing
a scene. Unsurprisingly, :meth:`.Scene.render` describes the full *render cycle*
of a scene. During this life cycle, there are three custom methods whose base
implementation is empty and that can be overwritten to suit your purposes. In
the order they are called, these customizable methods are:
- :meth:`.Scene.setup`, which is intended for preparing and, well, *setting up*
the scene for your animation (e.g., adding initial mobjects, assigning custom
attributes to your scene class, etc.),
- :meth:`.Scene.construct`, which is the *script* for your screen play and
contains programmatic descriptions of your animations, and
- :meth:`.Scene.tear_down`, which is intended for any operations you might
want to run on the scene after the last frame has already been rendered
(for example, this could run some code that generates a custom thumbnail
for the video based on the state of the objects in the scene -- this
hook is more relevant for situations where Manim is used within other
Python scripts).
After these three methods are run, the animations have been fully rendered,
and Manim calls :meth:`.CairoRenderer.scene_finished` to gracefully
complete the rendering process. This checks whether any animations have been
played -- and if so, it tells the :class:`.SceneFileWriter` to close the pipe
to ``ffmpeg``. If not, Manim assumes that a static image should be output
which it then renders using the same strategy by calling the render loop
(see below) once.
**Back in our toy example,** the call to :meth:`.Scene.render` first
triggers :meth:`.Scene.setup` (which only consists of ``pass``), followed by
a call of :meth:`.Scene.construct`. At this point, our *animation script*
is run, starting with the initialization of ``orange_square``.
Mobject Initialization
----------------------
Mobjects are, in a nutshell, the Python objects that represent all the
*things* we want to display in our scene. Before we follow our debugger
into the depths of mobject initialization code, it makes sense to
discuss Manim's different types of Mobjects and their basic data
structure.
What even is a Mobject?
^^^^^^^^^^^^^^^^^^^^^^^
:class:`.Mobject` stands for *mathematical object* or *Manim object*
(depends on who you ask 😄). The Python class :class:`.Mobject` is
the base class for all objects that should be displayed on screen.
Looking at the `initialization method
<https://github.com/ManimCommunity/manim/blob/5d72d9cfa2e3dd21c844b1da807576f5a7194fda/manim/mobject/mobject.py#L94>`__
of :class:`.Mobject`, you will find that not too much happens in there:
- some initial attribute values are assigned, like ``name`` (which makes the
render logs mention the name of the mobject instead of its type),
``submobjects`` (initially an empty list), ``color``, and some others.
- Then, two methods related to *points* are called: ``reset_points``
followed by ``generate_points``,
- and finally, ``init_colors`` is called.
Digging deeper, you will find that :meth:`.Mobject.reset_points` simply
sets the ``points`` attribute of the mobject to an empty NumPy vector,
while the other two methods, :meth:`.Mobject.generate_points` and
:meth:`.Mobject.init_colors` are just implemented as ``pass``.
This makes sense: :class:`.Mobject` is not supposed to be used as
an *actual* object that is displayed on screen; in fact the camera
(which we will discuss later in more detail; it is the class that is,
for the Cairo renderer, responsible for "taking a picture" of the
current scene) does not process "pure" :class:`Mobjects <.Mobject>`
in any way, they *cannot* even appear in the rendered output.
This is where different types of mobjects come into play. Roughly
speaking, the Cairo renderer setup knows three different types of
mobjects that can be rendered:
- :class:`.ImageMobject`, which represent images that you can display
in your scene,
- :class:`.PMobject`, which are very special mobjects used to represent
point clouds; we will not discuss them further in this guide,
- :class:`.VMobject`, which are *vectorized mobjects*, that is, mobjects
that consist of points that are connected via curves. These are pretty
much everywhere, and we will discuss them in detail in the next section.
... and what are VMobjects?
^^^^^^^^^^^^^^^^^^^^^^^^^^^
As just mentioned, :class:`VMobjects <.VMobject>` represent vectorized
mobjects. To render a :class:`.VMobject`, the camera looks at the
``points`` attribute of a :class:`.VMobject` and divides it into sets
of four points each. Each of these sets is then used to construct a
cubic Bézier curve with the first and last entry describing the
end points of the curve ("anchors"), and the second and third entry
describing the control points in between ("handles").
.. hint::
To learn more about Bézier curves, take a look at the excellent
online textbook `A Primer on Bézier curves <https://pomax.github.io/bezierinfo/>`__
by `Pomax <https://twitter.com/TheRealPomax>`__ -- there is a playground representing
cubic Bézier curves `in §1 <https://pomax.github.io/bezierinfo/#introduction>`__,
the red and yellow points are "anchors", and the green and blue
points are "handles".
In contrast to :class:`.Mobject`, :class:`.VMobject` can be displayed
on screen (even though, technically, it is still considered a base class).
To illustrate how points are processed, consider the following short example
of a :class:`.VMobject` with 8 points (and thus made out of 8/4 = 2 cubic
Bézier curves). The resulting :class:`.VMobject` is drawn in green.
The handles are drawn as red dots with a line to their closest anchor.
.. manim:: VMobjectDemo
:save_last_frame:
class VMobjectDemo(Scene):
def construct(self):
plane = NumberPlane()
my_vmobject = VMobject(color=GREEN)
my_vmobject.points = [
np.array([-2, -1, 0]), # start of first curve
np.array([-3, 1, 0]),
np.array([0, 3, 0]),
np.array([1, 3, 0]), # end of first curve
np.array([1, 3, 0]), # start of second curve
np.array([0, 1, 0]),
np.array([4, 3, 0]),
np.array([4, -2, 0]), # end of second curve
]
handles = [
Dot(point, color=RED) for point in
[[-3, 1, 0], [0, 3, 0], [0, 1, 0], [4, 3, 0]]
]
handle_lines = [
Line(
my_vmobject.points[ind],
my_vmobject.points[ind+1],
color=RED,
stroke_width=2
) for ind in range(0, len(my_vmobject.points), 2)
]
self.add(plane, *handles, *handle_lines, my_vmobject)
.. warning::
Manually setting the points of your :class:`.VMobject` is usually
discouraged; there are specialized methods that can take care of
that for you -- but it might be relevant when implementing your own,
custom :class:`.VMobject`.
Squares and Circles: back to our Toy Example
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
With a basic understanding of different types of mobjects,
and an idea of how vectorized mobjects are built we can now
come back to our toy example and the execution of the
:meth:`.Scene.construct` method. In the first two lines
of our animation script, the ``orange_square`` and the
``blue_circle`` are initialized.
When creating the orange square by running
::
Square(color=ORANGE, fill_opacity=0.5)
the initialization method of :class:`.Square`,
``Square.__init__``, is called. `Looking at the
implementation <https://github.com/ManimCommunity/manim/blob/5d72d9cfa2e3dd21c844b1da807576f5a7194fda/manim/mobject/geometry/polygram.py#L607>`__,
we can see that the ``side_length`` attribute of the square is set,
and then
::
super().__init__(height=side_length, width=side_length, **kwargs)
is called. This ``super`` call is the Python way of calling the
initialization function of the parent class. As :class:`.Square`
inherits from :class:`.Rectangle`, the next method called
is ``Rectangle.__init__``. There, only the first three lines
are really relevant for us::
super().__init__(UR, UL, DL, DR, color=color, **kwargs)
self.stretch_to_fit_width(width)
self.stretch_to_fit_height(height)
First, the initialization function of the parent class of
:class:`.Rectangle` -- :class:`.Polygon` -- is called. The
four positional arguments passed are the four corners of
the polygon: ``UR`` is up right (and equal to ``UP + RIGHT``),
``UL`` is up left (and equal to ``UP + LEFT``), and so forth.
Before we follow our debugger deeper, let us observe what
happens with the constructed polygon: the remaining two lines
stretch the polygon to fit the specified width and height
such that a rectangle with the desired measurements is created.
The initialization function of :class:`.Polygon` is particularly
simple, it only calls the initialization function of its parent
class, :class:`.Polygram`. There, we have almost reached the end
of the chain: :class:`.Polygram` inherits from :class:`.VMobject`,
whose initialization function mainly sets the values of some
attributes (quite similar to ``Mobject.__init__``, but more specific
to the Bézier curves that make up the mobject).
After calling the initialization function of :class:`.VMobject`,
the constructor of :class:`.Polygram` also does something somewhat
odd: it sets the points (which, you might remember above, should
actually be set in a corresponding ``generate_points`` method
of :class:`.Polygram`).
.. warning::
In several instances, the implementation of mobjects does
not really stick to all aspects of Manim's interface. This
is unfortunate, and increasing consistency is something
that we actively work on. Help is welcome!
Without going too much into detail, :class:`.Polygram` sets its
``points`` attribute via :meth:`.VMobject.start_new_path`,
:meth:`.VMobject.add_points_as_corners`, which take care of
setting the quadruples of anchors and handles appropriately.
After the points are set, Python continues to process the
call stack until it reaches the method that was first called;
the initialization method of :class:`.Square`. After this,
the square is initialized and assigned to the ``orange_square``
variable.
The initialization of ``blue_circle`` is similar to the one of
``orange_square``, with the main difference being that the inheritance
chain of :class:`.Circle` is different. Let us briefly follow the trace
of the debugger:
The implementation of :meth:`.Circle.__init__` immediately calls
the initialization method of :class:`.Arc`, as a circle in Manim
is simply an arc with an angle of :math:`\tau = 2\pi`. When
initializing the arc, some basic attributes are set (like
``Arc.radius``, ``Arc.arc_center``, ``Arc.start_angle``, and
``Arc.angle``), and then the initialization method of its
parent class, :class:`.TipableVMobject`, is called (which is
a rather abstract base class for mobjects which a arrow tip can
be attached to). Note that in contrast to :class:`.Polygram`,
this class does **not** preemptively generate the points of the circle.
After that, things are less exciting: :class:`.TipableVMobject` again
sets some attributes relevant for adding arrow tips, and afterwards
passes to the initialization method of :class:`.VMobject`. From there,
:class:`.Mobject` is initialized and :meth:`.Mobject.generate_points`
is called, which actually runs the method implemented in
:meth:`.Arc.generate_points`.
After both our ``orange_square`` and the ``blue_circle`` are initialized,
the square is actually added to the scene. The :meth:`.Scene.add` method
is actually doing a few interesting things, so it is worth to dig a bit
deeper in the next section.
Adding Mobjects to the Scene
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The code in our ``construct`` method that is run next is
::
self.add(orange_square)
From a high-level point of view, :meth:`.Scene.add` adds the
``orange_square`` to the list of mobjects that should be rendered,
which is stored in the ``mobjects`` attribute of the scene. However,
it does so in a very careful way to avoid the situation that a mobject
is being added to the scene more than once. At a first glance, this
sounds like a simple task -- the problem is that ``Scene.mobjects``
is not a "flat" list of mobjects, but a list of mobjects which
might contain mobjects themselves, and so on.
Stepping through the code in :meth:`.Scene.add`, we see that first
it is checked whether we are currently using the OpenGL renderer
(which we are not) -- adding mobjects to the scene works slightly
different (and actually easier!) for the OpenGL renderer. Then, the
code branch for the Cairo renderer is entered and the list of so-called
foreground mobjects (which are rendered on top of all other mobjects)
is added to the list of passed mobjects. This is to ensure that the
foreground mobjects will stay above of the other mobjects, even after
adding the new ones. In our case, the list of foreground mobjects
is actually empty, and nothing changes.
Next, :meth:`.Scene.restructure_mobjects` is called with the list
of mobjects to be added as the ``to_remove`` argument, which might
sound odd at first. Practically, this ensures that mobjects are not
added twice, as mentioned above: if they were present in the scene
``Scene.mobjects`` list before (even if they were contained as a
child of some other mobject), they are first removed from the list.
The way :meth:`.Scene.restrucutre_mobjects` works is rather aggressive:
It always operates on a given list of mobjects; in the ``add`` method
two different lists occur: the default one, ``Scene.mobjects`` (no extra
keyword argument is passed), and ``Scene.moving_mobjects`` (which we will
discuss later in more detail). It iterates through all of the members of
the list, and checks whether any of the mobjects passed in ``to_remove``
are contained as children (in any nesting level). If so, **their parent
mobject is deconstructed** and their siblings are inserted directly
one level higher. Consider the following example::
>>> from manim import Scene, Square, Circle, Group
>>> test_scene = Scene()
>>> mob1 = Square()
>>> mob2 = Circle()
>>> mob_group = Group(mob1, mob2)
>>> test_scene.add(mob_group)
<manim.scene.scene.Scene object at ...>
>>> test_scene.mobjects
[Group]
>>> test_scene.restructure_mobjects(to_remove=[mob1])
<manim.scene.scene.Scene object at ...>
>>> test_scene.mobjects
[Circle]
Note that the group is disbanded and the circle moves into the
root layer of mobjects in ``test_scene.mobjects``.
After the mobject list is "restructured", the mobject to be added
are simply appended to ``Scene.mobjects``. In our toy example,
the ``Scene.mobjects`` list is actually empty, so the
``restructure_mobjects`` method does not actually do anything. The
``orange_square`` is simply added to ``Scene.mobjects``, and as
the aforementioned ``Scene.moving_mobjects`` list is, at this point,
also still empty, nothing happens and :meth:`.Scene.add` returns.
We will hear more about the ``moving_mobject`` list when we discuss
the render loop. Before we do that, let us look at the next line
of code in our toy example, which includes the initialization of
an animation class,
::
ReplacementTransform(orange_square, blue_circle, run_time=3)
Hence it is time to talk about :class:`.Animation`.
Animations and the Render Loop
------------------------------
Initializing animations
^^^^^^^^^^^^^^^^^^^^^^^
Before we follow the trace of the debugger, let us briefly discuss
the general structure of the (abstract) base class :class:`.Animation`.
An animation object holds all the information necessary for the renderer
to generate the corresponding frames. Animations (in the sense of
animation objects) in Manim are *always* tied to a specific mobject;
even in the case of :class:`.AnimationGroup` (which you should actually
think of as an animation on a group of mobjects rather than a group
of animations). Moreover, except for in a particular special case,
the run time of animations is also fixed and known beforehand.
The initialization of animations actually is not very exciting,
:meth:`.Animation.__init__` merely sets some attributes derived
from the passed keyword arguments and additionally ensures that
the ``Animation.starting_mobject`` and ``Animation.mobject``
attributes are populated. Once the animation is played, the
``starting_mobject`` attribute holds an unmodified copy of the
mobject the animation is attached to; during the initialization
it is set to a placeholder mobject. The ``mobject`` attribute
is set to the mobject the animation is attached to.
Animations have a few special methods which are called during the
render loop:
- :meth:`.Animation.begin`, which is called (as hinted by its name)
at the beginning of every animation, so before the first frame
is rendered. In it, all the required setup for the animation happens.
- :meth:`.Animation.finish` is the counterpart to the ``begin`` method
which is called at the end of the life cycle of the animation (after
the last frame has been rendered).
- :meth:`.Animation.interpolate` is the method that updates the mobject
attached to the animation to the corresponding animation completion
percentage. For example, if in the render loop,
``some_animation.interpolate(0.5)`` is called, the attached mobject
will be updated to the state where 50% of the animation are completed.
We will discuss details about these and some further animation methods
once we walk through the actual render loop. For now, we continue with
our toy example and the code that is run when initializing the
:class:`.ReplacementTransform` animation.
The initialization method of :class:`.ReplacementTransform` only
consists of a call to the constructor of its parent class,
:class:`.Transform`, with the additional keyword argument
``replace_mobject_with_target_in_scene`` set to ``True``.
:class:`.Transform` then sets attributes that control how the
points of the starting mobject are deformed into the points of
the target mobject, and then passes on to the initialization
method of :class:`.Animation`. Other basic properties of the
animation (like its ``run_time``, the ``rate_func``, etc.) are
processed there -- and then the animation object is fully
initialized and ready to be played.
The ``play`` call: preparing to enter Manim's render loop
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
We are finally there, the render loop is in our reach. Let us
walk through the code that is run when :meth:`.Scene.play` is called.
.. hint::
Recall that this article is specifically about the Cairo renderer.
Up to here, things were more or less the same for the OpenGL renderer
as well; while some base mobjects might be different, the control flow
and lifecycle of mobjects is still more or less the same. There are more
substantial differences when it comes to the rendering loop.
As you will see when inspecting the method, :meth:`.Scene.play` almost
immediately passes over to the ``play`` method of the renderer,
in our case :class:`.CairoRenderer.play`. The one thing :meth:`.Scene.play`
takes care of is the management of subcaptions that you might have
passed to it (see the the documentation of :meth:`.Scene.play` and
:meth:`.Scene.add_subcaption` for more information).
.. warning::
As has been said before, the communication between scene and renderer
is not in a very clean state at this point, so the following paragraphs
might be confusing if you don't run a debugger and step through the
code yourself a bit.
Inside :meth:`.CairoRenderer.play`, the renderer first checks whether
it may skip rendering of the current play call. This might happen, for example,
when ``-s`` is passed to the CLI (i.e., only the last frame should be rendered),
or when the ``-n`` flag is passed and the current play call is outside of the
specified render bounds. The "skipping status" is updated in form of the
call to :meth:`.CairoRenderer.update_skipping_status`.
Next, the renderer asks the scene to process the animations in the play
call so that renderer obtains all of the information it needs. To
be more concrete, :meth:`.Scene.compile_animation_data` is called,
which then takes care of several things:
- The method processes all animations and the keyword arguments passed
to the initial :meth:`.Scene.play` call. In particular, this means
that it makes sure all arguments passed to the play call are actually
animations (or ``.animate`` syntax calls, which are also assembled to
be actual :class:`.Animation`-objects at that point). It also propagates
any animation-related keyword arguments (like ``run_time``,
or ``rate_func``) passed to :class:`.Scene.play` to each individual
animation. The processed animations are then stored in the ``animations``
attribute of the scene (which the renderer later reads...).
- It adds all mobjects to which the animations that are played are
bound to to the scene (provided the animation is not an mobject-introducing
animation -- for these, the addition to the scene happens later).
- In case the played animation is a :class:`.Wait` animation (this is the
case in a :meth:`.Scene.wait` call), the method checks whether a static
image should be rendered, or whether the render loop should be processed
as usual (see :meth:`.Scene.should_update_mobjects` for the exact conditions,
basically it checks whether there are any time-dependent updater functions
and so on).
- Finally, the method determines the total run time of the play call (which
at this point is computed as the maximum of the run times of the passed
animations). This is stored in the ``duration`` attribute of the scene.
After the animation data has been compiled by the scene, the renderer
continues to prepare for entering the render loop. It now checks the
skipping status which has been determined before. If the renderer can
skip this play call, it does so: it sets the current play call hash (which
we will get back to in a moment) to ``None`` and increases the time of the
renderer by the determined animation run time.
Otherwise, the renderer checks whether or not Manim's caching system should
be used. The idea of the caching system is simple: for every play call, a
hash value is computed, which is then stored and upon re-rendering the scene,
the hash is generated again and checked against the stored value. If it is the
same, the cached output is reused, otherwise it is fully rerendered again.
We will not go into details of the caching system here; if you would like
to learn more, the :func:`.get_hash_from_play_call` function in the
:mod:`.utils.hashing` module is essentially the entry point to the caching
mechanism.
In the event that the animation has to be rendered, the renderer asks
its :class:`.SceneFileWriter` to start a writing process. The process
is started by a call to ``ffmpeg`` and opens a pipe to which rendered
raw frames can be written. As long as the pipe is open, the process
can be accessed via the ``writing_process`` attribute of the file writer.
With the writing process in place, the renderer then asks the scene
to "begin" the animations.
First, it literally *begins* all of the animations by calling their
setup methods (:meth:`.Animation._setup_scene`, :meth:`.Animation.begin`).
In doing so, the mobjects that are newly introduced by an animation
(like via :class:`.Create` etc.) are added to the scene. Furthermore, the
animation suspends updater functions being called on its mobject, and
it sets its mobject to the state that corresponds to the first frame
of the animation.
After this has happened for all animations in the current ``play`` call,
the Cairo renderer determines which of the scene's mobjects can be
painted statically to the background, and which ones have to be
redrawn every frame. It does so by calling
:meth:`.Scene.get_moving_and_static_mobjects`, and the resulting
partition of mobjects is stored in the corresponding ``moving_mobjects``
and ``static_mobjects`` attributes.
.. NOTE::
The mechanism that determines static and moving mobjects is
specific for the Cairo renderer, the OpenGL renderer works differently.
Basically, moving mobjects are determined by checking whether they,
any of their children, or any of the mobjects "below" them (in the
sense of the order in which mobjects are processed in the scene)
either have an update function attached, or whether they appear
in one of the current animations. See the implementation of
:meth:`.Scene.get_moving_mobjects` for more details.
Up to this very point, we did not actually render any (partial)
image or movie files from the scene yet. This is, however, about to change.
Before we enter the render loop, let us briefly revisit our toy
example and discuss how the generic :meth:`.Scene.play` call
setup looks like there.
For the call that plays the :class:`.ReplacementTransform`, there
is no subcaption to be taken care of. The renderer then asks
the scene to compile the animation data: the passed argument
already is an animation (no additional preparations needed),
there is no need for processing any keyword arguments (as
we did not specify any additional ones to ``play``). The
mobject bound to the animation, ``orange_square``, is already
part of the scene (so again, no action taken). Finally, the run
time is extracted (3 seconds long) and stored in
``Scene.duration``. The renderer then checks whether it should
skip (it should not), then whether the animation is already
cached (it is not). The corresponding animation hash value is
determined and passed to the file writer, which then also calls
``ffmpeg`` to start the writing process which waits for rendered
frames from the library.
The scene then ``begin``\ s the animation: for the
:class:`.ReplacementTransform` this means that the animation populates
all of its relevant animation attributes (i.e., compatible copies
of the starting and the target mobject so that it can safely interpolate
between the two).
The mechanism determining static and moving mobjects considers
all of the scenes mobjects (at this point only the
``orange_square``), and determines that the ``orange_square`` is
bound to an animation that is currently played. As a result,
the square is classified as a "moving mobject".
Time to render some frames.
The render loop (for real this time)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
As mentioned above, due to the mechanism that determines static and moving
mobjects in the scene, the renderer knows which mobjects it can paint
statically to the background of the scene. Practically, this means that
it partially renders a scene (to produce a background image), and then
when iterating through the time progression of the animation only the
"moving mobjects" are re-painted on top of the static background.
The renderer calls :meth:`.CairoRenderer.save_static_frame_data`, which
first checks whether there are currently any static mobjects, and if there
are, it updates the frame (only with the static mobjects; more about how
exactly this works in a moment) and then saves a NumPy array representing
the rendered frame in the ``static_image`` attribute. In our toy example,
there are no static mobjects, and so the ``static_image`` attribute is
simply set to ``None``.
Next, the renderer asks the scene whether the current animation is
a "frozen frame" animation, which would mean that the renderer actually
does not have to repaint the moving mobjects in every frame of the time
progression. It can then just take the latest static frame, and display it
throughout the animation.
.. NOTE::
An animation is considered a "frozen frame" animation if only a
static :class:`.Wait` animation is played. See the description
of :meth:`.Scene.compile_animation_data` above, or the
implementation of :meth:`.Scene.should_update_mobjects` for
more details.
If this is not the case (just as in our toy example), the renderer
then calls the :meth:`.Scene.play_internal` method, which is the
integral part of the render loop (in which the library steps through
the time progression of the animation and renders the corresponding
frames).
Within :meth:`.Scene.play_internal`, the following steps are performed:
- The scene determines the run time of the animations by calling
:meth:`.Scene.get_run_time`. This method basically takes the maximum
``run_time`` attribute of all of the animations passed to the
:meth:`.Scene.play` call.
- Then the *time progression* is constructed via the (internal)
:meth:`.Scene._get_animation_time_progression` method, which wraps
the actual :meth:`.Scene.get_time_progression` method. The time
progression is a ``tqdm`` `progress bar object <https://tqdm.github.io>`__
for an iterator over ``np.arange(0, run_time, 1 / config.frame_rate)``. In
other words, the time progression holds the time stamps (relative to the
current animations, so starting at 0 and ending at the total animation run time,
with the step size determined by the render frame rate) of the timeline where
a new animation frame should be rendered.
- Then the scene iterates over the time progression: for each time stamp ``t``,
:meth:`.Scene.update_to_time` is called, which ...
- ... first computes the time passed since the last update (which might be 0,
especially for the initial call) and references it as ``dt``,
- then (in the order in which the animations are passed to :meth:`.Scene.play`)
calls :meth:`.Animation.update_mobjects` to trigger all updater functions that
are attached to the respective animation except for the "main mobject" of
the animation (that is, for example, for :class:`.Transform` the unmodified
copies of start and target mobject -- see :meth:`.Animation.get_all_mobjects_to_update`
for more details),
- then the relative time progression with respect to the current animation
is computed (``alpha = t / animation.run_time``), which is then used to
update the state of the animation with a call to :meth:`.Animation.interpolate`.
- After all of the passed animations have been processed, the updater functions
of all mobjects in the scene, all meshes, and finally those attached to
the scene itself are run.
At this point, the internal (Python) state of all mobjects has been updated
to match the currently processed timestamp. If rendering should not be skipped,
then it is now time to *take a picture*!
.. NOTE::
The update of the internal state (iteration over the time progression) happens
*always* once :meth:`.Scene.play_internal` is entered. This ensures that even
if frames do not need to be rendered (because, e.g., the ``-n`` CLI flag has
been passed, something has been cached, or because we might be in a *Section*
with skipped rendering), updater functions still run correctly, and the state
of the first frame that *is* rendered is kept consistent.
To render an image, the scene calls the corresponding method of its renderer,
:meth:`.CairoRenderer.render` and passes just the list of *moving mobjects* (remember,
the *static mobjects* are assumed to have already been painted statically to
the background of the scene). All of the hard work then happens when the renderer
updates its current frame via a call to :meth:`.CairoRenderer.update_frame`:
First, the renderer prepares its :class:`.Camera` by checking whether the renderer
has a ``static_image`` different from ``None`` stored already. If so, it sets the
image as the *background image* of the camera via :meth:`.Camera.set_frame_to_background`,
and otherwise it just resets the camera via :meth:`.Camera.reset`. The camera is then
asked to capture the scene with a call to :meth:`.Camera.capture_mobjects`.
Things get a bit technical here, and at some point it is more efficient to
delve into the implementation -- but here is a summary of what happens once the
camera is asked to capture the scene:
- First, a flat list of mobjects is created (so submobjects get extracted from
their parents). This list is then processed in groups of the same type of
mobjects (e.g., a batch of vectorized mobjects, followed by a batch of image mobjects,
followed by more vectorized mobjects, etc. -- in many cases there will just be
one batch of vectorized mobjects).
- Depending on the type of the currently processed batch, the camera uses dedicated
*display functions* to convert the :class:`.Mobject` Python object to
a NumPy array stored in the camera's ``pixel_array`` attribute.
The most important example in that context is the display function for
vectorized mobjects, :meth:`.Camera.display_multiple_vectorized_mobjects`,
or the more particular (in case you did not add a background image to your
:class:`.VMobject`), :meth:`.Camera.display_multiple_non_background_colored_vmobjects`.
This method first gets the current Cairo context, and then, for every (vectorized)
mobject in the batch, calls :meth:`.Camera.display_vectorized`. There,
the actual background stroke, fill, and then stroke of the mobject is
drawn onto the context. See :meth:`.Camera.apply_stroke` and
:meth:`.Camera.set_cairo_context_color` for more details -- but it does not get
much deeper than that, in the latter method the actual Bézier curves
determined by the points of the mobject are drawn; this is where the low-level
interaction with Cairo happens.
After all batches have been processed, the camera has an image representation
of the Scene at the current time stamp in form of a NumPy array stored in its
``pixel_array`` attribute. The renderer then takes this array and passes it to
its :class:`.SceneFileWriter`. This concludes one iteration of the render loop,
and once the time progression has been processed completely, a final bit
of cleanup is performed before the :meth:`.Scene.play_internal` call is completed.
A TL;DR for the render loop, in the context of our toy example, reads as follows:
- The scene finds that a 3 second long animation (the :class:`.ReplacementTransform`
changing the orange square to the blue circle) should be played. Given the requested
medium render quality, the frame rate is 30 frames per second, and so the time
progression with steps ``[0, 1/30, 2/30, ..., 89/30]`` is created.
- In the internal render loop, each of these time stamps is processed:
there are no updater functions, so effectively the scene updates the
state of the transformation animation to the desired time stamp (for example,
at time stamp ``t = 45/30``, the animation is completed to a rate of
``alpha = 0.5``).
- Then the scene asks the renderer to do its job. The renderer asks its camera
to capture the scene, the only mobject that needs to be processed at this point
is the main mobject attached to the transformation; the camera converts the
current state of the mobject to entries in a NumPy array. The renderer passes
this array to the file writer.
- At the end of the loop, 90 frames have been passed to the file writer.
Completing the render loop
^^^^^^^^^^^^^^^^^^^^^^^^^^
The last few steps in the :meth:`.Scene.play_internal` call are not too
exciting: for every animation, the corresponding :meth:`.Animation.finish`
and :meth:`.Animation.clean_up_from_scene` methods are called.
.. NOTE::
Note that as part of :meth:`.Animation.finish`, the :meth:`.Animation.interpolate`
method is called with an argument of 1.0 -- you might have noticed already that
the last frame of an animation can sometimes be a bit off or incomplete.
This is by current design! The last frame rendered in the render loop (and displayed
for a duration of ``1 / frame_rate`` seconds in the rendered video) corresponds to
the state of the animation ``1 / frame_rate`` seconds before it ends. To display
the final frame as well in the video, we would need to append another ``1 / frame_rate``
seconds to the video -- which would then mean that a 1 second rendered Manim video
would be slightly longer than 1 second. We decided against this at some point.
In the end, the time progression is closed (which completes the displayed progress bar)
in the terminal. With the closing of the time progression, the
:meth:`.Scene.play_internal` call is completed, and we return to the renderer,
which now orders the :class:`.SceneFileWriter` to close the movie pipe that has
been opened for this animation: a partial movie file is written.
This pretty much concludes the walkthrough of a :class:`.Scene.play` call,
and actually there is not too much more to say for our toy example either: at
this point, a partial movie file that represents playing the
:class:`.ReplacementTransform` has been written. The initialization of
the :class:`.Dot` happens analogous to the initialization of ``blue_circle``,
which has been discussed above. The :meth:`.Mobject.add_updater` call literally
just attaches a function to the ``updaters`` attribute of the ``small_dot``. And
the remaining :meth:`.Scene.play` and :meth:`.Scene.wait` calls follow the
exact same procedure as discussed in the render loop section above; each such call
produces a corresponding partial movie file.
Once the :meth:`.Scene.construct` method has been fully processed (and thus all
of the corresponding partial movie files have been written), the
scene calls its cleanup method :meth:`.Scene.tear_down`, and then
asks its renderer to finish the scene. The renderer, in turn, asks
its scene file writer to wrap things up by calling :meth:`.SceneFileWriter.finish`,
which triggers the combination of the partial movie files into the final product.
And there you go! This is a more or less detailed description of how Manim works
under the hood. While we did not discuss every single line of code in detail
in this walkthrough, it should still give you a fairly good idea of how the general
structural design of the library and at least the Cairo rendering flow in particular
looks like.
|
manim_ManimCommunity/docs/source/guides/using_text.rst
|
###########################
Rendering Text and Formulas
###########################
There are two different ways by which you can render **Text** in videos:
1. Using Pango (:mod:`~.text_mobject`)
2. Using LaTeX (:mod:`~.tex_mobject`)
If you want to render simple text, you should use either :class:`~.Text` or
:class:`~.MarkupText`, or one of its derivatives like :class:`~.Paragraph`.
See :ref:`using-text-objects` for more information.
LaTeX should be used when you need mathematical typesetting. See
:ref:`rendering-with-latex` for more information.
.. _using-text-objects:
Text Without LaTeX
******************
The simplest way to add text to your animations is to use the :class:`~.Text`
class. It uses the `Pango library`_ to render text. With Pango, you can also
render non-English alphabets like 你好 or こんにちは or 안녕하세요 or
مرحبا بالعالم.
Here is a simple *Hello World* animation.
.. manim:: HelloWorld
:save_last_frame:
:ref_classes: Text
class HelloWorld(Scene):
def construct(self):
text = Text("Hello world", font_size=144)
self.add(text)
You can also use :class:`~.MarkupText` which allows the use of PangoMarkup
(see the documentation of :class:`~.MarkupText` for details) to render text.
For example:
.. manim:: SingleLineColor
:save_last_frame:
:ref_classes: MarkupText
class SingleLineColor(Scene):
def construct(self):
text = MarkupText(
f'all in red <span fgcolor="{YELLOW}">except this</span>', color=RED
)
self.add(text)
.. _Pango library: https://pango.gnome.org
Working with :class:`~.Text`
============================
This section explains the properties of :class:`~.Text` and how can it be used
in your animations.
Using Fonts
-----------
You can set a different font using :attr:`~.Text.font`.
.. note::
The font used must be installed in your system, and Pango should know
about it. You can get a list of fonts using :func:`manimpango.list_fonts`.
>>> import manimpango
>>> manimpango.list_fonts()
[...]
.. manim:: FontsExample
:save_last_frame:
class FontsExample(Scene):
def construct(self):
ft = Text("Noto Sans", font="Noto Sans")
self.add(ft)
Setting Slant and Weight
------------------------
Slant is the style of the Text, and it can be ``NORMAL`` (the default),
``ITALIC`` or ``OBLIQUE``. Usually, for many fonts both ``ITALIC`` and
``OBLIQUE`` look similar, but ``ITALIC`` uses **Roman Style**, whereas
``OBLIQUE`` uses **Italic Style**.
Weight specifies the boldness of a font. You can see a list of weights in
:class:`manimpango.Weight`.
.. manim:: SlantsExample
:save_last_frame:
class SlantsExample(Scene):
def construct(self):
a = Text("Italic", slant=ITALIC)
self.add(a)
.. manim:: DifferentWeight
:save_last_frame:
class DifferentWeight(Scene):
def construct(self):
import manimpango
g = VGroup()
weight_list = dict(
sorted(
{
weight: manimpango.Weight(weight).value
for weight in manimpango.Weight
}.items(),
key=lambda x: x[1],
)
)
for weight in weight_list:
g += Text(weight.name, weight=weight.name, font="Open Sans")
self.add(g.arrange(DOWN).scale(0.5))
.. _using-colors:
Using Colors
------------
You can set the color of the text using :attr:`~.Text.color`:
.. manim:: SimpleColor
:save_last_frame:
class SimpleColor(Scene):
def construct(self):
col = Text("RED COLOR", color=RED)
self.add(col)
You can use utilities like :attr:`~.Text.t2c` for coloring specific characters.
This may be problematic if your text contains ligatures
as explained in :ref:`iterating-text`.
:attr:`~Text.t2c` accepts two types of dictionaries,
* The keys can contain indices like ``[2:-1]`` or ``[4:8]``,
this works similar to how `slicing <https://realpython.com/python-strings/#string-slicing>`_
works in Python. The values should be the color of the Text from :class:`~.Color`.
* The keys contain words or characters which should be colored separately
and the values should be the color from :class:`~.Color`:
.. manim:: Textt2cExample
:save_last_frame:
class Textt2cExample(Scene):
def construct(self):
t2cindices = Text('Hello', t2c={'[1:-1]': BLUE}).move_to(LEFT)
t2cwords = Text('World',t2c={'rl':RED}).next_to(t2cindices, RIGHT)
self.add(t2cindices, t2cwords)
If you want to avoid problems when using colors (due to ligatures), consider using
:class:`MarkupText`.
Using Gradients
---------------
You can add a gradient using :attr:`~.Text.gradient`. The value must
be an iterable of any length:
.. manim:: GradientExample
:save_last_frame:
class GradientExample(Scene):
def construct(self):
t = Text("Hello", gradient=(RED, BLUE, GREEN), font_size=96)
self.add(t)
You can also use :attr:`~.Text.t2g` for gradients with specific
characters of the text. It shares a similar syntax to :ref:`the
interface for colors <using-colors>`:
.. manim:: t2gExample
:save_last_frame:
class t2gExample(Scene):
def construct(self):
t2gindices = Text(
'Hello',
t2g={
'[1:-1]': (RED,GREEN),
},
).move_to(LEFT)
t2gwords = Text(
'World',
t2g={
'World':(RED,BLUE),
},
).next_to(t2gindices, RIGHT)
self.add(t2gindices, t2gwords)
Setting Line Spacing
--------------------
You can set the line spacing using :attr:`~.Text.line_spacing`:
.. manim:: LineSpacing
:save_last_frame:
class LineSpacing(Scene):
def construct(self):
a = Text("Hello\nWorld", line_spacing=1)
b = Text("Hello\nWorld", line_spacing=4)
self.add(Group(a,b).arrange(LEFT, buff=5))
.. _disable-ligatures:
Disabling Ligatures
-------------------
By disabling ligatures you would get a one-to-one mapping between characters and
submobjects. This fixes the issues with coloring text.
.. warning::
Be aware that using this method with text that heavily depends on
ligatures (Arabic text) may yield unexpected results.
You can disable ligatures by passing ``disable_ligatures`` to
:class:`Text`. For example:
.. manim:: DisableLigature
:save_last_frame:
class DisableLigature(Scene):
def construct(self):
li = Text("fl ligature",font_size=96)
nli = Text("fl ligature", disable_ligatures=True, font_size=96)
self.add(Group(li, nli).arrange(DOWN, buff=.8))
.. _iterating-text:
Iterating :class:`~.Text`
-------------------------
Text objects behave like :class:`VGroups <.VGroup>`. Therefore, you can slice and index
the text.
For example, you can set each letter to different color by iterating it.
.. manim:: IterateColor
:save_last_frame:
class IterateColor(Scene):
def construct(self):
text = Text("Colors", font_size=96)
for letter in text:
letter.set_color(random_bright_color())
self.add(text)
.. warning::
Please note that `Ligature`_ can cause problems here. If you need a
one-to-one mapping of characters to submobjects you should pass
the ``disable_ligatures`` parameter to :class:`~.Text`.
See :ref:`disable-ligatures`.
.. _Ligature: https://en.wikipedia.org/wiki/Ligature_(writing)
Working with :class:`~.MarkupText`
==================================
MarkupText is similar to :class:`~.Text`, the only difference between them is
that this accepts and processes PangoMarkup (which is similar to
html), instead of just rendering plain text.
Consult the documentation of :class:`~.MarkupText` for more details
and further references about PangoMarkup.
.. manim:: MarkupTest
:save_last_frame:
class MarkupTest(Scene):
def construct(self):
text = MarkupText(
f'<span underline="double" underline_color="green">double green underline</span> in red text<span fgcolor="{YELLOW}"> except this</span>',
color=RED,
font_size=34
)
self.add(text)
.. _rendering-with-latex:
Text With LaTeX
***************
Just as you can use :class:`~.Text` to add text to your videos, you can
use :class:`~.Tex` to insert LaTeX.
For example,
.. manim:: HelloLaTeX
:save_last_frame:
class HelloLaTeX(Scene):
def construct(self):
tex = Tex(r"\LaTeX", font_size=144)
self.add(tex)
.. note::
Note that we are using a raw string (``r'...'``) instead of a regular string (``'...'``).
This is because TeX code uses a lot of special characters - like ``\`` for example - that
have special meaning within a regular python string. An alternative would have been to
write ``\\`` to escape the backslash: ``Tex('\\LaTeX')``.
Working with :class:`~.MathTex`
===============================
Everything passed to :class:`~.MathTex` is in math mode by default. To be more precise,
:class:`~.MathTex` is processed within an ``align*`` environment. You can achieve a
similar effect with :class:`~.Tex` by enclosing your formula with ``$`` symbols:
``$\xrightarrow{x^6y^8}$``:
.. manim:: MathTeXDemo
:save_last_frame:
class MathTeXDemo(Scene):
def construct(self):
rtarrow0 = MathTex(r"\xrightarrow{x^6y^8}", font_size=96)
rtarrow1 = Tex(r"$\xrightarrow{x^6y^8}$", font_size=96)
self.add(VGroup(rtarrow0, rtarrow1).arrange(DOWN))
LaTeX commands and keyword arguments
====================================
We can use any standard LaTeX commands in the AMS maths packages. Such
as the ``mathtt`` math-text type or the ``looparrowright`` arrow.
.. manim:: AMSLaTeX
:save_last_frame:
class AMSLaTeX(Scene):
def construct(self):
tex = Tex(r'$\mathtt{H} \looparrowright$ \LaTeX', font_size=144)
self.add(tex)
On the Manim side, the :class:`~.Tex` class also accepts attributes to
change the appearance of the output. This is very similar to the
:class:`~.Text` class. For example, the ``color`` keyword changes the
color of the TeX mobject.
.. manim:: LaTeXAttributes
:save_last_frame:
class LaTeXAttributes(Scene):
def construct(self):
tex = Tex(r'Hello \LaTeX', color=BLUE, font_size=144)
self.add(tex)
Extra LaTeX Packages
====================
Some commands require special packages to be loaded into the TeX template.
For example, to use the ``mathscr`` script, we need to add the ``mathrsfs``
package. Since this package isn't loaded into Manim's tex template by default,
we have to add it manually.
.. manim:: AddPackageLatex
:save_last_frame:
class AddPackageLatex(Scene):
def construct(self):
myTemplate = TexTemplate()
myTemplate.add_to_preamble(r"\usepackage{mathrsfs}")
tex = Tex(
r"$\mathscr{H} \rightarrow \mathbb{H}$",
tex_template=myTemplate,
font_size=144,
)
self.add(tex)
Substrings and parts
====================
The TeX mobject can accept multiple strings as arguments. Afterwards you can
refer to the individual parts either by their index (like ``tex[1]``), or by
selecting parts of the tex code. In this example, we set the color
of the ``\bigstar`` using :func:`~.set_color_by_tex`:
.. manim:: LaTeXSubstrings
:save_last_frame:
class LaTeXSubstrings(Scene):
def construct(self):
tex = Tex('Hello', r'$\bigstar$', r'\LaTeX', font_size=144)
tex.set_color_by_tex('igsta', RED)
self.add(tex)
Note that :func:`~.set_color_by_tex` colors the entire substring containing
the Tex, not just the specific symbol or Tex expression. Consider the following example:
.. manim:: IncorrectLaTeXSubstringColoring
:save_last_frame:
class IncorrectLaTeXSubstringColoring(Scene):
def construct(self):
equation = MathTex(
r"e^x = x^0 + x^1 + \frac{1}{2} x^2 + \frac{1}{6} x^3 + \cdots + \frac{1}{n!} x^n + \cdots"
)
equation.set_color_by_tex("x", YELLOW)
self.add(equation)
As you can see, this colors the entire equation yellow, contrary to what
may be expected. To color only ``x`` yellow, we have to do the following:
.. manim:: CorrectLaTeXSubstringColoring
:save_last_frame:
class CorrectLaTeXSubstringColoring(Scene):
def construct(self):
equation = MathTex(
r"e^x = x^0 + x^1 + \frac{1}{2} x^2 + \frac{1}{6} x^3 + \cdots + \frac{1}{n!} x^n + \cdots",
substrings_to_isolate="x"
)
equation.set_color_by_tex("x", YELLOW)
self.add(equation)
By setting ``substrings_to_isolate`` to ``x``, we split up the
:class:`~.MathTex` into substrings automatically and isolate the ``x`` components
into individual substrings. Only then can :meth:`~.set_color_by_tex` be used
to achieve the desired result.
Note that Manim also supports a custom syntax that allows splitting
a TeX string into substrings easily: simply enclose parts of your formula
that you want to isolate with double braces. In the string
``MathTex(r"{{ a^2 }} + {{ b^2 }} = {{ c^2 }}")``, the rendered mobject
will consist of the substrings ``a^2``, ``+``, ``b^2``, ``=``, and ``c^2``.
This makes transformations between similar text fragments easy
to write using :class:`~.TransformMatchingTex`.
Using ``index_labels`` to work with complicated strings
=======================================================
You might sometimes be working with a very complicated :class:`~.MathTex` mobject
that makes it difficult to work with its individual components. This is
where the debugging function :func:`.index_labels` is very useful.
The method shows the index of a mobject's submobjects, allowing you
to easily find the components of the mobject you would like to change.
.. manim:: IndexLabelsMathTex
:save_last_frame:
class IndexLabelsMathTex(Scene):
def construct(self):
text = MathTex(r"\binom{2n}{n+2}", font_size=96)
# index the first (and only) term of the MathTex mob
self.add(index_labels(text[0]))
text[0][1:3].set_color(YELLOW)
text[0][3:6].set_color(RED)
self.add(text)
LaTeX Maths Fonts - The Template Library
========================================
Changing fonts in LaTeX when typesetting mathematical formulae is
trickier than regular text. It requires changing the template that is used
to compile the TeX. Manim comes with a collection of :class:`~.TexFontTemplates`
ready for you to use. These templates will all work in math mode:
.. manim:: LaTeXMathFonts
:save_last_frame:
class LaTeXMathFonts(Scene):
def construct(self):
tex = Tex(
r"$x^2 + y^2 = z^2$",
tex_template=TexFontTemplates.french_cursive,
font_size=144,
)
self.add(tex)
Manim also has a :class:`~.TexTemplateLibrary` containing the TeX
templates used by 3Blue1Brown. One example is the ctex template,
used for typesetting Chinese script. For this to work, the ctex LaTeX package
must be installed on your system. Furthermore, if you are only
typesetting Text, you probably do not need :class:`~.Tex` at all, and
should use :class:`~.Text` instead.
.. manim:: LaTeXTemplateLibrary
:save_last_frame:
class LaTeXTemplateLibrary(Scene):
def construct(self):
tex = Tex('Hello 你好 \\LaTeX', tex_template=TexTemplateLibrary.ctex, font_size=144)
self.add(tex)
Aligning formulae
=================
:class:`~.MathTex` mobject is typeset in the LaTeX ``align*``
environment. This means you can use the ``&`` alignment character
when typesetting multiline formulae:
.. manim:: LaTeXAlignEnvironment
:save_last_frame:
class LaTeXAlignEnvironment(Scene):
def construct(self):
tex = MathTex(r'f(x) &= 3 + 2 + 1\\ &= 5 + 1 \\ &= 6', font_size=96)
self.add(tex)
|
manim_ManimCommunity/docs/source/guides/add_voiceovers.rst
|
###########################
Adding Voiceovers to Videos
###########################
Creating a full-fledged video with voiceovers is a bit more involved than
creating purely visual Manim scenes. One has to use `a video editing
program <https://en.wikipedia.org/wiki/List_of_video_editing_software>`__
to add the voiceovers after the video has been rendered. This process
can be difficult and time-consuming, since it requires a lot of planning
and preparation.
To ease the process of adding voiceovers to videos, we have created
`Manim Voiceover <https://voiceover.manim.community>`__, a plugin
that lets you add voiceovers to scenes directly in Python. To install it, run
.. code-block:: bash
pip install "manim-voiceover[azure,gtts]"
Visit `the installation page <https://voiceover.manim.community/en/latest/installation.html>`__
for more details on how to install Manim Voiceover.
Basic Usage
###########
Manim Voiceover lets you ...
- Add voiceovers to Manim videos directly in Python, without having to use a video editor.
- Record voiceovers with your microphone during rendering through a simple command line interface.
- Develop animations with auto-generated AI voices from various free and proprietary services.
It provides a very simple API that lets you specify your voiceover script
and then record it during rendering:
.. code-block:: python
from manim import *
from manim_voiceover import VoiceoverScene
from manim_voiceover.services.recorder import RecorderService
# Simply inherit from VoiceoverScene instead of Scene to get all the
# voiceover functionality.
class RecorderExample(VoiceoverScene):
def construct(self):
# You can choose from a multitude of TTS services,
# or in this example, record your own voice:
self.set_speech_service(RecorderService())
circle = Circle()
# Surround animation sections with with-statements:
with self.voiceover(text="This circle is drawn as I speak.") as tracker:
self.play(Create(circle), run_time=tracker.duration)
# The duration of the animation is received from the audio file
# and passed to the tracker automatically.
# This part will not start playing until the previous voiceover is finished.
with self.voiceover(text="Let's shift it to the left 2 units.") as tracker:
self.play(circle.animate.shift(2 * LEFT), run_time=tracker.duration)
To get started with Manim Voiceover,
visit the `Quick Start Guide <https://voiceover.manim.community/en/latest/quickstart.html>`__.
Visit the `Example Gallery <https://voiceover.manim.community/en/latest/examples.html>`__
to see some examples of Manim Voiceover in action.
|
manim_ManimCommunity/docs/source/tutorials/quickstart.rst
|
==========
Quickstart
==========
.. note::
Before proceeding, install Manim and make sure it's running properly by
following the steps in :doc:`../installation`. For
information on using Manim with Jupyterlab or Jupyter notebook, go to the
documentation for the
:meth:`IPython magic command <manim.utils.ipython_magic.ManimMagic.manim>`,
``%%manim``.
Overview
********
This quickstart guide will lead you through creating a sample project using Manim: an animation
engine for precise programmatic animations.
First, you will use a command line
interface to create a ``Scene``, the class through which Manim generates videos.
In the ``Scene`` you will animate a circle. Then you will add another ``Scene`` showing
a square transforming into a circle. This will be your introduction to Manim's animation ability.
Afterwards, you will position multiple mathematical objects (``Mobject``\s). Finally, you
will learn the ``.animate`` syntax, a powerful feature that animates the methods you
use to modify ``Mobject``\s.
Starting a new project
**********************
Start by creating a new folder. For the purposes of this guide, name the folder ``project``:
.. code-block:: bash
project/
This folder is the root folder for your project. It contains all the files that Manim needs to function,
as well as any output that your project produces.
Animating a circle
******************
1. Open a text editor, such as Notepad. Copy the following code snippet into the window:
.. code-block:: python
from manim import *
class CreateCircle(Scene):
def construct(self):
circle = Circle() # create a circle
circle.set_fill(PINK, opacity=0.5) # set the color and transparency
self.play(Create(circle)) # show the circle on screen
2. Save the code snippet into your project folder with the name ``scene.py``.
.. code-block:: bash
project/
└─scene.py
3. Open the command line, navigate to your project folder, and execute
the following command:
.. code-block:: bash
manim -pql scene.py CreateCircle
Manim will output rendering information, then create an MP4 file.
Your default movie player will play the MP4 file, displaying the following animation.
.. manim:: CreateCircle
:hide_source:
class CreateCircle(Scene):
def construct(self):
circle = Circle() # create a circle
circle.set_fill(PINK, opacity=0.5) # set the color and transparency
self.play(Create(circle)) # show the circle on screen
If you see an animation of a pink circle being drawn, congratulations!
You just wrote your first Manim scene from scratch.
If you get an error
message instead, you do not see a video, or if the video output does not
look like the preceding animation, it is likely that Manim has not been
installed correctly. Please refer to our :doc:`FAQ section </faq/index>`
for help with the most common issues.
***********
Explanation
***********
Let's go over the script you just executed line by line to see how Manim was
able to draw the circle.
The first line imports all of the contents of the library:
.. code-block:: python
from manim import *
This is the recommended way of using Manim, as a single script often uses
multiple names from the Manim namespace. In your script, you imported and used
``Scene``, ``Circle``, ``PINK`` and ``Create``.
Now let's look at the next two lines:
.. code-block:: python
class CreateCircle(Scene):
def construct(self):
...
Most of the time, the code for scripting an animation is entirely contained within
the :meth:`~.Scene.construct` method of a :class:`.Scene` class.
Inside :meth:`~.Scene.construct`, you can create objects, display them on screen, and animate them.
The next two lines create a circle and set its color and opacity:
.. code-block:: python
circle = Circle() # create a circle
circle.set_fill(PINK, opacity=0.5) # set the color and transparency
Finally, the last line uses the animation :class:`.Create` to display the
circle on your screen:
.. code-block:: python
self.play(Create(circle)) # show the circle on screen
.. tip:: All animations must reside within the :meth:`~.Scene.construct` method of a
class derived from :class:`.Scene`. Other code, such as auxiliary
or mathematical functions, may reside outside the class.
Transforming a square into a circle
***********************************
With our circle animation complete, let's move on to something a little more complicated.
1. Open ``scene.py``, and add the following code snippet below the ``CreateCircle`` class:
.. code-block:: python
class SquareToCircle(Scene):
def construct(self):
circle = Circle() # create a circle
circle.set_fill(PINK, opacity=0.5) # set color and transparency
square = Square() # create a square
square.rotate(PI / 4) # rotate a certain amount
self.play(Create(square)) # animate the creation of the square
self.play(Transform(square, circle)) # interpolate the square into the circle
self.play(FadeOut(square)) # fade out animation
2. Render ``SquareToCircle`` by running the following command in the command line:
.. code-block:: bash
manim -pql scene.py SquareToCircle
The following animation will render:
.. manim:: SquareToCircle2
:hide_source:
class SquareToCircle2(Scene):
def construct(self):
circle = Circle() # create a circle
circle.set_fill(PINK, opacity=0.5) # set color and transparency
square = Square() # create a square
square.rotate(PI / 4) # rotate a certain amount
self.play(Create(square)) # animate the creation of the square
self.play(Transform(square, circle)) # interpolate the square into the circle
self.play(FadeOut(square)) # fade out animation
This example shows one of the primary features of Manim: the ability to
implement complicated and mathematically intensive animations (such as cleanly
interpolating between two geometric shapes) with just a few lines of code.
Positioning ``Mobject``\s
*************************
Next, let's go over some basic techniques for positioning ``Mobject``\s.
1. Open ``scene.py``, and add the following code snippet below the ``SquareToCircle`` method:
.. code-block:: python
class SquareAndCircle(Scene):
def construct(self):
circle = Circle() # create a circle
circle.set_fill(PINK, opacity=0.5) # set the color and transparency
square = Square() # create a square
square.set_fill(BLUE, opacity=0.5) # set the color and transparency
square.next_to(circle, RIGHT, buff=0.5) # set the position
self.play(Create(circle), Create(square)) # show the shapes on screen
2. Render ``SquareAndCircle`` by running the following command in the command line:
.. code-block:: bash
manim -pql scene.py SquareAndCircle
The following animation will render:
.. manim:: SquareAndCircle2
:hide_source:
class SquareAndCircle2(Scene):
def construct(self):
circle = Circle() # create a circle
circle.set_fill(PINK, opacity=0.5) # set the color and transparency
square = Square() # create a square
square.set_fill(BLUE, opacity=0.5) # set the color and transparency
square.next_to(circle, RIGHT, buff=0.5) # set the position
self.play(Create(circle), Create(square)) # show the shapes on screen
``next_to`` is a ``Mobject`` method for positioning ``Mobject``\s.
We first specified
the pink circle as the square's reference point by passing ``circle`` as the method's first argument.
The second argument is used to specify the direction the ``Mobject`` is placed relative to the reference point.
In this case, we set the direction to ``RIGHT``, telling Manim to position the square to the right of the circle.
Finally, ``buff=0.5`` applied a small distance buffer between the two objects.
Try changing ``RIGHT`` to ``LEFT``, ``UP``, or ``DOWN`` instead, and see how that changes the position of the square.
Using positioning methods, you can render a scene with multiple ``Mobject``\s,
setting their locations in the scene using coordinates or positioning them
relative to each other.
For more information on ``next_to`` and other positioning methods, check out the
list of :class:`.Mobject` methods in our reference manual.
Using ``.animate`` syntax to animate methods
********************************************
The final lesson in this tutorial is using ``.animate``, a ``Mobject`` method which
animates changes you make to a ``Mobject``. When you prepend ``.animate`` to any
method call that modifies a ``Mobject``, the method becomes an animation which
can be played using ``self.play``. Let's return to ``SquareToCircle`` to see the
differences between using methods when creating a ``Mobject``,
and animating those method calls with ``.animate``.
1. Open ``scene.py``, and add the following code snippet below the ``SquareAndCircle`` class:
.. code-block:: python
class AnimatedSquareToCircle(Scene):
def construct(self):
circle = Circle() # create a circle
square = Square() # create a square
self.play(Create(square)) # show the square on screen
self.play(square.animate.rotate(PI / 4)) # rotate the square
self.play(Transform(square, circle)) # transform the square into a circle
self.play(
square.animate.set_fill(PINK, opacity=0.5)
) # color the circle on screen
2. Render ``AnimatedSquareToCircle`` by running the following command in the command line:
.. code-block:: bash
manim -pql scene.py AnimatedSquareToCircle
The following animation will render:
.. manim:: AnimatedSquareToCircle2
:hide_source:
class AnimatedSquareToCircle2(Scene):
def construct(self):
circle = Circle() # create a circle
square = Square() # create a square
self.play(Create(square)) # show the square on screen
self.play(square.animate.rotate(PI / 4)) # rotate the square
self.play(Transform(square, circle)) # transform the square into a circle
self.play(square.animate.set_fill(PINK, opacity=0.5)) # color the circle on screen
The first ``self.play`` creates the square. The second animates rotating it 45 degrees.
The third transforms the square into a circle, and the last colors the circle pink.
Although the end result is the same as that of ``SquareToCircle``, ``.animate`` shows
``rotate`` and ``set_fill`` being applied to the ``Mobject`` dynamically, instead of creating them
with the changes already applied.
Try other methods, like ``flip`` or ``shift``, and see what happens.
3. Open ``scene.py``, and add the following code snippet below the ``AnimatedSquareToCircle`` class:
.. code-block:: python
class DifferentRotations(Scene):
def construct(self):
left_square = Square(color=BLUE, fill_opacity=0.7).shift(2 * LEFT)
right_square = Square(color=GREEN, fill_opacity=0.7).shift(2 * RIGHT)
self.play(
left_square.animate.rotate(PI), Rotate(right_square, angle=PI), run_time=2
)
self.wait()
4. Render ``DifferentRotations`` by running the following command in the command line:
.. code-block:: bash
manim -pql scene.py DifferentRotations
The following animation will render:
.. manim:: DifferentRotations2
:hide_source:
class DifferentRotations2(Scene):
def construct(self):
left_square = Square(color=BLUE, fill_opacity=0.7).shift(2*LEFT)
right_square = Square(color=GREEN, fill_opacity=0.7).shift(2*RIGHT)
self.play(left_square.animate.rotate(PI), Rotate(right_square, angle=PI), run_time=2)
self.wait()
This ``Scene`` illustrates the quirks of ``.animate``. When using ``.animate``, Manim
actually takes a ``Mobject``'s starting state and its ending state and interpolates the two.
In the ``AnimatedSquareToCircle`` class, you can observe this when the square rotates:
the corners of the square appear to contract slightly as they move into the positions required
for the first square to transform into the second one.
In ``DifferentRotations``, the difference between ``.animate``'s interpretation of rotation and the
``Rotate`` method is far more apparent. The starting and ending states of a ``Mobject`` rotated 180 degrees
are the same, so ``.animate`` tries to interpolate two identical objects and the result is the left square.
If you find that your own usage of ``.animate`` is causing similar unwanted behavior, consider
using conventional animation methods like the right square, which uses ``Rotate``.
``Transform`` vs ``ReplacementTransform``
*****************************************
The difference between ``Transform`` and ``ReplacementTransform`` is that ``Transform(mob1, mob2)`` transforms the points
(as well as other attributes like color) of ``mob1`` into the points/attributes of ``mob2``.
``ReplacementTransform(mob1, mob2)`` on the other hand literally replaces ``mob1`` on the scene with ``mob2``.
The use of ``ReplacementTransform`` or ``Transform`` is mostly up to personal preference. They can be used to accomplish the same effect, as shown below.
.. code-block:: python
class TwoTransforms(Scene):
def transform(self):
a = Circle()
b = Square()
c = Triangle()
self.play(Transform(a, b))
self.play(Transform(a, c))
self.play(FadeOut(a))
def replacement_transform(self):
a = Circle()
b = Square()
c = Triangle()
self.play(ReplacementTransform(a, b))
self.play(ReplacementTransform(b, c))
self.play(FadeOut(c))
def construct(self):
self.transform()
self.wait(0.5) # wait for 0.5 seconds
self.replacement_transform()
However, in some cases it is more beneficial to use ``Transform``, like when you are transforming several mobjects one after the other.
The code below avoids having to keep a reference to the last mobject that was transformed.
.. manim:: TransformCycle
class TransformCycle(Scene):
def construct(self):
a = Circle()
t1 = Square()
t2 = Triangle()
self.add(a)
self.wait()
for t in [t1,t2]:
self.play(Transform(a,t))
************
You're done!
************
With a working installation of Manim and this sample project under your belt,
you're ready to start creating animations of your own. To learn
more about what Manim is doing under the hood, move on to the next tutorial:
:doc:`output_and_config`. For an overview of
Manim's features, as well as its configuration and other settings, check out the
other :doc:`Tutorials <../tutorials/index>`. For a list of all available features, refer to the
:doc:`../reference` page.
|
manim_ManimCommunity/docs/source/tutorials/building_blocks.rst
|
#######################
Manim's building blocks
#######################
This document explains the building blocks of manim and will give you all the
necessary tools to start producing your own videos.
Essentially, manim puts at your disposal three different concepts that you can
orchestrate together to produce mathematical animations: the
**mathematical object** (or **mobject** for short), the **animation**, and the
**scene**. As we will see in the following sections, each of these three
concepts is implemented in manim as a separate class: the :class:`.Mobject`,
:class:`.Animation`, and :class:`.Scene` classes.
.. note:: It is recommended that you read the tutorials :doc:`quickstart` and
:doc:`output_and_config` before reading this page.
********
Mobjects
********
Mobjects are the basic building blocks for all manim animations. Each class
that derives from :class:`.Mobject` represents an object that can be displayed
on the screen. For example, simple shapes such as :class:`.Circle`,
:class:`.Arrow`, and :class:`.Rectangle` are all mobjects. More complicated
constructs such as :class:`.Axes`, :class:`.FunctionGraph`, or
:class:`.BarChart` are mobjects as well.
If you try to display an instance of :class:`.Mobject` on the screen, you will only
see an empty frame. The reason is that the :class:`.Mobject` class is an
abstract base class of all other mobjects, i.e. it does not have any
pre-determined visual shape that can be displayed on the screen. It is only the
skeleton of a thing that *could* be displayed. Therefore, you will rarely need
to use plain instances of :class:`.Mobject`; instead, you will most likely
create instances of its derived classes. One of these derived classes is
:class:`.VMobject`. The ``V`` stands for Vectorized Mobject. In essence, a
vmobject is a mobject that uses `vector graphics
<https://en.wikipedia.org/wiki/Vector_graphics>`_ to be displayed. Most of
the time, you will be dealing with vmobjects, though we will continue to use
the term "mobject" to refer to the class of shapes that can be displayed on the
screen, as it is more general.
.. note:: Any object that can be displayed on the screen is a ``mobject``, even if
it is not necessarily *mathematical* in nature.
.. tip:: To see examples of classes derived from :class:`.Mobject`, see the
:mod:`.geometry` module. Most of these are in fact derived from
:class:`.VMobject` as well.
Creating and displaying mobjects
================================
As explained in :doc:`quickstart`, usually all of the code in a manim
script is put inside the :meth:`.construct` method of a :class:`.Scene` class.
To display a mobject on the screen, call the :meth:`~.Scene.add` method of the
containing :class:`.Scene`. This is the principal way of displaying a mobject
on the screen when it is not being animated. To remove a mobject from the
screen, simply call the :meth:`~.Scene.remove` method from the containing
:class:`.Scene`.
.. manim:: CreatingMobjects
class CreatingMobjects(Scene):
def construct(self):
circle = Circle()
self.add(circle)
self.wait(1)
self.remove(circle)
self.wait(1)
Placing mobjects
================
Let's define a new :class:`.Scene` called ``Shapes`` and :meth:`~.Scene.add`
some mobjects to it. This script generates a static picture that displays a
circle, a square, and a triangle:
.. manim:: Shapes
class Shapes(Scene):
def construct(self):
circle = Circle()
square = Square()
triangle = Triangle()
circle.shift(LEFT)
square.shift(UP)
triangle.shift(RIGHT)
self.add(circle, square, triangle)
self.wait(1)
By default, mobjects are placed at the center of coordinates, or *origin*, when
they are first created. They are also given some default colors. Further, the
``Shapes`` scene places the mobjects by using the :meth:`.shift` method. The
square is shifted one unit in the ``UP`` direction from the origin, while the
circle and triangle are shifted one unit ``LEFT`` and ``RIGHT``, respectively.
.. attention:: Unlike other graphics software, manim places the center of
coordinates at the center of the screen. The positive vertical
direction is up, and the positive horizontal direction is right.
See also the constants ``ORIGIN``, ``UP``, ``DOWN``, ``LEFT``,
``RIGHT``, and others, defined in the :mod:`.constants` module.
There are many other possible ways to place mobjects on the screen, for example
:meth:`.move_to`, :meth:`.next_to`, and :meth:`.align_to`. The next scene
``MobjectPlacement`` uses all three.
.. manim:: MobjectPlacement
class MobjectPlacement(Scene):
def construct(self):
circle = Circle()
square = Square()
triangle = Triangle()
# place the circle two units left from the origin
circle.move_to(LEFT * 2)
# place the square to the left of the circle
square.next_to(circle, LEFT)
# align the left border of the triangle to the left border of the circle
triangle.align_to(circle, LEFT)
self.add(circle, square, triangle)
self.wait(1)
The :meth:`.move_to` method uses absolute units (measured relative to the
``ORIGIN``), while :meth:`.next_to` uses relative units (measured from the
mobject passed as the first argument). :meth:`align_to` uses ``LEFT`` not as
measuring units but as a way to determine the border to use for alignment. The
coordinates of the borders of a mobject are determined using an imaginary
bounding box around it.
.. tip:: Many methods in manim can be chained together. For example the two
lines
.. code-block:: python
square = Square()
square.shift(LEFT)
can be replaced by
.. code-block:: python
square = Square().shift(LEFT)
Technically, this is possible because most methods calls return the modified mobject.
Styling mobjects
================
The following scene changes the default aesthetics of the mobjects.
.. manim:: MobjectStyling
class MobjectStyling(Scene):
def construct(self):
circle = Circle().shift(LEFT)
square = Square().shift(UP)
triangle = Triangle().shift(RIGHT)
circle.set_stroke(color=GREEN, width=20)
square.set_fill(YELLOW, opacity=1.0)
triangle.set_fill(PINK, opacity=0.5)
self.add(circle, square, triangle)
self.wait(1)
This scene uses two of the main functions that change the visual style of a
mobject: :meth:`.set_stroke` and :meth:`.set_fill`. The former changes the
visual style of the mobject's border while the latter changes the style of the
interior. By default, most mobjects have a fully transparent interior so you
must specify the ``opacity`` parameter to display the color. An
opacity of ``1.0`` means fully opaque, while ``0.0`` means fully transparent.
Only instances of :class:`.VMobject` implement :meth:`.set_stroke` and
:meth:`.set_fill`. Instances of :class:`.Mobject` implement
:meth:`.~Mobject.set_color` instead. The vast majority of pre-defined classes
are derived from :class:`.VMobject` so it is usually safe to assume that you
have access to :meth:`.set_stroke` and :meth:`.set_fill`.
Mobject on-screen order
=======================
The next scene is exactly the same as the ``MobjectStyling`` scene from the
previous section, except for exactly one line.
.. manim:: MobjectZOrder
class MobjectZOrder(Scene):
def construct(self):
circle = Circle().shift(LEFT)
square = Square().shift(UP)
triangle = Triangle().shift(RIGHT)
circle.set_stroke(color=GREEN, width=20)
square.set_fill(YELLOW, opacity=1.0)
triangle.set_fill(PINK, opacity=0.5)
self.add(triangle, square, circle)
self.wait(1)
The only difference here (besides the scene name) is the order in which the
mobjects are added to the scene. In ``MobjectStyling``, we added them as
``add(circle, square, triangle)``, whereas in ``MobjectZOrder`` we add them as
``add(triangle, square, circle)``.
As you can see, the order of the arguments of :meth:`~.Scene.add` determines
the order that the mobjects are displayed on the screen, with the left-most
arguments being put in the back.
**********
Animations
**********
At the heart of manim is animation. Generally, you can add an animation to
your scene by calling the :meth:`~.Scene.play` method.
.. manim:: SomeAnimations
class SomeAnimations(Scene):
def construct(self):
square = Square()
# some animations display mobjects, ...
self.play(FadeIn(square))
# ... some move or rotate mobjects around...
self.play(Rotate(square, PI/4))
# some animations remove mobjects from the screen
self.play(FadeOut(square))
self.wait(1)
Put simply, animations are procedures that interpolate between two mobjects.
For example, :code:`FadeIn(square)` starts with a fully transparent version of
:code:`square` and ends with a fully opaque version, interpolating between them
by gradually increasing the opacity. :class:`.FadeOut` works in the opposite
way: it interpolates from fully opaque to fully transparent. As another
example, :class:`.Rotate` starts with the mobject passed to it as argument, and
ends with the same object but rotated by a certain amount, this time
interpolating the mobject's angle instead of its opacity.
Animating methods
=================
Any property of a mobject that can be changed can be animated. In fact, any
method that changes a mobject's property can be used as an animation, through
the use of :meth:`.animate`.
.. manim:: AnimateExample
:ref_classes: Animation
class AnimateExample(Scene):
def construct(self):
square = Square().set_fill(RED, opacity=1.0)
self.add(square)
# animate the change of color
self.play(square.animate.set_fill(WHITE))
self.wait(1)
# animate the change of position and the rotation at the same time
self.play(square.animate.shift(UP).rotate(PI / 3))
self.wait(1)
:meth:`.animate` is a property of all mobjects that animates the methods that come
afterward. For example, :code:`square.set_fill(WHITE)` sets the fill color of
the square, while :code:`square.animate.set_fill(WHITE)` animates this action.
Animation run time
==================
By default, any animation passed to :meth:`play` lasts for exactly one second.
Use the :code:`run_time` argument to control the duration.
.. manim:: RunTime
class RunTime(Scene):
def construct(self):
square = Square()
self.add(square)
self.play(square.animate.shift(UP), run_time=3)
self.wait(1)
Creating a custom animation
===========================
Even though Manim has many built-in animations, you will find times when you need to smoothly animate from one state of a :class:`~.Mobject` to another.
If you find yourself in that situation, then you can define your own custom animation.
You start by extending the :class:`~.Animation` class and overriding its :meth:`~.Animation.interpolate_mobject`.
The :meth:`~.Animation.interpolate_mobject` method receives alpha as a parameter that starts at 0 and changes throughout the animation.
So, you just have to manipulate self.mobject inside Animation according to the alpha value in its interpolate_mobject method.
Then you get all the benefits of :class:`~.Animation` such as playing it for different run times or using different rate functions.
Let's say you start with a number and want to create a :class:`~.Transform` animation that transforms it to a target number.
You can do it using :class:`~.FadeTransform`, which will fade out the starting number and fade in the target number.
But when we think about transforming a number from one to another, an intuitive way of doing it is by incrementing or decrementing it smoothly.
Manim has a feature that allows you to customize this behavior by defining your own custom animation.
You can start by creating your own ``Count`` class that extends :class:`~.Animation`.
The class can have a constructor with three arguments, a :class:`~.DecimalNumber` Mobject, start, and end.
The constructor will pass the :class:`~.DecimalNumber` Mobject to the super constructor (in this case, the :class:`~.Animation` constructor) and will set start and end.
The only thing that you need to do is to define how you want it to look at every step of the animation.
Manim provides you with the alpha value in the :meth:`~.Animation.interpolate_mobject` method based on frame rate of video, rate function, and run time of animation played.
The alpha parameter holds a value between 0 and 1 representing the step of the currently playing animation.
For example, 0 means the beginning of the animation, 0.5 means halfway through the animation, and 1 means the end of the animation.
In the case of the ``Count`` animation, you just have to figure out a way to determine the number to display at the given alpha value and then set that value in the :meth:`~.Animation.interpolate_mobject` method of the ``Count`` animation.
Suppose you are starting at 50 and incrementing until the :class:`~.DecimalNumber` reaches 100 at the end of the animation.
* If alpha is 0, you want the value to be 50.
* If alpha is 0.5, you want the value to be 75.
* If alpha is 1, you want the value to be 100.
Generally, you start with the starting number and add only some part of the value to be increment according to the alpha value.
So, the logic of calculating the number to display at each step will be ``50 + alpha * (100 - 50)``.
Once you set the calculated value for the :class:`~.DecimalNumber`, you are done.
Once you have defined your ``Count`` animation, you can play it in your :class:`~.Scene` for any duration you want for any :class:`~.DecimalNumber` with any rate function.
.. manim:: CountingScene
:ref_classes: Animation DecimalNumber
:ref_methods: Animation.interpolate_mobject Scene.play
class Count(Animation):
def __init__(self, number: DecimalNumber, start: float, end: float, **kwargs) -> None:
# Pass number as the mobject of the animation
super().__init__(number, **kwargs)
# Set start and end
self.start = start
self.end = end
def interpolate_mobject(self, alpha: float) -> None:
# Set value of DecimalNumber according to alpha
value = self.start + (alpha * (self.end - self.start))
self.mobject.set_value(value)
class CountingScene(Scene):
def construct(self):
# Create Decimal Number and add it to scene
number = DecimalNumber().set_color(WHITE).scale(5)
# Add an updater to keep the DecimalNumber centered as its value changes
number.add_updater(lambda number: number.move_to(ORIGIN))
self.add(number)
self.wait()
# Play the Count Animation to count from 0 to 100 in 4 seconds
self.play(Count(number, 0, 100), run_time=4, rate_func=linear)
self.wait()
Using coordinates of a mobject
==============================
Mobjects contain points that define their boundaries.
These points can be used to add other mobjects respectively to each other,
e.g. by methods like :meth:`~.Mobject.get_center` , :meth:`~.Mobject.get_top`
and :meth:`~.Mobject.get_start`. Here is an example of some important coordinates:
.. manim:: MobjectExample
:save_last_frame:
class MobjectExample(Scene):
def construct(self):
p1 = [-1,-1, 0]
p2 = [ 1,-1, 0]
p3 = [ 1, 1, 0]
p4 = [-1, 1, 0]
a = Line(p1,p2).append_points(Line(p2,p3).points).append_points(Line(p3,p4).points)
point_start = a.get_start()
point_end = a.get_end()
point_center = a.get_center()
self.add(Text(f"a.get_start() = {np.round(point_start,2).tolist()}", font_size=24).to_edge(UR).set_color(YELLOW))
self.add(Text(f"a.get_end() = {np.round(point_end,2).tolist()}", font_size=24).next_to(self.mobjects[-1],DOWN).set_color(RED))
self.add(Text(f"a.get_center() = {np.round(point_center,2).tolist()}", font_size=24).next_to(self.mobjects[-1],DOWN).set_color(BLUE))
self.add(Dot(a.get_start()).set_color(YELLOW).scale(2))
self.add(Dot(a.get_end()).set_color(RED).scale(2))
self.add(Dot(a.get_top()).set_color(GREEN_A).scale(2))
self.add(Dot(a.get_bottom()).set_color(GREEN_D).scale(2))
self.add(Dot(a.get_center()).set_color(BLUE).scale(2))
self.add(Dot(a.point_from_proportion(0.5)).set_color(ORANGE).scale(2))
self.add(*[Dot(x) for x in a.points])
self.add(a)
Transforming mobjects into other mobjects
=========================================
It is also possible to transform a mobject into another mobject like this:
.. manim:: ExampleTransform
class ExampleTransform(Scene):
def construct(self):
self.camera.background_color = WHITE
m1 = Square().set_color(RED)
m2 = Rectangle().set_color(RED).rotate(0.2)
self.play(Transform(m1,m2))
The Transform function maps points of the previous mobject to the points of the
next mobject.
This might result in strange behaviour, e.g. when the dots of one mobject are
arranged clockwise and the other points are arranged counterclockwise.
Here it might help to use the `flip` function and reposition the points via the
`roll <https://numpy.org/doc/stable/reference/generated/numpy.roll.html>`_
function of numpy:
.. manim:: ExampleRotation
class ExampleRotation(Scene):
def construct(self):
self.camera.background_color = WHITE
m1a = Square().set_color(RED).shift(LEFT)
m1b = Circle().set_color(RED).shift(LEFT)
m2a = Square().set_color(BLUE).shift(RIGHT)
m2b = Circle().set_color(BLUE).shift(RIGHT)
points = m2a.points
points = np.roll(points, int(len(points)/4), axis=0)
m2a.points = points
self.play(Transform(m1a,m1b),Transform(m2a,m2b), run_time=1)
******
Scenes
******
The :class:`.Scene` class is the connective tissue of manim. Every mobject has
to be :meth:`added <.Scene.add>` to a scene to be displayed, or :meth:`removed
<.Scene.remove>` from it to cease being displayed. Every animation has to be
:meth:`played <.Scene.play>` by a scene, and every time interval where no
animation occurs is determined by a call to :meth:`~.Scene.wait`. All of the
code of your video must be contained in the :meth:`~.Scene.construct` method of
a class that derives from :class:`.Scene`. Finally, a single file may contain
multiple :class:`.Scene` subclasses if multiple scenes are to be
rendered at the same time.
|
manim_ManimCommunity/docs/source/tutorials/index.rst
|
Tutorials
=========
.. toctree::
:caption: Table of Contents
:maxdepth: 2
quickstart
output_and_config
building_blocks
|
manim_ManimCommunity/docs/source/tutorials/output_and_config.rst
|
Manim's Output Settings
=======================
This document will focus on understanding manim's output files and some of the
main command-line flags available.
.. note:: This tutorial picks up where :doc:`quickstart` left off, so please
read that document before starting this one.
Manim output folders
********************
At this point, you have just executed the following command.
.. code-block:: bash
manim -pql scene.py SquareToCircle
Let's dissect what just happened step by step. First, this command executes
manim on the file ``scene.py``, which contains our animation code. Further,
this command tells manim exactly which ``Scene`` is to be rendered, in this case,
it is ``SquareToCircle``. This is necessary because a single scene file may
contain more than one scene. Next, the flag `-p` tells manim to play the scene
once it's rendered, and the `-ql` flag tells manim to render the scene in low
quality.
After the video is rendered, you will see that manim has generated some new
files and the project folder will look as follows.
.. code-block:: bash
project/
├─scene.py
└─media
├─videos
| └─scene
| └─480p15
| ├─SquareToCircle.mp4
| └─partial_movie_files
├─text
└─Tex
There are quite a few new files. The main output is in
``media/videos/scene/480p15/SquareToCircle.mp4``. By default, the ``media``
folder will contain all of manim's output files. The ``media/videos``
subfolder contains the rendered videos. Inside of it, you will find one folder
for each different video quality. In our case, since we used the ``-l`` flag,
the video was generated at 480 resolution at 15 frames per second from the
``scene.py`` file. Therefore, the output can be found inside
``media/videos/scene/480p15``. The additional folders
``media/videos/scene/480p15/partial_movie_files`` as well as ``media/text`` and
``media/Tex`` contain files that are used by manim internally.
You can see how manim makes use of the generated folder structure by executing
the following command,
.. code-block:: bash
manim -pqh scene.py SquareToCircle
The ``-ql`` flag (for low quality) has been replaced by the ``-qh`` flag, for
high quality. Manim will take considerably longer to render this file, and it
will play it once it's done since we are using the ``-p`` flag. The output
should look like this:
.. manim:: SquareToCircle3
:hide_source:
:quality: high
class SquareToCircle3(Scene):
def construct(self):
circle = Circle() # create a circle
circle.set_fill(PINK, opacity=0.5) # set color and transparency
square = Square() # create a square
square.flip(RIGHT) # flip horizontally
square.rotate(-3 * TAU / 8) # rotate a certain amount
self.play(Create(square)) # animate the creation of the square
self.play(Transform(square, circle)) # interpolate the square into the circle
self.play(FadeOut(square)) # fade out animation
And the folder structure should look as follows.
.. code-block:: bash
project/
├─scene.py
└─media
├─videos
| └─scene
| ├─480p15
| | ├─SquareToCircle.mp4
| | └─partial_movie_files
| └─1080p60
| ├─SquareToCircle.mp4
| └─partial_movie_files
├─text
└─Tex
Manim has created a new folder ``media/videos/1080p60``, which corresponds to
the high resolution and the 60 frames per second. Inside of it, you can find
the new ``SquareToCircle.mp4``, as well as the corresponding
``partial_movie_files``.
When working on a project with multiple scenes, and trying out multiple
resolutions, the structure of the output directories will keep all your videos
organized.
Further, manim has the option to output the last frame of a scene, when adding
the flag ``-s``. This is the fastest option to quickly get a preview of a scene.
The corresponding folder structure looks like this:
.. code-block:: bash
project/
├─scene.py
└─media
├─images
| └─scene
| ├─SquareToCircle.png
├─videos
| └─scene
| ├─480p15
| | ├─SquareToCircle.mp4
| | └─partial_movie_files
| └─1080p60
| ├─SquareToCircle.mp4
| └─partial_movie_files
├─text
└─Tex
Saving the last frame with ``-s`` can be combined with the flags for different
resolutions, e.g. ``-s -ql``, ``-s -qh``
Sections
********
In addition to the movie output file one can use sections. Each section produces
its own output video. The cuts between two sections can be set like this:
.. code-block:: python
def construct(self):
# play the first animations...
# you don't need a section in the very beginning as it gets created automatically
self.next_section()
# play more animations...
self.next_section("this is an optional name that doesn't have to be unique")
# play even more animations...
self.next_section("this is a section without any animations, it will be removed")
All the animations between two of these cuts get concatenated into a single output
video file.
Be aware that you need at least one animation in each section. For example this wouldn't create an output video:
.. code-block:: python
def construct(self):
self.next_section()
# this section doesn't have any animations and will be removed
# but no error will be thrown
# feel free to tend your flock of empty sections if you so desire
self.add(Circle())
self.next_section()
One way of fixing this is to wait a little:
.. code-block:: python
def construct(self):
self.next_section()
self.add(Circle())
# now we wait 1sec and have an animation to satisfy the section
self.wait()
self.next_section()
For videos to be created for each section you have to add the ``--save_sections`` flag to the Manim call like this:
.. code-block:: bash
manim --save_sections scene.py
If you do this, the ``media`` folder will look like this:
.. code-block:: bash
media
├── images
│ └── simple_scenes
└── videos
└── simple_scenes
└── 480p15
├── ElaborateSceneWithSections.mp4
├── partial_movie_files
│ └── ElaborateSceneWithSections
│ ├── 2201830969_104169243_1331664314.mp4
│ ├── 2201830969_398514950_125983425.mp4
│ ├── 2201830969_398514950_3447021159.mp4
│ ├── 2201830969_398514950_4144009089.mp4
│ ├── 2201830969_4218360830_1789939690.mp4
│ ├── 3163782288_524160878_1793580042.mp4
│ └── partial_movie_file_list.txt
└── sections
├── ElaborateSceneWithSections_0000.mp4
├── ElaborateSceneWithSections_0001.mp4
├── ElaborateSceneWithSections_0002.mp4
└── ElaborateSceneWithSections.json
As you can see each section receives their own output video in the ``sections`` directory.
The JSON file in here contains some useful information for each section:
.. code-block:: json
[
{
"name": "create square",
"type": "default.normal",
"video": "ElaborateSceneWithSections_0000.mp4",
"codec_name": "h264",
"width": 854,
"height": 480,
"avg_frame_rate": "15/1",
"duration": "2.000000",
"nb_frames": "30"
},
{
"name": "transform to circle",
"type": "default.normal",
"video": "ElaborateSceneWithSections_0001.mp4",
"codec_name": "h264",
"width": 854,
"height": 480,
"avg_frame_rate": "15/1",
"duration": "2.000000",
"nb_frames": "30"
},
{
"name": "fade out",
"type": "default.normal",
"video": "ElaborateSceneWithSections_0002.mp4",
"codec_name": "h264",
"width": 854,
"height": 480,
"avg_frame_rate": "15/1",
"duration": "2.000000",
"nb_frames": "30"
}
]
This data can be used by third party applications, like a presentation system or automated video editing tool.
You can also skip rendering all animations belonging to a section like this:
.. code-block:: python
def construct(self):
self.next_section(skip_animations=True)
# play some animations that shall be skipped...
self.next_section()
# play some animations that won't get skipped...
Some command line flags
***********************
When executing the command
.. code-block:: bash
manim -pql scene.py SquareToCircle
it was necessary to specify which ``Scene`` class to render. This is because a
single file can contain more than one ``Scene`` class. If your file contains
multiple ``Scene`` classes, and you want to render them all, you can use the
``-a`` flag.
As discussed previously, the ``-ql`` specifies low render quality. This does
not look very good, but is very useful for rapid prototyping and testing. The
other options that specify render quality are ``-qm``, ``-qh``, and ``-qk`` for
medium, high, and 4k quality, respectively.
The ``-p`` flag plays the animation once it is rendered. If you want to open
the file browser at the location of the animation instead of playing it, you
can use the ``-f`` flag. You can also omit these two flags.
Finally, by default manim will output .mp4 files. If you want your animations
in .gif format instead, use the ``-i`` flag. The output files will be in the
same folder as the .mp4 files, and with the same name, but a different file
extension.
This was a quick review of some of the most frequent command-line flags. For a
thorough review of all flags available, see the
:doc:`thematic guide on Manim's configuration system </guides/configuration>`.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.