Manim animations¶
Starcel can play manim scenes (3Blue1Brown's manimgl) as real 3D actors: lines become cylinders and splines, dots become spheres, text becomes 3D letters.
from ue_spawn import spawn_manim
from unreal_engine import FVector
spawn_manim('LorenzAttractor', location=FVector(0, 0, 400))
Click the sphere at the lower left of the scene to play, pause and resume it.
The first time, a scene is recorded by a Python process in the background (this can take a minute or two). Recordings are cached in Saved/Manim, so it starts at once after that. The sphere's colour tells you the state:
| Colour | State |
|---|---|
| Grey | Recording |
| Green | Ready |
| Yellow | Playing |
| Blue | Paused |
| Red | Failed (the log says why) |
Built-in scenes¶
FourierEpicycles, RiemannSums, ComplexMaps, LinearMap, TaylorSeries, VectorFieldFlow, DoublePendulumChaos, HilbertCurve, LorenzAttractor, SurfaceMorph, HopfFibration, BooleanAndText.
They are in Content/Scripts/ue_math/manim_render/examples.py. Show them all as a wall:
from ue_math.manim_render.client import example_names
for i, scene in enumerate(example_names()):
row, column = divmod(i, 4)
spawn_manim(scene, location=FVector(0, 400 + column * 700, 1000 - row * 400))
Your own scene¶
From a file:
Or straight from code, for example in a code cell:
spawn_manim(code='''
from manimlib import *
class Hello(Scene):
def construct(self):
circle = Circle(color=BLUE)
square = Square(color=YELLOW)
self.play(ShowCreation(circle))
self.play(Transform(circle, square))
self.wait()
''', autoplay=True)
With path or code, scene can be left out to use the last Scene in the file.
manimgl, not Manim Community
Scenes are written for manimgl: use ShowCreation, Tex and axes.get_graph, not Create, MathTex and axes.plot.
Options¶
| Option | Default | Does |
|---|---|---|
location |
origin | Centre of the frame. The viewer looks along +X. |
uu |
40.0 |
Size of a manim unit in UU. The frame is 14.2 × 8 units. |
fps |
30 |
Frames per second |
autoplay |
False |
Start without a click |
panel |
False |
Dark rectangle behind the scene |
The returned actor's actor.get_py_proxy().player has click() and state.
In the tests¶
Copied from the code when this site is built.
From the tests: test_manim (Content/Scripts/test_spawn.py:1830)
def test_manim(base_y=0):
"""Hang the manim_render examples as a wall, 4 wide and 3 high.
Click the sphere at the lower left of a scene to play it. Colours: grey
being recorded (the first time only), green ready, yellow playing, blue
paused, red failed.
Recording takes a minute or two the first time, so PASS means that the
scene was set up; a scene that can't be recorded turns red and logs why.
"""
from ue_spawn import spawn_manim
from ue_math.manim_render.client import example_names
results = {}
for i, scene in enumerate(example_names()):
row, column = divmod(i, 4)
loc = FVector(0, base_y + 400 + column * 700, 1000 - row * 400)
actor = None
t0 = time.monotonic()
try:
actor = spawn_manim(scene, location=loc)
except Exception as e:
_log_exception(f'manim_{scene}', e)
results.update(_result(f'manim_{scene}', actor, elapsed=time.monotonic() - t0))
return results
The offline tests (python -m ue_math.manim_render.test_manim_render in Content/Scripts) show what each kind of mobject becomes in Unreal:
From the tests: Actors.test_line_is_a_cylinder (Content/Scripts/ue_math/manim_render/test_manim_render.py:161)
def test_line_is_a_cylinder(self):
line = Line([-2, 1, 0], [3, 2, 1], stroke_width=10)
prims, parts = actors_of(line)
self.assertEqual(([p.kind for p in prims], parts), (['cylinder'], []))
a, b = ends(prims[0].row)
np.testing.assert_allclose(a, [-2, 1, 0], atol=1e-6)
np.testing.assert_allclose(b, [3, 2, 1], atol=1e-6)
self.assertAlmostEqual(prims[0].row[6] * 50, 0.05 * actors.STROKE_SCALE * actors.UNIT)
From the tests: Actors.test_dots_are_spheres (Content/Scripts/ue_math/manim_render/test_manim_render.py:190)
def test_dots_are_spheres(self):
prims, _ = actors_of(Dot([1, 2, 0], radius=0.2))
self.assertEqual(prims[0].kind, 'sphere')
np.testing.assert_allclose(manim(prims[0].row[:3])[:2], [1, 2], atol=1e-6)
self.assertAlmostEqual(prims[0].row[6] * 50, 0.2 * actors.UNIT)
self.assertEqual(kinds(DotCloud(np.random.default_rng(0).random((7, 3)))), {'sphere': 7})
From the tests: Actors.test_arrow_is_cylinder_and_cone (Content/Scripts/ue_math/manim_render/test_manim_render.py:197)
def test_arrow_is_cylinder_and_cone(self):
prims, _ = actors_of(Arrow(LEFT, RIGHT, buff=0))
self.assertEqual([p.kind for p in prims], ['cylinder', 'cone'])
np.testing.assert_allclose(ends(prims[1].row)[1][:2], [1, 0], atol=1e-5)
np.testing.assert_allclose(ends(prims[0].row)[0][:2], [-1, 0], atol=0.02)