Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fixed Mobject.copy to be more efficient and fault taulerent

Grant Sanderson6fac1a578c0e
diff --git a/animation/transform.py b/animation/transform.py
index 3391b9ecf..d51755a1e 100644
--- a/animation/transform.py
+++ b/animation/transform.py
@@ -100,7 +100,7 @@ class Swap(CyclicReplace):
class GrowFromPoint(Transform):
def __init__(self, mobject, point, **kwargs):
- target = mobject.deepcopy()
+ target = mobject.copy()
point_mob = Point(point)
mobject.replace(point_mob)
mobject.highlight(point_mob.get_color())
@@ -143,7 +143,7 @@ class ApplyMethod(Transform):
)
assert(isinstance(method.im_self, Mobject))
method_kwargs = kwargs.get("method_kwargs", {})
- target = method.im_self.deepcopy()
+ target = method.im_self.copy()
method.im_func(target, *args, **method_kwargs)
Transform.__init__(self, method.im_self, target, **kwargs)
@@ -165,7 +165,7 @@ class FadeOut(Transform):
class FadeIn(Transform):
def __init__(self, mobject, **kwargs):
- target = mobject.deepcopy()
+ target = mobject.copy()
Transform.__init__(self, mobject, target, **kwargs)
self.starting_mobject.fade(1)
if isinstance(self.starting_mobject, VMobject):
@@ -232,7 +232,6 @@ class Rotate(ApplyMethod):
)
Transform.__init__(self, mobject, target, **kwargs)
-
class ApplyPointwiseFunction(ApplyMethod):
CONFIG = {
"run_time" : DEFAULT_POINTWISE_FUNCTION_RUN_TIME
@@ -258,7 +257,7 @@ class ApplyFunction(Transform):
Transform.__init__(
self,
mobject,
- function(mobject.deepcopy()),
+ function(mobject.copy()),
**kwargs
)
self.name = "ApplyFunctionTo"+str(mobject)
diff --git a/mobject/mobject.py b/mobject/mobject.py
index 3cefce44e..94a83515d 100644
--- a/mobject/mobject.py
+++ b/mobject/mobject.py
@@ -102,13 +102,17 @@ class Mobject(object):
def copy(self):
#TODO, either justify reason for shallow copy, or
#remove this redundancy everywhere
- return self.deepcopy()
- # copy_mobject = copy.copy(self)
- # copy_mobject.points = np.array(self.points)
- # copy_mobject.submobjects = [
- # submob.copy() for submob in self.submobjects
- # ]
- # return copy_mobject
+ # return self.deepcopy()
+ copy_mobject = copy.copy(self)
+ copy_mobject.points = np.array(self.points)
+ copy_mobject.submobjects = [
+ submob.copy() for submob in self.submobjects
+ ]
+ family = self.submobject_family()
+ for attr, value in self.__dict__.items():
+ if isinstance(value, Mobject) and value in family and value is not self:
+ setattr(copy_mobject, attr, value.copy())
+ return copy_mobject
def deepcopy(self):
return copy.deepcopy(self)
@@ -443,7 +447,7 @@ class Mobject(object):
if hasattr(self, "saved_state"):
#Prevent exponential growth of data
self.saved_state = None
- self.saved_state = self.deepcopy()
+ self.saved_state = self.copy()
return self
def restore(self):