Clean up around z_index

This commit is contained in:
Grant Sanderson 2024-09-06 12:20:26 -05:00
parent aebf2220a6
commit 1f55832a6a
3 changed files with 12 additions and 8 deletions

View file

@ -29,9 +29,11 @@ class CameraFrame(Mobject):
# Field of view in the y direction # Field of view in the y direction
fovy: float = 45 * DEGREES, fovy: float = 45 * DEGREES,
euler_axes: str = "zxz", euler_axes: str = "zxz",
# This keeps it ordered first in a scene
z_index=-1,
**kwargs, **kwargs,
): ):
super().__init__(**kwargs) super().__init__(z_index=z_index, **kwargs)
self.uniforms["orientation"] = Rotation.identity().as_quat() self.uniforms["orientation"] = Rotation.identity().as_quat()
self.uniforms["fovy"] = fovy self.uniforms["fovy"] = fovy

View file

@ -94,7 +94,6 @@ class Mobject(object):
self.texture_paths = texture_paths self.texture_paths = texture_paths
self.depth_test = depth_test self.depth_test = depth_test
self.z_index = z_index self.z_index = z_index
self._scene_order = 0
# Internal state # Internal state
self.submobjects: list[Mobject] = [] self.submobjects: list[Mobject] = []
@ -1246,6 +1245,10 @@ class Mobject(object):
def set_z(self, z: float, direction: Vect3 = ORIGIN) -> Self: def set_z(self, z: float, direction: Vect3 = ORIGIN) -> Self:
return self.set_coord(z, 2, direction) 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: def space_out_submobjects(self, factor: float = 1.5, **kwargs) -> Self:
self.scale(factor, **kwargs) self.scale(factor, **kwargs)
for submob in self.submobjects: for submob in self.submobjects:

View file

@ -414,13 +414,12 @@ class Scene(object):
foreground in the order with which they are added. foreground in the order with which they are added.
""" """
self.remove(*new_mobjects) 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 += 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({ self.id_to_mobject_map.update({
id(sm): sm id(sm): sm
for m in new_mobjects for m in new_mobjects