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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"id": "substantial-impact",
|
6 |
+
"metadata": {},
|
7 |
+
"source": [
|
8 |
+
"# Welcome to Manim!"
|
9 |
+
]
|
10 |
+
},
|
11 |
+
{
|
12 |
+
"cell_type": "markdown",
|
13 |
+
"id": "first-armenia",
|
14 |
+
"metadata": {},
|
15 |
+
"source": [
|
16 |
+
"This is a temporary test environment in which you can play around with Manim without the need of installing it locally. Some basic knowledge of Python is helpful! Keep in mind that this is a *temporary* environment, though: your changes will not be saved and cannot be shared with others. To save your work, you will need to download the notebook file (\"File > Download as > Notebook (.ipynb)\"). Enjoy!\n",
|
17 |
+
"\n",
|
18 |
+
"> *Useful resources:* [Documentation](https://docs.manim.community), [Discord](https://discord.gg/mMRrZQW), [Reddit](https://www.reddit.com/r/manim/)"
|
19 |
+
]
|
20 |
+
},
|
21 |
+
{
|
22 |
+
"cell_type": "markdown",
|
23 |
+
"id": "honest-cruise",
|
24 |
+
"metadata": {},
|
25 |
+
"source": [
|
26 |
+
"## Setup"
|
27 |
+
]
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"cell_type": "markdown",
|
31 |
+
"id": "governing-increase",
|
32 |
+
"metadata": {},
|
33 |
+
"source": [
|
34 |
+
"We begin our short walkthrough by importing everything from the library. Run the following code cell to do so (focus the cell and hit the *Run* button above, or press `Shift`+`Enter` – you can find more information about how to navigate and work with Jupyter notebooks in the *Help* menu at the top of this page).\n",
|
35 |
+
"\n",
|
36 |
+
"The second line controls the maximum width used to display videos in this notebook, and the third line controls the verbosity of the log output. Feel free to adapt both of these settings to your liking."
|
37 |
+
]
|
38 |
+
},
|
39 |
+
{
|
40 |
+
"cell_type": "code",
|
41 |
+
"execution_count": null,
|
42 |
+
"id": "wound-foundation",
|
43 |
+
"metadata": {},
|
44 |
+
"outputs": [],
|
45 |
+
"source": [
|
46 |
+
"from manim import *\n",
|
47 |
+
"\n",
|
48 |
+
"config.media_width = \"75%\"\n",
|
49 |
+
"config.verbosity = \"WARNING\""
|
50 |
+
]
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"cell_type": "markdown",
|
54 |
+
"id": "respected-copying",
|
55 |
+
"metadata": {},
|
56 |
+
"source": [
|
57 |
+
"If you have executed the cell successfully, a message printing the installed version of the library should have appeared below it."
|
58 |
+
]
|
59 |
+
},
|
60 |
+
{
|
61 |
+
"cell_type": "markdown",
|
62 |
+
"id": "antique-knock",
|
63 |
+
"metadata": {},
|
64 |
+
"source": [
|
65 |
+
"## Your first Scene"
|
66 |
+
]
|
67 |
+
},
|
68 |
+
{
|
69 |
+
"cell_type": "markdown",
|
70 |
+
"id": "drawn-dylan",
|
71 |
+
"metadata": {},
|
72 |
+
"source": [
|
73 |
+
"Manim generates videos by rendering *Scenes*. These are special classes that have a `construct` method describing the animations that should be rendered. (For the sake of this tutorial it doesn't matter if you are not that familiar with Python or object-oriented programming terminology like *class* or *method* – but you should consider working through a Python tutorial if you want to keep learning Manim.)\n",
|
74 |
+
"\n",
|
75 |
+
"Enough of fancy words, let us look at an example. Run the cell below to render and display a video."
|
76 |
+
]
|
77 |
+
},
|
78 |
+
{
|
79 |
+
"cell_type": "code",
|
80 |
+
"execution_count": null,
|
81 |
+
"id": "complicated-retirement",
|
82 |
+
"metadata": {},
|
83 |
+
"outputs": [],
|
84 |
+
"source": [
|
85 |
+
"%%manim -qm CircleToSquare\n",
|
86 |
+
"\n",
|
87 |
+
"class CircleToSquare(Scene):\n",
|
88 |
+
" def construct(self):\n",
|
89 |
+
" blue_circle = Circle(color=BLUE, fill_opacity=0.5)\n",
|
90 |
+
" green_square = Square(color=GREEN, fill_opacity=0.8)\n",
|
91 |
+
" self.play(Create(blue_circle))\n",
|
92 |
+
" self.wait()\n",
|
93 |
+
" \n",
|
94 |
+
" self.play(Transform(blue_circle, green_square))\n",
|
95 |
+
" self.wait()"
|
96 |
+
]
|
97 |
+
},
|
98 |
+
{
|
99 |
+
"cell_type": "markdown",
|
100 |
+
"id": "tutorial-kelly",
|
101 |
+
"metadata": {},
|
102 |
+
"source": [
|
103 |
+
"While parts of this example might seem self-explanatory, we'll still go over it step by step. First,\n",
|
104 |
+
"```\n",
|
105 |
+
"%%manim -qm CircleToSquare\n",
|
106 |
+
"```\n",
|
107 |
+
"is a *magic command*, it only works within Jupyter notebooks. It is very similar to how you would call `manim` from a terminal: The flag `-qm` controls the render quality, it is shorthand for `--quality=m`, medium rendering quality. This means that the video will be rendered in 720p with 30 fps. (Try to change it to `-qh` or `-ql` for *high* and *low* quality, respectively!)\n",
|
108 |
+
"\n",
|
109 |
+
"Finally, `CircleToSquare` is the name of the scene class you want to render in this particular cell, which already brings us to the next few lines:\n",
|
110 |
+
"```py\n",
|
111 |
+
"class CircleToSquare(Scene):\n",
|
112 |
+
" def construct(self):\n",
|
113 |
+
" [...]\n",
|
114 |
+
"```\n",
|
115 |
+
"This defines a Manim scene named `CircleToSquare`, and defines a custom `construct` method which acts as the *blueprint* for the video. The content of the `construct` method describes what exactly is rendered in the video. \n",
|
116 |
+
"```py\n",
|
117 |
+
"blue_circle = Circle(color=BLUE, fill_opacity=0.5)\n",
|
118 |
+
"green_square = Square(color=GREEN, fill_opacity=0.8)\n",
|
119 |
+
"```\n",
|
120 |
+
"The first two lines create a `Circle` and a `Square` object with the specified colors and fill opacities. However, these are not added to the scene yet! To do that, you either have to use `self.add`, or ...\n",
|
121 |
+
"```py\n",
|
122 |
+
"self.play(Create(blue_circle))\n",
|
123 |
+
"self.wait()\n",
|
124 |
+
"```\n",
|
125 |
+
"... by playing an animation that adds a Manim object (*Mobject*) to the scene. Within the method, `self` references the current scene, `self.play(my_animation)` can be read as \"*This scene should play my animation.*\" \n",
|
126 |
+
"\n",
|
127 |
+
"`Create` is such an animation, but there are many others (for example `FadeIn`, or `DrawBorderThenFill` – try them out above!). The `self.wait()` call does exactly what you would expect: it pauses the video for a while (by default: one second). Change it to `self.wait(2)` for a two-second pause, and so on.\n",
|
128 |
+
"\n",
|
129 |
+
"The final two lines,\n",
|
130 |
+
"```\n",
|
131 |
+
"self.play(Transform(blue_circle, green_square))\n",
|
132 |
+
"self.wait()\n",
|
133 |
+
"```\n",
|
134 |
+
"are responsible for the actual transformation from the blue circle to the green square (plus a one second pause afterwards)."
|
135 |
+
]
|
136 |
+
},
|
137 |
+
{
|
138 |
+
"cell_type": "markdown",
|
139 |
+
"id": "polyphonic-sucking",
|
140 |
+
"metadata": {},
|
141 |
+
"source": [
|
142 |
+
"## Positioning Mobjects and moving them around"
|
143 |
+
]
|
144 |
+
},
|
145 |
+
{
|
146 |
+
"cell_type": "markdown",
|
147 |
+
"id": "pursuant-bubble",
|
148 |
+
"metadata": {},
|
149 |
+
"source": [
|
150 |
+
"New problem: We want to create a scene in which a circle is created while simultaneously some text is written below it. We can reuse our blue circle from above, and then add some new code:"
|
151 |
+
]
|
152 |
+
},
|
153 |
+
{
|
154 |
+
"cell_type": "code",
|
155 |
+
"execution_count": null,
|
156 |
+
"id": "dedicated-wiring",
|
157 |
+
"metadata": {},
|
158 |
+
"outputs": [],
|
159 |
+
"source": [
|
160 |
+
"%%manim -qm HelloCircle\n",
|
161 |
+
"\n",
|
162 |
+
"class HelloCircle(Scene):\n",
|
163 |
+
" def construct(self):\n",
|
164 |
+
" # blue_circle = Circle(color=BLUE, fill_opacity=0.5)\n",
|
165 |
+
" # We can also create a \"plain\" circle and add the desired attributes via set methods:\n",
|
166 |
+
" circle = Circle()\n",
|
167 |
+
" blue_circle = circle.set_color(BLUE).set_opacity(0.5)\n",
|
168 |
+
" \n",
|
169 |
+
" label = Text(\"A wild circle appears!\")\n",
|
170 |
+
" label.next_to(blue_circle, DOWN, buff=0.5)\n",
|
171 |
+
" \n",
|
172 |
+
" self.play(Create(blue_circle), Write(label))\n",
|
173 |
+
" self.wait()"
|
174 |
+
]
|
175 |
+
},
|
176 |
+
{
|
177 |
+
"cell_type": "markdown",
|
178 |
+
"id": "quick-temperature",
|
179 |
+
"metadata": {},
|
180 |
+
"source": [
|
181 |
+
"Apparently, text can be rendered by using a `Text` Mobject – and the desired position is achieved by the line\n",
|
182 |
+
"```py\n",
|
183 |
+
"label.next_to(blue_circle, DOWN, buff=0.5)\n",
|
184 |
+
"```\n",
|
185 |
+
"Mobjects have a few methods for positioning, `next_to` is one of them (`shift`, `to_edge`, `to_corner`, `move_to` are a few others – check them out in our [documentation](https://docs.manim.community/) by using the search bar on the left!). For `next_to`, the first argument that is passed (`blue_circle`) describes next to which object our `label` should be placed. The second argument, `DOWN`, describes the direction (try changing it to `LEFT`, `UP`, or `RIGHT` instead!). And finally, `buff=0.5` controls the \"buffer distance\" between `blue_circle` and `label`, increasing this value will push `label` further down.\n",
|
186 |
+
"\n",
|
187 |
+
"But also note that the `self.play` call has been changed: it is possible to pass several animation arguments to `self.play`, they will then be played simultaneously. If you want to play them one after the other, replace the `self.play` call with the lines\n",
|
188 |
+
"```py\n",
|
189 |
+
"self.play(Create(blue_circle))\n",
|
190 |
+
"self.play(Write(label))\n",
|
191 |
+
"```\n",
|
192 |
+
"and see what happens.\n",
|
193 |
+
"\n",
|
194 |
+
"By the way, Mobjects naturally also have non-positioning related methods: for example, to get our blue circle, we could also create a default one, and then set color and opacity:\n",
|
195 |
+
"```py\n",
|
196 |
+
"circle = Circle()\n",
|
197 |
+
"blue_transparent_circle = circle.set_color(BLUE)\n",
|
198 |
+
"blue_circle = blue_transparent_circle.set_opacity(0.5)\n",
|
199 |
+
"```\n",
|
200 |
+
"A shorter version of this would be\n",
|
201 |
+
"```py\n",
|
202 |
+
"blue_circle = Circle().set_color(BLUE).set_opacity(0.5)\n",
|
203 |
+
"```\n",
|
204 |
+
"For now, we will stick with setting the attributes directly in the call to `Circle`."
|
205 |
+
]
|
206 |
+
},
|
207 |
+
{
|
208 |
+
"cell_type": "markdown",
|
209 |
+
"id": "composed-solomon",
|
210 |
+
"metadata": {},
|
211 |
+
"source": [
|
212 |
+
"## Animating Method calls: the `.animate` syntax"
|
213 |
+
]
|
214 |
+
},
|
215 |
+
{
|
216 |
+
"cell_type": "markdown",
|
217 |
+
"id": "opponent-institution",
|
218 |
+
"metadata": {},
|
219 |
+
"source": [
|
220 |
+
"In the last example we have encountered the `.next_to` method, one of many (!) methods that modify Mobjects in one way or the other. But what if we wanted to animate how a Mobject changes when one of these methods is applied, say, when we `.shift` something around, or `.rotate` a Mobject, or maybe `.scale` it? The `.animate` syntax is the answer to this question, let us look at an example."
|
221 |
+
]
|
222 |
+
},
|
223 |
+
{
|
224 |
+
"cell_type": "code",
|
225 |
+
"execution_count": null,
|
226 |
+
"id": "formal-bargain",
|
227 |
+
"metadata": {},
|
228 |
+
"outputs": [],
|
229 |
+
"source": [
|
230 |
+
"%%manim -qm CircleAnnouncement\n",
|
231 |
+
"\n",
|
232 |
+
"class CircleAnnouncement(Scene):\n",
|
233 |
+
" def construct(self):\n",
|
234 |
+
" blue_circle = Circle(color=BLUE, fill_opacity=0.5)\n",
|
235 |
+
" announcement = Text(\"Let us draw a circle.\")\n",
|
236 |
+
" \n",
|
237 |
+
" self.play(Write(announcement))\n",
|
238 |
+
" self.wait()\n",
|
239 |
+
" \n",
|
240 |
+
" self.play(announcement.animate.next_to(blue_circle, UP, buff=0.5))\n",
|
241 |
+
" self.play(Create(blue_circle))"
|
242 |
+
]
|
243 |
+
},
|
244 |
+
{
|
245 |
+
"cell_type": "markdown",
|
246 |
+
"id": "attractive-citizenship",
|
247 |
+
"metadata": {},
|
248 |
+
"source": [
|
249 |
+
"Where we would normally use `announcement.next_to(blue_circle, UP, buff=0.5)` to position the text without animation, we can prepend `.animate` to the method call to turn the application of the method into an animation which can then be played using `self.play`. This works with all methods that modify a Mobject in some way:"
|
250 |
+
]
|
251 |
+
},
|
252 |
+
{
|
253 |
+
"cell_type": "code",
|
254 |
+
"execution_count": null,
|
255 |
+
"id": "primary-postage",
|
256 |
+
"metadata": {},
|
257 |
+
"outputs": [],
|
258 |
+
"source": [
|
259 |
+
"%%manim -qm AnimateSyntax\n",
|
260 |
+
"\n",
|
261 |
+
"class AnimateSyntax(Scene):\n",
|
262 |
+
" def construct(self):\n",
|
263 |
+
" triangle = Triangle(color=RED, fill_opacity=1)\n",
|
264 |
+
" self.play(DrawBorderThenFill(triangle))\n",
|
265 |
+
" self.play(triangle.animate.shift(LEFT))\n",
|
266 |
+
" self.play(triangle.animate.shift(RIGHT).scale(2))\n",
|
267 |
+
" self.play(triangle.animate.rotate(PI/3))"
|
268 |
+
]
|
269 |
+
},
|
270 |
+
{
|
271 |
+
"cell_type": "markdown",
|
272 |
+
"id": "prompt-reaction",
|
273 |
+
"metadata": {},
|
274 |
+
"source": [
|
275 |
+
"In the first play call the triangle is created, in the second it is shifted to the left, then in the third it is shifted back to the right and simultaneously scaled by a factor of 2, and finally in the fourth call it is rotated by an angle of $\\pi/3$. Run the cell above again after modifying some of the values, or trying other methods like, e.g., `set_color`)."
|
276 |
+
]
|
277 |
+
},
|
278 |
+
{
|
279 |
+
"cell_type": "markdown",
|
280 |
+
"id": "expired-stomach",
|
281 |
+
"metadata": {},
|
282 |
+
"source": [
|
283 |
+
"When looking closely at the last animation from the scene above, the rotation, you might notice that this is not *actually* a rotation. The triangle is transformed to a rotated version of itself, but during the animation the vertices of the triangle don't move along an arc (as they would when the triangle was rotated around its center), but rather along straight lines, which gives the animation the impression that the triangle first shrinks a bit and then grows again.\n",
|
284 |
+
"\n",
|
285 |
+
"This is actually **not a bug**, but a consequence of how the `.animate` syntax works: the animation is constructed by specifying the starting state (the `triangle` Mobject in the example above), and the final state (the rotated mobject, `triangle.rotate(PI/3)`). Manim then tries to interpolate between these two, but doesn't actually know that you would like to smoothly rotate the triangle. The following example illustrates this clearly:"
|
286 |
+
]
|
287 |
+
},
|
288 |
+
{
|
289 |
+
"cell_type": "code",
|
290 |
+
"execution_count": null,
|
291 |
+
"id": "recent-department",
|
292 |
+
"metadata": {},
|
293 |
+
"outputs": [],
|
294 |
+
"source": [
|
295 |
+
"%%manim -qm DifferentRotations\n",
|
296 |
+
"\n",
|
297 |
+
"class DifferentRotations(Scene):\n",
|
298 |
+
" def construct(self):\n",
|
299 |
+
" left_square = Square(color=BLUE, fill_opacity=0.7).shift(2*LEFT)\n",
|
300 |
+
" right_square = Square(color=GREEN, fill_opacity=0.7).shift(2*RIGHT)\n",
|
301 |
+
" self.play(left_square.animate.rotate(PI), Rotate(right_square, angle=PI), run_time=2)\n",
|
302 |
+
" self.wait()"
|
303 |
+
]
|
304 |
+
},
|
305 |
+
{
|
306 |
+
"cell_type": "markdown",
|
307 |
+
"id": "urban-liver",
|
308 |
+
"metadata": {},
|
309 |
+
"source": [
|
310 |
+
"## Typesetting Mathematics"
|
311 |
+
]
|
312 |
+
},
|
313 |
+
{
|
314 |
+
"cell_type": "markdown",
|
315 |
+
"id": "divine-evanescence",
|
316 |
+
"metadata": {},
|
317 |
+
"source": [
|
318 |
+
"Manim supports rendering and animating LaTeX, the markup language mathematics is very often typeset in. Learn more about it [in this 30 minute tutorial](https://www.overleaf.com/learn/latex/Learn_LaTeX_in_30_minutes).\n",
|
319 |
+
"\n",
|
320 |
+
"Here is a simple example for working with LaTeX in Manim:"
|
321 |
+
]
|
322 |
+
},
|
323 |
+
{
|
324 |
+
"cell_type": "code",
|
325 |
+
"execution_count": null,
|
326 |
+
"id": "bulgarian-violin",
|
327 |
+
"metadata": {},
|
328 |
+
"outputs": [],
|
329 |
+
"source": [
|
330 |
+
"%%manim -qm CauchyIntegralFormula\n",
|
331 |
+
"\n",
|
332 |
+
"class CauchyIntegralFormula(Scene):\n",
|
333 |
+
" def construct(self):\n",
|
334 |
+
" formula = MathTex(r\"[z^n]f(z) = \\frac{1}{2\\pi i}\\oint_{\\gamma} \\frac{f(z)}{z^{n+1}}~dz\")\n",
|
335 |
+
" self.play(Write(formula), run_time=3)\n",
|
336 |
+
" self.wait()"
|
337 |
+
]
|
338 |
+
},
|
339 |
+
{
|
340 |
+
"cell_type": "markdown",
|
341 |
+
"id": "potential-crowd",
|
342 |
+
"metadata": {},
|
343 |
+
"source": [
|
344 |
+
"As this example demonstrates, `MathTex` allows to render simple (math mode) LaTeX strings. If you want to render \"normal mode\" LaTex, use `Tex` instead.\n",
|
345 |
+
"\n",
|
346 |
+
"Of course, Manim can also help you to visualize transformations of typeset formulae. Consider the following example:"
|
347 |
+
]
|
348 |
+
},
|
349 |
+
{
|
350 |
+
"cell_type": "code",
|
351 |
+
"execution_count": null,
|
352 |
+
"id": "proof-multiple",
|
353 |
+
"metadata": {},
|
354 |
+
"outputs": [],
|
355 |
+
"source": [
|
356 |
+
"%%manim -qm TransformEquation\n",
|
357 |
+
"\n",
|
358 |
+
"class TransformEquation(Scene):\n",
|
359 |
+
" def construct(self):\n",
|
360 |
+
" eq1 = MathTex(\"42 {{ a^2 }} + {{ b^2 }} = {{ c^2 }}\")\n",
|
361 |
+
" eq2 = MathTex(\"42 {{ a^2 }} = {{ c^2 }} - {{ b^2 }}\")\n",
|
362 |
+
" eq3 = MathTex(r\"a^2 = \\frac{c^2 - b^2}{42}\")\n",
|
363 |
+
" self.add(eq1)\n",
|
364 |
+
" self.wait()\n",
|
365 |
+
" self.play(TransformMatchingTex(eq1, eq2))\n",
|
366 |
+
" self.wait()\n",
|
367 |
+
" self.play(TransformMatchingShapes(eq2, eq3))\n",
|
368 |
+
" self.wait()"
|
369 |
+
]
|
370 |
+
},
|
371 |
+
{
|
372 |
+
"cell_type": "markdown",
|
373 |
+
"id": "fuzzy-output",
|
374 |
+
"metadata": {},
|
375 |
+
"source": [
|
376 |
+
"In this last example, `eq1` and `eq2` have some double braces positions where, conventionally, there wouldn't be any in plain LaTeX. This is special Manim notation that groups the resulting `Tex` Mobjects `eq1` and `eq2` in a particular way.\n",
|
377 |
+
"\n",
|
378 |
+
"This special notation is helpful when using the `TransformMatchingTex` animation: it will transform parts with equal TeX strings (for example, `a^2` to `a^2`) into each other – and without the special notation, the equation is considered to be one long TeX string. In comparison, `TransformMatchingShapes` is less smart: it simply tries to transform shapes that \"look the same\" into each other – nonetheless, it is still often very useful."
|
379 |
+
]
|
380 |
+
},
|
381 |
+
{
|
382 |
+
"cell_type": "markdown",
|
383 |
+
"id": "monetary-sleep",
|
384 |
+
"metadata": {},
|
385 |
+
"source": [
|
386 |
+
"If you have made it this far, you should have a first impression of basic usage of the library. You can find a few more advanced examples that illustrate some more specialized concepts in the library below. Go ahead, try to play around and modify them just like you did for the ones above! Explore our [documentation](https://docs.manim.community) to get an idea about things that are already implemented – and look at the source code in case you want to build some more complex objects yourself.\n",
|
387 |
+
"\n",
|
388 |
+
"The [community](https://www.manim.community/discord/) is certainly also happy to answer questions – and we hope you share your awesome projects with us! **Happy *manimating*!**"
|
389 |
+
]
|
390 |
+
},
|
391 |
+
{
|
392 |
+
"cell_type": "markdown",
|
393 |
+
"id": "attractive-fight",
|
394 |
+
"metadata": {},
|
395 |
+
"source": [
|
396 |
+
"## Some more specialized examples"
|
397 |
+
]
|
398 |
+
},
|
399 |
+
{
|
400 |
+
"cell_type": "markdown",
|
401 |
+
"id": "editorial-growth",
|
402 |
+
"metadata": {},
|
403 |
+
"source": [
|
404 |
+
"Before you delve right into these examples: please note that they illustrate specialized concepts, they are meant to give you a feeling for how more complex scenes are setup and coded. The examples don't come with additional explanation, they are **not intended as (entry level) learning resources**."
|
405 |
+
]
|
406 |
+
},
|
407 |
+
{
|
408 |
+
"cell_type": "code",
|
409 |
+
"execution_count": null,
|
410 |
+
"id": "unexpected-berlin",
|
411 |
+
"metadata": {},
|
412 |
+
"outputs": [],
|
413 |
+
"source": [
|
414 |
+
"%%manim -qm FormulaEmphasis\n",
|
415 |
+
"\n",
|
416 |
+
"class FormulaEmphasis(Scene):\n",
|
417 |
+
" def construct(self):\n",
|
418 |
+
" product_formula = MathTex(\n",
|
419 |
+
" r\"\\frac{d}{dx} f(x)g(x) =\",\n",
|
420 |
+
" r\"f(x) \\frac{d}{dx} g(x)\",\n",
|
421 |
+
" r\"+\",\n",
|
422 |
+
" r\"g(x) \\frac{d}{dx} f(x)\"\n",
|
423 |
+
" )\n",
|
424 |
+
" self.play(Write(product_formula))\n",
|
425 |
+
" box1 = SurroundingRectangle(product_formula[1], buff=0.1)\n",
|
426 |
+
" box2 = SurroundingRectangle(product_formula[3], buff=0.1)\n",
|
427 |
+
" self.play(Create(box1))\n",
|
428 |
+
" self.wait()\n",
|
429 |
+
" self.play(Transform(box1, box2))\n",
|
430 |
+
" self.wait()"
|
431 |
+
]
|
432 |
+
},
|
433 |
+
{
|
434 |
+
"cell_type": "code",
|
435 |
+
"execution_count": null,
|
436 |
+
"id": "aware-dispatch",
|
437 |
+
"metadata": {},
|
438 |
+
"outputs": [],
|
439 |
+
"source": [
|
440 |
+
"%%manim -qm PlotExample\n",
|
441 |
+
"\n",
|
442 |
+
"class PlotExample(Scene):\n",
|
443 |
+
" def construct(self):\n",
|
444 |
+
" plot_axes = Axes(\n",
|
445 |
+
" x_range=[0, 1, 0.05],\n",
|
446 |
+
" y_range=[0, 1, 0.05],\n",
|
447 |
+
" x_length=9,\n",
|
448 |
+
" y_length=5.5,\n",
|
449 |
+
" axis_config={\n",
|
450 |
+
" \"numbers_to_include\": np.arange(0, 1 + 0.1, 0.1),\n",
|
451 |
+
" \"font_size\": 24,\n",
|
452 |
+
" },\n",
|
453 |
+
" tips=False,\n",
|
454 |
+
" )\n",
|
455 |
+
"\n",
|
456 |
+
" y_label = plot_axes.get_y_axis_label(\"y\", edge=LEFT, direction=LEFT, buff=0.4)\n",
|
457 |
+
" x_label = plot_axes.get_x_axis_label(\"x\")\n",
|
458 |
+
" plot_labels = VGroup(x_label, y_label)\n",
|
459 |
+
"\n",
|
460 |
+
" plots = VGroup()\n",
|
461 |
+
" for n in np.arange(1, 20 + 0.5, 0.5):\n",
|
462 |
+
" plots += plot_axes.plot(lambda x: x**n, color=WHITE)\n",
|
463 |
+
" plots += plot_axes.plot(\n",
|
464 |
+
" lambda x: x**(1 / n), color=WHITE, use_smoothing=False\n",
|
465 |
+
" )\n",
|
466 |
+
"\n",
|
467 |
+
" extras = VGroup()\n",
|
468 |
+
" extras += plot_axes.get_horizontal_line(plot_axes.c2p(1, 1, 0), color=BLUE)\n",
|
469 |
+
" extras += plot_axes.get_vertical_line(plot_axes.c2p(1, 1, 0), color=BLUE)\n",
|
470 |
+
" extras += Dot(point=plot_axes.c2p(1, 1, 0), color=YELLOW)\n",
|
471 |
+
" title = Title(\n",
|
472 |
+
" r\"Graphs of $y=x^{\\frac{1}{n}}$ and $y=x^n (n=1, 1.5, 2, 2.5, 3, \\dots, 20)$\",\n",
|
473 |
+
" include_underline=False,\n",
|
474 |
+
" font_size=40,\n",
|
475 |
+
" )\n",
|
476 |
+
" \n",
|
477 |
+
" self.play(Write(title))\n",
|
478 |
+
" self.play(Create(plot_axes), Create(plot_labels), Create(extras))\n",
|
479 |
+
" self.play(AnimationGroup(*[Create(plot) for plot in plots], lag_ratio=0.05))"
|
480 |
+
]
|
481 |
+
},
|
482 |
+
{
|
483 |
+
"cell_type": "code",
|
484 |
+
"execution_count": null,
|
485 |
+
"id": "significant-toner",
|
486 |
+
"metadata": {},
|
487 |
+
"outputs": [],
|
488 |
+
"source": [
|
489 |
+
"%%manim -qm ErdosRenyiGraph\n",
|
490 |
+
"\n",
|
491 |
+
"import networkx as nx\n",
|
492 |
+
"\n",
|
493 |
+
"nxgraph = nx.erdos_renyi_graph(14, 0.5)\n",
|
494 |
+
"\n",
|
495 |
+
"class ErdosRenyiGraph(Scene):\n",
|
496 |
+
" def construct(self):\n",
|
497 |
+
" G = Graph.from_networkx(nxgraph, layout=\"spring\", layout_scale=3.5)\n",
|
498 |
+
" self.play(Create(G))\n",
|
499 |
+
" self.play(*[G[v].animate.move_to(5*RIGHT*np.cos(ind/7 * PI) +\n",
|
500 |
+
" 3*UP*np.sin(ind/7 * PI))\n",
|
501 |
+
" for ind, v in enumerate(G.vertices)])\n",
|
502 |
+
" self.play(Uncreate(G))"
|
503 |
+
]
|
504 |
+
},
|
505 |
+
{
|
506 |
+
"cell_type": "code",
|
507 |
+
"execution_count": null,
|
508 |
+
"id": "electrical-forty",
|
509 |
+
"metadata": {},
|
510 |
+
"outputs": [],
|
511 |
+
"source": [
|
512 |
+
"%%manim -qm CodeFromString\n",
|
513 |
+
"\n",
|
514 |
+
"class CodeFromString(Scene):\n",
|
515 |
+
" def construct(self):\n",
|
516 |
+
" code = '''from manim import Scene, Square\n",
|
517 |
+
"\n",
|
518 |
+
"class FadeInSquare(Scene):\n",
|
519 |
+
" def construct(self):\n",
|
520 |
+
" s = Square()\n",
|
521 |
+
" self.play(FadeIn(s))\n",
|
522 |
+
" self.play(s.animate.scale(2))\n",
|
523 |
+
" self.wait()\n",
|
524 |
+
"'''\n",
|
525 |
+
" rendered_code = Code(code=code, tab_width=4, background=\"window\",\n",
|
526 |
+
" language=\"Python\", font=\"Monospace\")\n",
|
527 |
+
" self.play(Write(rendered_code))\n",
|
528 |
+
" self.wait(2)"
|
529 |
+
]
|
530 |
+
},
|
531 |
+
{
|
532 |
+
"cell_type": "code",
|
533 |
+
"execution_count": null,
|
534 |
+
"id": "funny-reservoir",
|
535 |
+
"metadata": {},
|
536 |
+
"outputs": [],
|
537 |
+
"source": [
|
538 |
+
"%%manim -qm OpeningManim\n",
|
539 |
+
"\n",
|
540 |
+
"class OpeningManim(Scene):\n",
|
541 |
+
" def construct(self):\n",
|
542 |
+
" title = Tex(r\"This is some \\LaTeX\")\n",
|
543 |
+
" basel = MathTex(r\"\\sum_{n=1}^\\infty \\frac{1}{n^2} = \\frac{\\pi^2}{6}\")\n",
|
544 |
+
" VGroup(title, basel).arrange(DOWN)\n",
|
545 |
+
" self.play(\n",
|
546 |
+
" Write(title),\n",
|
547 |
+
" FadeIn(basel, shift=UP),\n",
|
548 |
+
" )\n",
|
549 |
+
" self.wait()\n",
|
550 |
+
"\n",
|
551 |
+
" transform_title = Tex(\"That was a transform\")\n",
|
552 |
+
" transform_title.to_corner(UP + LEFT)\n",
|
553 |
+
" self.play(\n",
|
554 |
+
" Transform(title, transform_title),\n",
|
555 |
+
" LaggedStart(*[FadeOut(obj, shift=DOWN) for obj in basel]),\n",
|
556 |
+
" )\n",
|
557 |
+
" self.wait()\n",
|
558 |
+
"\n",
|
559 |
+
" grid = NumberPlane(x_range=(-10, 10, 1), y_range=(-6.0, 6.0, 1))\n",
|
560 |
+
" grid_title = Tex(\"This is a grid\")\n",
|
561 |
+
" grid_title.scale(1.5)\n",
|
562 |
+
" grid_title.move_to(transform_title)\n",
|
563 |
+
"\n",
|
564 |
+
" self.add(grid, grid_title)\n",
|
565 |
+
" self.play(\n",
|
566 |
+
" FadeOut(title),\n",
|
567 |
+
" FadeIn(grid_title, shift=DOWN),\n",
|
568 |
+
" Create(grid, run_time=3, lag_ratio=0.1),\n",
|
569 |
+
" )\n",
|
570 |
+
" self.wait()\n",
|
571 |
+
"\n",
|
572 |
+
" grid_transform_title = Tex(\n",
|
573 |
+
" r\"That was a non-linear function \\\\ applied to the grid\"\n",
|
574 |
+
" )\n",
|
575 |
+
" grid_transform_title.move_to(grid_title, UL)\n",
|
576 |
+
" grid.prepare_for_nonlinear_transform()\n",
|
577 |
+
" self.play(\n",
|
578 |
+
" grid.animate.apply_function(\n",
|
579 |
+
" lambda p: p + np.array([np.sin(p[1]), np.sin(p[0]), 0])\n",
|
580 |
+
" ),\n",
|
581 |
+
" run_time=3,\n",
|
582 |
+
" )\n",
|
583 |
+
" self.wait()\n",
|
584 |
+
" self.play(Transform(grid_title, grid_transform_title))\n",
|
585 |
+
" self.wait()"
|
586 |
+
]
|
587 |
+
},
|
588 |
+
{
|
589 |
+
"cell_type": "code",
|
590 |
+
"execution_count": null,
|
591 |
+
"id": "enabling-greek",
|
592 |
+
"metadata": {},
|
593 |
+
"outputs": [],
|
594 |
+
"source": []
|
595 |
+
}
|
596 |
+
],
|
597 |
+
"metadata": {
|
598 |
+
"kernelspec": {
|
599 |
+
"display_name": "Python 3 (ipykernel)",
|
600 |
+
"language": "python",
|
601 |
+
"name": "python3"
|
602 |
+
},
|
603 |
+
"language_info": {
|
604 |
+
"codemirror_mode": {
|
605 |
+
"name": "ipython",
|
606 |
+
"version": 3
|
607 |
+
},
|
608 |
+
"file_extension": ".py",
|
609 |
+
"mimetype": "text/x-python",
|
610 |
+
"name": "python",
|
611 |
+
"nbconvert_exporter": "python",
|
612 |
+
"pygments_lexer": "ipython3",
|
613 |
+
"version": "3.9.9"
|
614 |
+
}
|
615 |
+
},
|
616 |
+
"nbformat": 4,
|
617 |
+
"nbformat_minor": 5
|
618 |
+
}
|