A community-maintained Python framework for creating mathematical animations.
Fixed issues concerning frame_center in ThreeDScene (#1821)
Suhail Sherifedd7b3423d7b
diff --git a/manim/camera/three_d_camera.py b/manim/camera/three_d_camera.pyindex 55462fd2f..448979c63 100644--- a/manim/camera/three_d_camera.py+++ b/manim/camera/three_d_camera.py@@ -44,7 +44,7 @@ class ThreeDCamera(Camera):*kwargsAny keyword argument of Camera."""- self._frame_center = Point(kwargs.get("frame_center", ORIGIN))+ self._frame_center = Point(kwargs.get("frame_center", ORIGIN), stroke_width=0)super().__init__(**kwargs)self.distance = distanceself.phi = phi@@ -289,7 +289,6 @@ class ThreeDCamera(Camera):factor = distance / (distance - zs)factor[(distance - zs) < 0] = 10 ** 6points[:, i] *= factor- points = points + frame_centerreturn pointsdef project_point(self, point):@@ -387,7 +386,7 @@ class ThreeDCamera(Camera):mobjects : :class:`Mobject`The mobjects whose orientation need not be fixed any longer."""- for mobject in self.extract_mobject_family_members(mobjects):+ for mobject in extract_mobject_family_members(mobjects):if mobject in self.fixed_orientation_mobjects:self.fixed_orientation_mobjects.remove(mobject)@@ -401,6 +400,6 @@ class ThreeDCamera(Camera):mobjects : :class:`Mobject`The mobjects which need not be fixed in frame any longer."""- for mobject in self.extract_mobject_family_members(mobjects):+ for mobject in extract_mobject_family_members(mobjects):if mobject in self.fixed_in_frame_mobjects:self.fixed_in_frame_mobjects.remove(mobject)diff --git a/manim/scene/three_d_scene.py b/manim/scene/three_d_scene.pyindex 0e7091d73..e0aac5a46 100644--- a/manim/scene/three_d_scene.py+++ b/manim/scene/three_d_scene.py@@ -3,14 +3,18 @@__all__ = ["ThreeDScene", "SpecialThreeDScene"]+from typing import Iterable, Optional, Sequence, Union+import numpy as npfrom .. import config+from ..animation.animation import Animationfrom ..animation.transform import ApplyMethodfrom ..camera.three_d_camera import ThreeDCamerafrom ..constants import DEGREESfrom ..mobject.coordinate_systems import ThreeDAxesfrom ..mobject.geometry import Line+from ..mobject.mobject import Mobjectfrom ..mobject.three_dimensions import Spherefrom ..mobject.types.vectorized_mobject import VectorizedPoint, VGroupfrom ..mobject.value_tracker import ValueTracker@@ -42,7 +46,14 @@ class ThreeDScene(Scene):)super().__init__(camera_class=camera_class, **kwargs)- def set_camera_orientation(self, phi=None, theta=None, distance=None, gamma=None):+ def set_camera_orientation(+ self,+ phi: Optional[float] = None,+ theta: Optional[float] = None,+ gamma: Optional[float] = None,+ distance: Optional[float] = None,+ frame_center: Optional[Union["Mobject", Sequence[float]]] = None,+ ):"""This method sets the orientation of the camera in the scene.@@ -59,6 +70,10 @@ class ThreeDScene(Scene):gamma : int or float, optionalThe rotation of the camera about the vector from the ORIGIN to the Camera.++ frame_center : list, tuple or np.array, optional+ The new center of the camera frame in cartesian coordinates.+"""if phi is not None:self.renderer.camera.set_phi(phi)@@ -68,6 +83,8 @@ class ThreeDScene(Scene):self.renderer.camera.set_distance(distance)if gamma is not None:self.renderer.camera.set_gamma(gamma)+ if frame_center is not None:+ self.renderer.camera._frame_center.move_to(frame_center)def begin_ambient_camera_rotation(self, rate=0.02, about="theta"):"""@@ -146,12 +163,12 @@ class ThreeDScene(Scene):def move_camera(self,- phi=None,- theta=None,- distance=None,- gamma=None,- frame_center=None,- added_anims=[],+ phi: Optional[float] = None,+ theta: Optional[float] = None,+ gamma: Optional[float] = None,+ distance: Optional[float] = None,+ frame_center: Optional[Union["Mobject", Sequence[float]]] = None,+ added_anims: Iterable["Animation"] = [],**kwargs,):"""@@ -173,7 +190,7 @@ class ThreeDScene(Scene):The rotation of the camera about the vector from the ORIGIN to the Camera.frame_center : list, tuple or np.array, optional- The new center of the camera frame in cartesian coordinates+ The new center of the camera frame in cartesian coordinates.added_anims : list, optionalAny other animations to be played at the same time.@@ -191,11 +208,20 @@ class ThreeDScene(Scene):anims.append(ApplyMethod(tracker.set_value, value, **kwargs))if frame_center is not None:anims.append(- ApplyMethod(self.renderer.camera._frame_center.move_to, frame_center)+ ApplyMethod(+ self.renderer.camera._frame_center.move_to, frame_center, **kwargs+ ))self.play(*anims + added_anims)+ # These lines are added to improve performance. If manim thinks that frame_center is moving,+ # it is required to redraw every object. These lines remove frame_center from the Scene once+ # its animation is done, ensuring that manim does not think that it is moving. Since the+ # frame_center is never actually drawn, this shouldn't break anything.+ if frame_center is not None:+ self.remove(self.renderer.camera._frame_center)+def get_moving_mobjects(self, *animations):"""This method returns a list of all of the Mobjects in the Scene that@@ -207,7 +233,9 @@ class ThreeDScene(Scene):The animations whose mobjects will be checked."""moving_mobjects = Scene.get_moving_mobjects(self, *animations)- camera_mobjects = self.renderer.camera.get_value_trackers()+ camera_mobjects = self.renderer.camera.get_value_trackers() + [+ self.renderer.camera._frame_center+ ]if any([cm in moving_mobjects for cm in camera_mobjects]):return self.mobjectsreturn moving_mobjectsdiff --git a/tests/control_data/graphical_units_data/threed/CameraMoveTest.npz b/tests/control_data/graphical_units_data/threed/CameraMoveTest.npzindex f43882a93..9624c629d 100644Binary files a/tests/control_data/graphical_units_data/threed/CameraMoveTest.npz and b/tests/control_data/graphical_units_data/threed/CameraMoveTest.npz differdiff --git a/tests/test_graphical_units/test_threed.py b/tests/test_graphical_units/test_threed.pyindex 1dae1b4cc..ff310bbc8 100644--- a/tests/test_graphical_units/test_threed.py+++ b/tests/test_graphical_units/test_threed.py@@ -52,10 +52,13 @@ class AxesTest(ThreeDScene):class CameraMoveTest(ThreeDScene):+ """Tests camera movement to explore varied views of a static scene."""+def construct(self):- cube = Cube()- self.add(cube)- self.move_camera(phi=PI / 4, theta=PI / 4, frame_center=[0, 0, -1])+ axes = ThreeDAxes()+ self.add(axes)+ self.add(Dot([1, 2, 3]))+ self.move_camera(phi=PI / 8, theta=-PI / 8, frame_center=[1, 2, 3])class AmbientCameraMoveTest(ThreeDScene):