使用 Manim,我可以在现有对象的背景中绘制新对象吗?

Using Manim, can I draw a new object in the background of existing ones?

我已经说明了由两个向量跨越的平行四边形,并且想在该平行四边形的区域中添加阴影,我尝试这样做:

from manim import *
import numpy as np

class DrawParallelogram( Scene ):
    def construct( self ):
        o = np.array( [ 0, -2, 0 ] )
        p1 = np.array( [ 3, 1, 0 ] ) # u
        p2 = np.array( [ 1, 3, 0 ] ) # v
        op1 = o + p1
        op2 = o + p2
        op3 = o + p1 + p2

        v1 = Arrow( start = o, end = op1, buff = 0, color = RED ) # u
        v2 = Arrow( start = o, end = op2, buff = 0, color = YELLOW ) # v
        v1p = Arrow( start = op2, end = op3, buff = 0, color = RED ) # u'
        v2p = Arrow( start = op1, end = op3, buff = 0, color = YELLOW ) # v'

        parallelogram = [ o, op1, op3, op2 ]
        poly = Polygon( *parallelogram, color = PURPLE, fill_opacity = 0.5 )

        self.play( AnimationGroup( Write( v1 ), Write( v2 ), Write( v1p ), Write( v2p ) ) )
        self.wait( )
        self.play( Write( poly ) )

但是,这个平行四边形会在我已经绘制的箭头上着色,如下所示:

而且我希望它在后台。有没有办法在场景中引入一个新对象,使其在逻辑上位于任何现有对象的后面,就好像我先画了它一样,这样它看起来像:

您可以使用set_z_index方法将平行四边形的z_index 属性设置为小于箭头的值。

这里我设置的比v1的值低:

poly.set_z_index(v1.z_index - 1)

或者你可以直接操作 z_index 属性:

poly.z_index = v1.z_index - 1

使用 set_z_index 方法将是更清洁的解决方案。

完整代码:

from manim import *
import numpy as np

class DrawParallelogram( Scene ):
    def construct( self ):
        o = np.array( [ 0, -2, 0 ] )
        p1 = np.array( [ 3, 1, 0 ] ) # u
        p2 = np.array( [ 1, 3, 0 ] ) # v
        op1 = o + p1
        op2 = o + p2
        op3 = o + p1 + p2

        v1 = Arrow( start = o, end = op1, buff = 0, color = RED ) # u
        v2 = Arrow( start = o, end = op2, buff = 0, color = YELLOW ) # v
        v1p = Arrow( start = op2, end = op3, buff = 0, color = RED ) # u'
        v2p = Arrow( start = op1, end = op3, buff = 0, color = YELLOW ) # v'

        parallelogram = [ o, op1, op3, op2 ]
        poly = Polygon( *parallelogram, color = PURPLE, fill_opacity = 0.5 )
        # Set the z-index
        poly.set_z_index(v1.z_index - 1)

        self.play( AnimationGroup( Write( v1 ), Write( v2 ), Write( v1p ), Write( v2p ) ) )
        self.wait( )
        self.play( Write( poly ) )