mirror of
https://github.com/3b1b/manim.git
synced 2025-09-01 00:48:45 +00:00
Merge pull request #580 from Kolloom/pull_jarwin_patch-3
Update learning_by_example.rst
This commit is contained in:
commit
22b4c3578b
2 changed files with 101 additions and 96 deletions
|
@ -1,13 +1,19 @@
|
||||||
Learning by Example
|
Learning by Example
|
||||||
===================
|
===================
|
||||||
|
|
||||||
You create videos in manim by writing :class:`~scene.scene.Scene` instances.
|
SquareToCircle
|
||||||
``example_scenes.py`` contains a few simple ones that we can use to learn about
|
--------------
|
||||||
manim. For instance, take ``SquareToCircle``.
|
|
||||||
|
``example_scenes.py`` contains simple examples that we can use to learn about manim.
|
||||||
|
|
||||||
|
Go ahead and try out the ``SquareToCircle`` scene by running it with ``$ manim example_scenes.py SquareToCircle -p``
|
||||||
|
in manim directory.
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
|
from manimlib.imports import *
|
||||||
|
|
||||||
class SquareToCircle(Scene):
|
class SquareToCircle(Scene):
|
||||||
def construct(self):
|
def construct(self):
|
||||||
circle = Circle()
|
circle = Circle()
|
||||||
|
@ -20,115 +26,106 @@ manim. For instance, take ``SquareToCircle``.
|
||||||
self.play(Transform(square, circle))
|
self.play(Transform(square, circle))
|
||||||
self.play(FadeOut(square))
|
self.play(FadeOut(square))
|
||||||
|
|
||||||
:meth:`~scene.scene.Scene.construct` specifies what is displayed on the screen
|
|
||||||
when the :class:`~scene.scene.Scene` is rendered to video. You can render a
|
|
||||||
:class:`~scene.scene.Scene` by running ``extract_scene.py``. Run ``python
|
|
||||||
extract_scene.py -h`` to see how it's used.
|
|
||||||
|
|
||||||
.. code-block:: none
|
|
||||||
|
|
||||||
> python extract_scene.py -h
|
|
||||||
usage: extract_scene.py [-h] [-p] [-w] [-s] [-l] [-m] [-g] [-f] [-t] [-q] [-a]
|
|
||||||
[-o OUTPUT_NAME] [-n START_AT_ANIMATION_NUMBER]
|
|
||||||
[-r RESOLUTION] [-c COLOR] [-d OUTPUT_DIRECTORY]
|
|
||||||
file [scene_name]
|
|
||||||
|
|
||||||
positional arguments:
|
|
||||||
file path to file holding the python code for the scene
|
|
||||||
scene_name Name of the Scene class you want to see
|
|
||||||
|
|
||||||
optional arguments:
|
|
||||||
-h, --help show this help message and exit
|
|
||||||
-p, --preview
|
|
||||||
-w, --write_to_movie
|
|
||||||
-s, --show_last_frame
|
|
||||||
-l, --low_quality
|
|
||||||
-m, --medium_quality
|
|
||||||
-g, --save_pngs
|
|
||||||
-f, --show_file_in_finder
|
|
||||||
-t, --transparent
|
|
||||||
-q, --quiet
|
|
||||||
-a, --write_all
|
|
||||||
-o OUTPUT_NAME, --output_name OUTPUT_NAME
|
|
||||||
-n START_AT_ANIMATION_NUMBER, --start_at_animation_number START_AT_ANIMATION_NUMBER
|
|
||||||
-r RESOLUTION, --resolution RESOLUTION
|
|
||||||
-c COLOR, --color COLOR
|
|
||||||
-d OUTPUT_DIRECTORY, --output_directory OUTPUT_DIRECTORY
|
|
||||||
|
|
||||||
The most common flags are ``-p``, to automatically play the generated video,
|
|
||||||
``-l``, to render in lower quality in favor of speed, and ``-s``, to show the
|
|
||||||
last frame of the :class:`~scene.scene.Scene` for faster development. Run
|
|
||||||
``python extract_scene.py example_scenes.py SquareToCircle -pl`` to produce a
|
|
||||||
file called SquareToCircle.mp4 in the media directory that you have configured,
|
|
||||||
and automatically play it.
|
|
||||||
|
|
||||||
.. raw:: html
|
.. raw:: html
|
||||||
|
|
||||||
<video width="560" height="315" controls>
|
<video width="560" height="315" controls>
|
||||||
<source src="../_static/SquareToCircle.mp4" type="video/mp4">
|
<source src="../_static/SquareToCircle.mp4" type="video/mp4">
|
||||||
</video>
|
</video>
|
||||||
|
|
||||||
Let's step through each line of the :class:`~scene.scene.Scene`. Lines 3 and 4
|
|
||||||
instantiate a :class:`~mobject.geometry.Circle` and
|
.. note::
|
||||||
:class:`~mobject.geometry.Square`, respectively. Both of these subclass
|
|
||||||
:class:`~mobject.mobject.Mobject`, the base class for objects in manim. Note
|
The flag ``-p`` plays the rendered video with default video player.
|
||||||
|
|
||||||
|
Other frequently used flags are:
|
||||||
|
|
||||||
|
* ``-l`` for rendering video in lower resolution (which renders faster)
|
||||||
|
* ``-s`` to show the last frame of the video.
|
||||||
|
|
||||||
|
Run ``manim -h`` all the available flags (``python -m manim -h`` if you installed it to a venv)
|
||||||
|
|
||||||
|
|
||||||
|
Let's step through each line of ``SquareToCircle``
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
:lineno-start: 3
|
||||||
|
|
||||||
|
class SquareToCircle(Scene):
|
||||||
|
|
||||||
|
You create videos in manim by writing :class:`~scene.scene.Scene` classes.
|
||||||
|
|
||||||
|
Each :class:`~scene.scene.Scene` in manim is self-contained. That means everything
|
||||||
|
you created under this scene does not exist outside the class.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
:lineno-start: 4
|
||||||
|
|
||||||
|
def construct(self):
|
||||||
|
|
||||||
|
:meth:`~scene.scene.Scene.construct` specifies what is displayed on the screen
|
||||||
|
when the :class:`~scene.scene.Scene` is rendered to video.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
:lineno-start: 5
|
||||||
|
|
||||||
|
circle = Circle()
|
||||||
|
square = Square()
|
||||||
|
|
||||||
|
``Circle()`` and ``Square()`` create :class:`~mobject.geometry.Circle` and :class:`~mobject.geometry.Square`.
|
||||||
|
|
||||||
|
Both of these are instances of :class:`~mobject.mobject.Mobject` subclasses, the base class for objects in manim. Note
|
||||||
that instantiating a :class:`~mobject.mobject.Mobject` does not add it to the
|
that instantiating a :class:`~mobject.mobject.Mobject` does not add it to the
|
||||||
:class:`~scene.scene.Scene`, so you wouldn't see anything if you were to render
|
:class:`~scene.scene.Scene`, so you wouldn't see anything if you were to render
|
||||||
the :class:`~scene.scene.Scene` at this point.
|
the :class:`~scene.scene.Scene` at this point.
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
:linenos:
|
:lineno-start: 7
|
||||||
:lineno-start: 3
|
|
||||||
|
|
||||||
circle = Circle()
|
|
||||||
square = Square()
|
|
||||||
|
|
||||||
Lines 5, 6, and 7 apply various modifications to the mobjects before animating
|
|
||||||
them. The call to :meth:`~mobject.mobject.Mobject.flip` on line 5 flips the
|
|
||||||
:class:`~mobject.geometry.Square` across the RIGHT vector. This is equivalent
|
|
||||||
to a refection across the x-axis. Then the call to
|
|
||||||
:meth:`~mobject.mobject.Mobject.rotate` on line 6 rotates the
|
|
||||||
:class:`~mobject.geometry.Square` 3/8ths of a full rotation counterclockwise.
|
|
||||||
Finally, the call to :meth:`~mobject.mobject.Mobject.set_fill` on line 7 sets
|
|
||||||
the fill color for the :class:`~mobject.geometry.Circle` to pink, and its
|
|
||||||
opacity to 0.5.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
:linenos:
|
|
||||||
:lineno-start: 5
|
|
||||||
|
|
||||||
square.flip(RIGHT)
|
square.flip(RIGHT)
|
||||||
square.rotate(-3 * TAU / 8)
|
square.rotate(-3 * TAU / 8)
|
||||||
circle.set_fill(PINK, opacity=0.5)
|
circle.set_fill(PINK, opacity=0.5)
|
||||||
|
|
||||||
Line 9 is the first to generate video.
|
``flip()`` ``rotate()`` ``set_fill()`` apply various modifications to the mobjects before animating
|
||||||
:class:`~animation.creation.ShowCreation`,
|
them. The call to :meth:`~mobject.mobject.Mobject.flip` flips the
|
||||||
:class:`~animation.transform.Transform`, and
|
:class:`~mobject.geometry.Square` across the RIGHT vector. This is equivalent
|
||||||
:class:`~animation.creation.FadeOut` are
|
to a refection across the x-axis.
|
||||||
:class:`~animation.animation.Animation` instances. Each
|
|
||||||
:class:`~animation.animation.Animation` takes one or more
|
The call to :meth:`~mobject.mobject.Mobject.rotate` rotates the
|
||||||
:class:`~mobject.mobject.Mobject` instances as arguments, which it animates
|
:class:`~mobject.geometry.Square` 3/8ths of a full rotation counterclockwise.
|
||||||
when passed to :meth:`~scene.scene.Scene.play`. This is how video is typically
|
|
||||||
created in manim. :class:`~mobject.mobject.Mobject` instances are automatically
|
The call to :meth:`~mobject.mobject.Mobject.set_fill` sets
|
||||||
added to the :class:`~scene.scene.Scene` when they are animated. You can add a
|
the fill color for the :class:`~mobject.geometry.Circle` to pink, and its opacity to 0.5.
|
||||||
:class:`~mobject.mobject.Mobject` to the :class:`~scene.scene.Scene` manually
|
|
||||||
by passing it as an argument to :meth:`~scene.scene.Scene.add`.
|
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
:linenos:
|
:lineno-start: 11
|
||||||
:lineno-start: 9
|
|
||||||
|
|
||||||
self.play(ShowCreation(square))
|
self.play(ShowCreation(square))
|
||||||
self.play(Transform(square, circle))
|
self.play(Transform(square, circle))
|
||||||
self.play(FadeOut(square))
|
self.play(FadeOut(square))
|
||||||
|
|
||||||
:class:`~animation.creation.ShowCreation` draws a
|
To generated animation, :class:`~animation.animation.Animation` classes are used.
|
||||||
:class:`~mobject.mobject.Mobject` to the screen,
|
|
||||||
:class:`~animation.transform.Transform` morphs one
|
Each :class:`~animation.animation.Animation` takes one or more :class:`~mobject.mobject.Mobject` instances as arguments, which it animates
|
||||||
:class:`~mobject.mobject.Mobject` into another, and
|
when passed to :meth:`~scene.scene.Scene.play`. This is how video is typically
|
||||||
:class:`~animation.creation.FadeOut` fades a
|
created in manim.
|
||||||
:class:`~mobject.mobject.Mobject` out of the :class:`~scene.scene.Scene`. Note
|
|
||||||
that only the first argument to :class:`~animation.transform.Transform` is
|
:class:`~mobject.mobject.Mobject` instances are automatically
|
||||||
modified, and the second is not added to the :class:`~scene.scene.Scene`. After
|
added to the :class:`~scene.scene.Scene` when they are animated. You can add a
|
||||||
line 10 is executed ``square`` is a :class:`~mobject.geometry.Square` instance
|
:class:`~mobject.mobject.Mobject` to the :class:`~scene.scene.Scene` manually
|
||||||
with the shape of a :class:`~mobject.geometry.Circle`.
|
by passing it as an argument to :meth:`~scene.scene.Scene.add`.
|
||||||
|
|
||||||
|
|
||||||
|
:class:`~animation.creation.ShowCreation` draws a :class:`~mobject.mobject.Mobject` to the screen.
|
||||||
|
|
||||||
|
:class:`~animation.transform.Transform` morphs one :class:`~mobject.mobject.Mobject` into another.
|
||||||
|
|
||||||
|
:class:`~animation.creation.FadeOut` fades a :class:`~mobject.mobject.Mobject` out of the :class:`~scene.scene.Scene`.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
Only the first argument to :class:`~animation.transform.Transform` is modified,
|
||||||
|
the second is not added to the :class:`~scene.scene.Scene`. :class:`~animation.tranform.Transform`
|
||||||
|
only changes the appearance but not the underlying properties.
|
||||||
|
|
||||||
|
After the call to ``transform()`` ``square`` is still a :class:`~mobject.geometry.Square` instance
|
||||||
|
but with the shape of :class:`~mobject.geometry.Circle`.
|
||||||
|
|
|
@ -1,4 +1,12 @@
|
||||||
Mac
|
Mac
|
||||||
===
|
===
|
||||||
|
|
||||||
A stub for mac installation
|
The simplest way to install the system dependencies on Mac OS X is with Homebrew.
|
||||||
|
Mac come preinstalled with python2, but to use manim, python3 is required
|
||||||
|
|
||||||
|
1. Install python3 https://docs.python.org/3/using/mac.html
|
||||||
|
2. Install Cairo: ``brew install cairo``
|
||||||
|
3. Install Sox: ``brew install sox``
|
||||||
|
4. Install ffmpeg: ``brew install ffmpeg``
|
||||||
|
5. Install latex (MiKTeX): https://miktex.org/howto/install-miktex-mac
|
||||||
|
6. Install manimlib ``pip install manimlib`` (or ``pip install --user manimlib`` to just yourself)
|
||||||
|
|
Loading…
Add table
Reference in a new issue