Merge pull request #2057 from germanzhu/add-zorder-mobject

add zorder to mobject
This commit is contained in:
Grant Sanderson 2024-09-06 10:11:40 -07:00 committed by GitHub
commit aebf2220a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 0 deletions

View file

@ -86,12 +86,15 @@ class Mobject(object):
# If true, the mobject will not get rotated according to camera position # If true, the mobject will not get rotated according to camera position
is_fixed_in_frame: bool = False, is_fixed_in_frame: bool = False,
depth_test: bool = False, depth_test: bool = False,
z_index: int = 0,
): ):
self.color = color self.color = color
self.opacity = opacity self.opacity = opacity
self.shading = shading self.shading = shading
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._scene_order = 0
# Internal state # Internal state
self.submobjects: list[Mobject] = [] self.submobjects: list[Mobject] = []

View file

@ -414,7 +414,13 @@ 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))
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