Search code examples
pythonmanim

Implementing custom Manim Animation


Manim's Animation class calls the interpolate_submobject method which is implemented in a subclass.

So I implemented this interface/contract:

class ScaleAction(Animation):
    def __init__(
        self,
        mobject: Mobject,
        **kwargs,
    ):
        super().__init__(mobject, **kwargs)
        
    def interpolate_submobject(
        self,
        submobject: Mobject,
        starting_submobject: Mobject,
        alpha: float,
    ):
        def action(array):
            return array *(1*(1-alpha)+2*alpha)
        
        submobject = starting_submobject.copy().apply_function(action)
        return self

However, the animation doesn't end up doing anything. I feel I haven't understood the contract properly. Any thoughts?

You can play around with the code here in binder


Solution

  • The submobject being passed to interpolate_submobject is the one you need to modify directly; reassigning the variable will not change the mobject. By changing your reassignment to

    submobject.become(starting_submobject.copy().apply_function(action))
    

    your animation will work correctly; I've tested it with the simpler toy example

    class ScaleTest(Scene):
        def construct(self):
            s = Square()
            self.play(ScaleAction(s))