mirror of
https://github.com/3b1b/manim.git
synced 2025-08-05 16:49:03 +00:00
Updates to copying based on pickle serializing
This commit is contained in:
parent
c04615c4e9
commit
fe3e10acd2
1 changed files with 46 additions and 34 deletions
|
@ -462,32 +462,21 @@ class Mobject(object):
|
||||||
self.assemble_family()
|
self.assemble_family()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
# Creating new Mobjects from this one
|
# Copying and serialization
|
||||||
|
|
||||||
def replicate(self, n: int) -> Group:
|
def serialize(self):
|
||||||
return self.get_group_class()(
|
pre, self.parents = self.parents, []
|
||||||
*(self.copy() for x in range(n))
|
result = pickle.dumps(self)
|
||||||
)
|
self.parents = pre
|
||||||
|
return result
|
||||||
def get_grid(self, n_rows: int, n_cols: int, height: float | None = None, **kwargs):
|
|
||||||
"""
|
|
||||||
Returns a new mobject containing multiple copies of this one
|
|
||||||
arranged in a grid
|
|
||||||
"""
|
|
||||||
grid = self.replicate(n_rows * n_cols)
|
|
||||||
grid.arrange_in_grid(n_rows, n_cols, **kwargs)
|
|
||||||
if height is not None:
|
|
||||||
grid.set_height(height)
|
|
||||||
return grid
|
|
||||||
|
|
||||||
# Copying
|
|
||||||
|
|
||||||
def copy(self):
|
def copy(self):
|
||||||
self.parents = []
|
|
||||||
try:
|
try:
|
||||||
return pickle.loads(pickle.dumps(self))
|
serial = self.serialize()
|
||||||
|
return pickle.loads(serial)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return copy.deepcopy(self)
|
return copy.deepcopy(self)
|
||||||
|
return result
|
||||||
|
|
||||||
def deepcopy(self):
|
def deepcopy(self):
|
||||||
# This used to be different from copy, so is now just here for backward compatibility
|
# This used to be different from copy, so is now just here for backward compatibility
|
||||||
|
@ -513,7 +502,7 @@ class Mobject(object):
|
||||||
self.become(self.saved_state)
|
self.become(self.saved_state)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def save_to_file(self, file_path):
|
def save_to_file(self, file_path: str):
|
||||||
if not file_path.endswith(".mob"):
|
if not file_path.endswith(".mob"):
|
||||||
file_path += ".mob"
|
file_path += ".mob"
|
||||||
if os.path.exists(file_path):
|
if os.path.exists(file_path):
|
||||||
|
@ -521,7 +510,7 @@ class Mobject(object):
|
||||||
if cont != "y":
|
if cont != "y":
|
||||||
return
|
return
|
||||||
with open(file_path, "wb") as fp:
|
with open(file_path, "wb") as fp:
|
||||||
pickle.dump(self, fp)
|
fp.write(self.serialize())
|
||||||
log.info(f"Saved mobject to {file_path}")
|
log.info(f"Saved mobject to {file_path}")
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
@ -534,6 +523,41 @@ class Mobject(object):
|
||||||
mobject = pickle.load(fp)
|
mobject = pickle.load(fp)
|
||||||
return mobject
|
return mobject
|
||||||
|
|
||||||
|
def become(self, mobject: Mobject):
|
||||||
|
"""
|
||||||
|
Edit all data and submobjects to be idential
|
||||||
|
to another mobject
|
||||||
|
"""
|
||||||
|
self.align_family(mobject)
|
||||||
|
for sm1, sm2 in zip(self.get_family(), mobject.get_family()):
|
||||||
|
sm1.set_data(sm2.data)
|
||||||
|
sm1.set_uniforms(sm2.uniforms)
|
||||||
|
sm1.shader_folder = sm2.shader_folder
|
||||||
|
sm1.texture_paths = sm2.texture_paths
|
||||||
|
sm1.depth_test = sm2.depth_test
|
||||||
|
sm1.render_primitive = sm2.render_primitive
|
||||||
|
self.refresh_shader_wrapper_id()
|
||||||
|
self.refresh_bounding_box(recurse_down=True)
|
||||||
|
return self
|
||||||
|
|
||||||
|
# Creating new Mobjects from this one
|
||||||
|
|
||||||
|
def replicate(self, n: int) -> Group:
|
||||||
|
serial = self.serialize()
|
||||||
|
group_class = self.get_group_class()
|
||||||
|
return group_class(*(pickle.loads(serial) for _ in range(n)))
|
||||||
|
|
||||||
|
def get_grid(self, n_rows: int, n_cols: int, height: float | None = None, **kwargs) -> Group:
|
||||||
|
"""
|
||||||
|
Returns a new mobject containing multiple copies of this one
|
||||||
|
arranged in a grid
|
||||||
|
"""
|
||||||
|
grid = self.replicate(n_rows * n_cols)
|
||||||
|
grid.arrange_in_grid(n_rows, n_cols, **kwargs)
|
||||||
|
if height is not None:
|
||||||
|
grid.set_height(height)
|
||||||
|
return grid
|
||||||
|
|
||||||
# Updating
|
# Updating
|
||||||
|
|
||||||
def init_updaters(self):
|
def init_updaters(self):
|
||||||
|
@ -1521,18 +1545,6 @@ class Mobject(object):
|
||||||
"""
|
"""
|
||||||
pass # To implement in subclass
|
pass # To implement in subclass
|
||||||
|
|
||||||
def become(self, mobject: Mobject):
|
|
||||||
"""
|
|
||||||
Edit all data and submobjects to be idential
|
|
||||||
to another mobject
|
|
||||||
"""
|
|
||||||
self.align_family(mobject)
|
|
||||||
for sm1, sm2 in zip(self.get_family(), mobject.get_family()):
|
|
||||||
sm1.set_data(sm2.data)
|
|
||||||
sm1.set_uniforms(sm2.uniforms)
|
|
||||||
self.refresh_bounding_box(recurse_down=True)
|
|
||||||
return self
|
|
||||||
|
|
||||||
# Locking data
|
# Locking data
|
||||||
|
|
||||||
def lock_data(self, keys: Iterable[str]):
|
def lock_data(self, keys: Iterable[str]):
|
||||||
|
|
Loading…
Add table
Reference in a new issue