'Manim: creating objects behind existing objects (force z-index at creation time)

I'm using Manim CE 0.8.0, and I'm trying to fade in the axes behind the existing objects in the scene; I found no way to accomplish that. Here's a POC:

from manim import *

class FadeBehind(Scene):
    def construct(self):
        myDot = Dot(
            point = [0, 0, 0],
            radius = 3,
            color = RED,
        )
        self.play(
            FadeIn(myDot),
        )

        myLine = Line(
            start = [-5, 0, 0],
            end = [5, 0, 0],
            stroke_color = BLUE,
            stroke_width = 30,
        )
        myLine.z_index = myDot.z_index - 1
        self.play(
            FadeIn(myLine) # works as expected (the blue line is shown behind the dot)
        )
        self.wait()

        ax = Axes(
            x_range=[-7, 7, 1],
            y_range=[-5, 5, 1],
        )            
        ax.z_index = myLine.z_index - 1
        self.play(
            FadeIn(ax) # doesn't work as expected (the axes are overlayed on top of everything in the scene)
        )


Solution 1:[1]

The problem is, that the default z_index is 0: print(myDot.z_index) gives 0. And z_index have to be positive. Here is the script that works:

class FadeBehind(Scene):
    def construct(self):
        myDot = Dot(
            point = [0, 0, 0],
            radius = 2,
            color = RED,
        )
        self.play(
            FadeIn(myDot),
        )
        myDot.z_index=1
        myLine = Line(
            start = [-5, 0, 0],
            end = [5, 0, 0],
            stroke_color = BLUE,
            stroke_width = 30,
        )
        myLine.z_index = 0
        self.play(
            FadeIn(myLine) # works as expected (the blue line is shown behind the dot)
        )

        ax = Axes(
            x_range=[-7, 7, 1],
            y_range=[-5, 5, 1],
        )            
        ax.z_index = 0
        self.play(
            FadeIn(ax) # now works as expected since lower z-index
        )

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael Scott Asato Cuthbert