mirror of
https://github.com/3b1b/manim.git
synced 2025-09-01 00:48:45 +00:00
Only reconstruct family as needed
This commit is contained in:
parent
e124aecd6b
commit
4b14c11e4b
1 changed files with 23 additions and 20 deletions
|
@ -97,7 +97,7 @@ class Mobject(object):
|
||||||
# Internal state
|
# Internal state
|
||||||
self.submobjects: list[Mobject] = []
|
self.submobjects: list[Mobject] = []
|
||||||
self.parents: list[Mobject] = []
|
self.parents: list[Mobject] = []
|
||||||
self.family: list[Mobject] = [self]
|
self.family: list[Mobject] | None = [self]
|
||||||
self.locked_data_keys: set[str] = set()
|
self.locked_data_keys: set[str] = set()
|
||||||
self.const_data_keys: set[str] = set()
|
self.const_data_keys: set[str] = set()
|
||||||
self.locked_uniform_keys: set[str] = set()
|
self.locked_uniform_keys: set[str] = set()
|
||||||
|
@ -409,23 +409,26 @@ class Mobject(object):
|
||||||
return self.submobjects
|
return self.submobjects
|
||||||
|
|
||||||
@affects_data
|
@affects_data
|
||||||
def assemble_family(self) -> Self:
|
def note_updated_family(self, only_changed_order=False) -> Self:
|
||||||
sub_families = (sm.get_family() for sm in self.submobjects)
|
self.family = None
|
||||||
self.family = [self, *it.chain(*sub_families)]
|
if not only_changed_order:
|
||||||
self.refresh_has_updater_status()
|
self.refresh_has_updater_status()
|
||||||
self.refresh_bounding_box()
|
self.refresh_bounding_box()
|
||||||
for parent in self.parents:
|
for parent in self.parents:
|
||||||
parent.assemble_family()
|
parent.note_updated_family()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def get_family(self, recurse: bool = True) -> list[Mobject]:
|
def get_family(self, recurse: bool = True) -> list[Mobject]:
|
||||||
if recurse:
|
if not recurse:
|
||||||
return self.family
|
|
||||||
else:
|
|
||||||
return [self]
|
return [self]
|
||||||
|
if self.family is None:
|
||||||
|
# Reconstruct and save
|
||||||
|
sub_families = (sm.get_family() for sm in self.submobjects)
|
||||||
|
self.family = [self, *it.chain(*sub_families)]
|
||||||
|
return self.family
|
||||||
|
|
||||||
def family_members_with_points(self) -> list[Self]:
|
def family_members_with_points(self) -> list[Mobject]:
|
||||||
return [m for m in self.family if len(m.data) > 0]
|
return [m for m in self.get_family() if len(m.data) > 0]
|
||||||
|
|
||||||
def get_ancestors(self, extended: bool = False) -> list[Mobject]:
|
def get_ancestors(self, extended: bool = False) -> list[Mobject]:
|
||||||
"""
|
"""
|
||||||
|
@ -456,7 +459,7 @@ class Mobject(object):
|
||||||
self.submobjects.append(mobject)
|
self.submobjects.append(mobject)
|
||||||
if self not in mobject.parents:
|
if self not in mobject.parents:
|
||||||
mobject.parents.append(self)
|
mobject.parents.append(self)
|
||||||
self.assemble_family()
|
self.note_updated_family()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def remove(
|
def remove(
|
||||||
|
@ -472,7 +475,7 @@ class Mobject(object):
|
||||||
if parent in child.parents:
|
if parent in child.parents:
|
||||||
child.parents.remove(parent)
|
child.parents.remove(parent)
|
||||||
if reassemble:
|
if reassemble:
|
||||||
parent.assemble_family()
|
parent.note_updated_family()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def clear(self) -> Self:
|
def clear(self) -> Self:
|
||||||
|
@ -489,12 +492,12 @@ class Mobject(object):
|
||||||
old_submob.parents.remove(self)
|
old_submob.parents.remove(self)
|
||||||
self.submobjects[index] = new_submob
|
self.submobjects[index] = new_submob
|
||||||
new_submob.parents.append(self)
|
new_submob.parents.append(self)
|
||||||
self.assemble_family()
|
self.note_updated_family()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def insert_submobject(self, index: int, new_submob: Mobject) -> Self:
|
def insert_submobject(self, index: int, new_submob: Mobject) -> Self:
|
||||||
self.submobjects.insert(index, new_submob)
|
self.submobjects.insert(index, new_submob)
|
||||||
self.assemble_family()
|
self.note_updated_family()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def set_submobjects(self, submobject_list: list[Mobject]) -> Self:
|
def set_submobjects(self, submobject_list: list[Mobject]) -> Self:
|
||||||
|
@ -606,7 +609,7 @@ class Mobject(object):
|
||||||
self.submobjects.sort(key=submob_func)
|
self.submobjects.sort(key=submob_func)
|
||||||
else:
|
else:
|
||||||
self.submobjects.sort(key=lambda m: point_to_num_func(m.get_center()))
|
self.submobjects.sort(key=lambda m: point_to_num_func(m.get_center()))
|
||||||
self.assemble_family()
|
self.note_updated_family(only_changed_order=True)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def shuffle(self, recurse: bool = False) -> Self:
|
def shuffle(self, recurse: bool = False) -> Self:
|
||||||
|
@ -614,12 +617,12 @@ class Mobject(object):
|
||||||
for submob in self.submobjects:
|
for submob in self.submobjects:
|
||||||
submob.shuffle(recurse=True)
|
submob.shuffle(recurse=True)
|
||||||
random.shuffle(self.submobjects)
|
random.shuffle(self.submobjects)
|
||||||
self.assemble_family()
|
self.note_updated_family(only_changed_order=True)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def reverse_submobjects(self) -> Self:
|
def reverse_submobjects(self) -> Self:
|
||||||
self.submobjects.reverse()
|
self.submobjects.reverse()
|
||||||
self.assemble_family()
|
self.note_updated_family(only_changed_order=True)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
# Copying and serialization
|
# Copying and serialization
|
||||||
|
@ -692,7 +695,7 @@ class Mobject(object):
|
||||||
for attr, value in self.__dict__.items():
|
for attr, value in self.__dict__.items():
|
||||||
if isinstance(value, Mobject) and value is not self:
|
if isinstance(value, Mobject) and value is not self:
|
||||||
if value in family:
|
if value in family:
|
||||||
setattr(result, attr, result.family[self.family.index(value)])
|
setattr(result, attr, result.family[family.index(value)])
|
||||||
elif isinstance(value, np.ndarray):
|
elif isinstance(value, np.ndarray):
|
||||||
setattr(result, attr, value.copy())
|
setattr(result, attr, value.copy())
|
||||||
return result
|
return result
|
||||||
|
|
Loading…
Add table
Reference in a new issue