首页 > 解决方案 > Manim:在现有对象后面创建对象(在创建时强制 z-index)

问题描述

我正在使用 Manim CE 0.8.0,并且试图淡入场景中现有对象后面的轴;我发现没有办法做到这一点。这是一个 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)
        )

标签: pythonmanim

解决方案


问题是,默认 z_index 为 0:print(myDot.z_index)给出 0。z_index 必须为正。这是有效的脚本:

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) # doesn't work as expected (the axes are overlayed on top of everything in the scene)
        )

在此处输入图像描述


推荐阅读