Search code examples
transformationpython-3.8manim

Shifting N objects with Manim at the same time in different directions


I have a list of dots of variable random length and I want to be able to apply a transform (shift in this case) to these objects with independence but at the same time.

list = [Dot(), Dot() ...] # Variable length

I am using the Manim Library by https://github.com/3b1b/manim by 3blue1brown. As a note, other related posts don't solve my problem as they only work with a fix number of objects (dots).


Solution

  • The following code from this reddit post, used as an example, solves the problem:

    import numpy as np
    
    class DotsMoving(Scene):
        def construct(self):
            dots = [Dot() for i in range(5)]
            directions = [np.random.randn(3) for dot in dots]
            self.add(*dots) # It isn't absolutely necessary
            animations = [ApplyMethod(dot.shift,direction) for dot,direction in zip(dots,directions)]
            self.play(*animations) # * -> unpacks the list animations
    

    Special thanks to u/Xorlium.