Search code examples
pythonanimationmanim

Animate multiple Lambda-functions at once in Manim


I want to animate three Values all dependent on the ValueTracker k.

class ValueTrackers(Scene):
    def construct(self):
        k = ValueTracker(3.5)
        num1 = always_redraw(lambda : DecimalNumber().set_value(k.get_value()))
        num2 = always_redraw(lambda : DecimalNumber().set_value(k.get_value())).to_edge(LEFT)
        num3 = always_redraw(lambda : DecimalNumber().set_value(k.get_value())).to_edge(RIGHT)
        self.play(Create(VGroup(num1, num2, num3)))
        self.wait()
        self.play(k.animate.set_value(0), run_time=3)

When I tried it like this all three Variables appeared next to each other (as intended), but when k is being changed all but the first Value disappear. Where is my mistake?


Solution

  • I suggest doing it this way:

    class ValueTrackers(Scene):
    def construct(self):
        k = ValueTracker(3.5)
    
        updateFunction = lambda d: d.set_value(k.get_value())
        num1 = DecimalNumber().add_updater(updateFunction)
        num2 = DecimalNumber().add_updater(updateFunction).to_edge(LEFT)
        num3 = DecimalNumber().add_updater(updateFunction).to_edge(RIGHT)
    
        self.play(Create(VGroup(num1, num2, num3)))
        self.wait()
        self.play(k.animate.set_value(0), run_time=3)