add zorder to mobject

This commit is contained in:
german2020 2023-08-27 15:35:22 +08:00
parent d8428585f8
commit 0b5e9d4a8b
2 changed files with 9 additions and 0 deletions

View file

@ -83,6 +83,7 @@ class Mobject(object):
# If true, the mobject will not get rotated according to camera position
is_fixed_in_frame: bool = False,
depth_test: bool = False,
zorder: int = 0,
):
self.color = color
self.opacity = opacity
@ -90,6 +91,8 @@ class Mobject(object):
self.texture_paths = texture_paths
self._is_fixed_in_frame = is_fixed_in_frame
self.depth_test = depth_test
self.zorder = zorder
self._scene_order = 0
# Internal state
self.submobjects: list[Mobject] = []

View file

@ -414,7 +414,13 @@ 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.zorder,m._scene_order))
self.id_to_mobject_map.update({
id(sm): sm
for m in new_mobjects