Home > database >  manim set_z_index with value_tracker
manim set_z_index with value_tracker

Time:02-16

The value_tracker seems to not take into account the z.index for a small amount of time at the end when changing its values and I don't really understand why. I've deliberately added lots of set_z_index(1) to try and solve it but nothing did. Here is a sort of minimalist example of the problem:

from manim import *    
class RatioHF(MovingCameraScene):
        def bar_mouvante(self, x_position, y_position, pourcentage_homme):
            rectangle_de_base = \
                Rectangle(height=1, width=4,
                          stroke_color=BLACK).move_to([x_position, y_position, 0]).set_fill(PINK, opacity=0.8)
    
            remplissage_de_base = \
                Rectangle(height=1, width=2,
                          stroke_color=BLACK).move_to([x_position-1, y_position, 0]).set_fill(BLUE, opacity=0.8)
    
            tracker = ValueTracker(0.5).set_z_index(1)
            pourcentage_de_base = DecimalNumber(0.5, color=BLACK).move_to(remplissage_de_base.get_critical_point(LEFT)  
                                                                          np.array([0.5, 0, 0])).set_z_index(1)
            pourcentage_de_base.add_updater(lambda m: m.set_value(tracker.get_value())).set_z_index(1)
    
            self.play(FadeIn(rectangle_de_base, remplissage_de_base, pourcentage_de_base))
    
            self.play(
                AnimationGroup(
                    remplissage_de_base.animate.become(
                        Rectangle(height=remplissage_de_base.height,
                                  width=pourcentage_homme*4,
                                  stroke_color=BLACK).align_to(rectangle_de_base, LEFT UP).set_fill(BLUE, opacity=0.8)
                    ),
                    tracker.animate.set_value(pourcentage_homme).set_z_index(1)
                )
            )
    
    
            self.wait()
    
    
        def construct(self):
            self.wait()
            self.bar_mouvante(0, 0, 0.8)
            self.wait()

CodePudding user response:

AnimationGroup does some very weird things from time to time. In this particular instance, it messes with the rendered mobject order (which is a particularly common side effect, unfortunately). If you don't absolutely have to use it, avoid it.

In this case, you can simply change your play call to

        self.play(
            remplissage_de_base.animate.become(
                Rectangle(height=remplissage_de_base.height,
                            width=pourcentage_homme*4,
                            stroke_color=BLACK).align_to(rectangle_de_base, LEFT UP).set_fill(BLUE, opacity=0.8)
            ),
            tracker.animate.set_value(pourcentage_homme)
        )

and it should work.

Note that setting the z_index of the ValueTracker will not do anything; the ValueTracker is an invisible auxiliary mobject; the actually relevant mobject is the DecimalNumber.

  • Related