Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Add Scene.embed()

Devin Neala75d551bafff
diff --git a/example_scenes/opengl.py b/example_scenes/opengl.py
index c3a80e9b5..1f862a28f 100644
--- a/example_scenes/opengl.py
+++ b/example_scenes/opengl.py
@@ -3,8 +3,57 @@ from manim.opengl import *
import os
from pathlib import Path
+
# Copied from https://3b1b.github.io/manim/getting_started/example_scenes.html#surfaceexample.
# Lines that do not yet work with the Community Version are commented.
+
+
+class InteractiveDevelopment(Scene):
+ def construct(self):
+ circle = OpenGLCircle()
+ circle.set_fill(BLUE, opacity=0.5)
+ circle.set_stroke(BLUE_E, width=4)
+ square = OpenGLSquare()
+
+ self.play(ShowCreation(square))
+ self.wait()
+
+ # This opens an iPython termnial where you can keep writing
+ # lines as if they were part of this construct method.
+ # In particular, 'square', 'circle' and 'self' will all be
+ # part of the local namespace in that terminal.
+ self.embed()
+
+ # Try copying and pasting some of the lines below into
+ # the interactive shell
+ self.play(ReplacementTransform(square, circle))
+ self.wait()
+ self.play(circle.animate.stretch(4, 0))
+ self.play(Rotate(circle, 90 * DEGREES))
+ self.play(circle.animate.shift(2 * RIGHT).scale(0.25))
+
+ # text = Text("""
+ # In general, using the interactive shell
+ # is very helpful when developing new scenes
+ # """)
+ # self.play(Write(text))
+
+ # # In the interactive shell, you can just type
+ # # play, add, remove, clear, wait, save_state and restore,
+ # # instead of self.play, self.add, self.remove, etc.
+
+ # # To interact with the window, type touch(). You can then
+ # # scroll in the window, or zoom by holding down 'z' while scrolling,
+ # # and change camera perspective by holding down 'd' while moving
+ # # the mouse. Press 'r' to reset to the standard camera position.
+ # # Press 'q' to stop interacting with the window and go back to
+ # # typing new commands into the shell.
+
+ # # In principle you can customize a scene to be responsive to
+ # # mouse and keyboard interactions
+ # always(circle.move_to, self.mouse_point)
+
+
class SurfaceExample(Scene):
def construct(self):
# surface_text = Text("For 3d scenes, try using surfaces")
diff --git a/manim/scene/scene.py b/manim/scene/scene.py
index 720d6212a..bedc1c04c 100644
--- a/manim/scene/scene.py
+++ b/manim/scene/scene.py
@@ -873,6 +873,46 @@ class Scene(Container):
self.update_mobjects(0)
self.renderer.static_image = None
+ def embed(self):
+ if not config["preview"]:
+ logger.warning("Called embed() while no preview window is available.")
+ return
+ if config["write_to_movie"]:
+ logger.warning("embed() is skipped while writing to a file.")
+ return
+
+ self.renderer.render(self, -1, self.moving_mobjects)
+
+ # Configure IPython shell.
+ from IPython.terminal.embed import InteractiveShellEmbed
+
+ shell = InteractiveShellEmbed()
+
+ # Have the frame update after each command
+ shell.events.register(
+ "post_run_cell",
+ lambda *a, **kw: self.renderer.render(self, -1, self.moving_mobjects),
+ )
+
+ # Use the locals of the caller as the local namespace
+ # once embeded, and add a few custom shortcuts.
+ local_ns = inspect.currentframe().f_back.f_locals
+ # local_ns["touch"] = self.interact
+ for method in (
+ "play",
+ "wait",
+ "add",
+ "remove",
+ # "clear",
+ # "save_state",
+ # "restore",
+ ):
+ local_ns[method] = getattr(self, method)
+ shell(local_ns=local_ns, stack_depth=2)
+
+ # End scene when exiting an embed.
+ raise Exception("Exiting scene.")
+
def update_to_time(self, t):
dt = t - self.last_t
self.last_t = t