diff --git a/manimlib/camera/camera_frame.py b/manimlib/camera/camera_frame.py index c126053c..648d55cd 100644 --- a/manimlib/camera/camera_frame.py +++ b/manimlib/camera/camera_frame.py @@ -29,9 +29,11 @@ class CameraFrame(Mobject): # Field of view in the y direction fovy: float = 45 * DEGREES, euler_axes: str = "zxz", + # This keeps it ordered first in a scene + z_index=-1, **kwargs, ): - super().__init__(**kwargs) + super().__init__(z_index=z_index, **kwargs) self.uniforms["orientation"] = Rotation.identity().as_quat() self.uniforms["fovy"] = fovy diff --git a/manimlib/mobject/mobject.py b/manimlib/mobject/mobject.py index 2dfbb812..a09d6116 100644 --- a/manimlib/mobject/mobject.py +++ b/manimlib/mobject/mobject.py @@ -94,7 +94,6 @@ class Mobject(object): self.texture_paths = texture_paths self.depth_test = depth_test self.z_index = z_index - self._scene_order = 0 # Internal state self.submobjects: list[Mobject] = [] @@ -1246,6 +1245,10 @@ class Mobject(object): def set_z(self, z: float, direction: Vect3 = ORIGIN) -> Self: return self.set_coord(z, 2, direction) + def set_z_index(self, z_index: int) -> Self: + self.z_index = z_index + return self + def space_out_submobjects(self, factor: float = 1.5, **kwargs) -> Self: self.scale(factor, **kwargs) for submob in self.submobjects: diff --git a/manimlib/scene/scene.py b/manimlib/scene/scene.py index 5fab8f85..f73fb088 100644 --- a/manimlib/scene/scene.py +++ b/manimlib/scene/scene.py @@ -414,13 +414,12 @@ class Scene(object): foreground in the order with which they are added. """ self.remove(*new_mobjects) - idx = 0 - scene_order = len(self.mobjects) - for m in new_mobjects: - m._scene_order = scene_order+idx - idx += 1 self.mobjects += new_mobjects - self.mobjects = [self.mobjects[0]]+sorted(self.mobjects[1:], key=lambda m:(m.z_index, m._scene_order)) + + # Reorder based on z_index + id_to_scene_order = {id(m): idx for idx, m in enumerate(self.mobjects)} + self.mobjects.sort(key=lambda m: (m.z_index, id_to_scene_order[id(m)])) + self.id_to_mobject_map.update({ id(sm): sm for m in new_mobjects