Search code examples
pythonmanim

How to put one 3D object behind another in manim without it showing through?


When I plot overlapping 3D objects, the object in the background is visible through the one in the foreground despite opacity being set to one. What can I do to prevent it? In the following code, the beginning of the arrow should be hidden by the sphere, but it is clearly visible. How can I get it to work?

I know that with 2D objects, one can change the z axis value of one of them to put it behind the other. However, I'm not aware of a way to do it in 3D scenes.

enter image description here

class TT(ThreeDScene):
    def construct(self):
        s = Sphere(
            center=ORIGIN,
            radius=1,
        ).set_color(YELLOW).set_opacity(1)

        l = Line(start=ORIGIN, end=ORIGIN + RIGHT * 2,
             color=RED, buff=0.05).add_tip(tip_shape=ArrowTriangleFilledTip)
        self.add(l, s)

Solution

  • Adjust the z-index:

    Output

    class TT(ThreeDScene):
        def construct(self):
            s = Sphere(
                center=ORIGIN,
                radius=1,
            ).set_color(YELLOW).set_opacity(1)
    
            l = Line(start=ORIGIN, end=ORIGIN + RIGHT * 2,
                 color=RED, buff=0.05).add_tip(tip_shape=ArrowTriangleFilledTip)
            s.set_z_index(l.z_index + 1)
            self.add(l, s)