Fixed ApplyMethod to only create target once the begin method is called

This commit is contained in:
Grant Sanderson 2019-02-08 16:51:26 -08:00
parent eb7f1e21a3
commit 85616f0019

View file

@ -119,33 +119,48 @@ class MoveToTarget(Transform):
class ApplyMethod(Transform): class ApplyMethod(Transform):
CONFIG = { CONFIG = {}
"lag_ratio": 0
}
def __init__(self, method, *args, **kwargs): def __init__(self, method, *args, **kwargs):
""" """
Method is a method of Mobject. *args is for the method, method is a method of Mobject, *args are arguments for
**kwargs is for the transform itself. that method. Key word arguments should be passed in
as the last arg, as a dict, since **kwargs is for
configuration of the transform itslef
Relies on the fact that mobject methods return the mobject Relies on the fact that mobject methods return the mobject
""" """
self.check_validity_of_input(method)
self.method = method
self.method_args = args
# This will be replaced
temp_target = method.__self__
Transform.__init__(self, method.__self__, temp_target, **kwargs)
def check_validity_of_input(self, method):
if not inspect.ismethod(method): if not inspect.ismethod(method):
raise Exception( raise Exception(
"Whoops, looks like you accidentally invoked " "Whoops, looks like you accidentally invoked "
"the method you want to animate" "the method you want to animate"
) )
assert(isinstance(method.__self__, Mobject)) assert(isinstance(method.__self__, Mobject))
args = list(args) # So that args.pop() works
if "method_kwargs" in kwargs: def begin(self):
method_kwargs = kwargs["method_kwargs"] self.target_mobject = self.create_target()
elif len(args) > 0 and isinstance(args[-1], dict): super().begin()
def create_target(self):
method = self.method
# Make sure it's a list so that args.pop() works
args = list(self.method_args)
if len(args) > 0 and isinstance(args[-1], dict):
method_kwargs = args.pop() method_kwargs = args.pop()
else: else:
method_kwargs = {} method_kwargs = {}
target = method.__self__.copy() target = method.__self__.copy()
method.__func__(target, *args, **method_kwargs) method.__func__(target, *args, **method_kwargs)
Transform.__init__(self, method.__self__, target, **kwargs) return target
class ApplyPointwiseFunction(ApplyMethod): class ApplyPointwiseFunction(ApplyMethod):