如果你打算在 manim 中使用 SVG,建议你学习一些基本的 SVG 课程,因为你肯定会经常遇到这种问题,你可以在 youtube 上找到很多,我建议你从这个开始.
解决你的问题,分析发生了什么我创建了一个脚本:
creature = SVGMobject('creature.svg').set_height(FRAME_HEIGHT-1)
index = VGroup()
colors = it.cycle([YELLOW,GREEN,PURPLE,PINK,GRAY,TEAL])
for i in range(len(creature)):
text = Text(f"{i}",color=WHITE,stroke=0,font="Arial").set_height(0.4)
color = next(colors)
creature[i].set_color(color)
creature[i].set_stroke(color,2)
text.next_to(creature[i],RIGHT,buff=0)
index.add(text)
这个脚本向我展示了 svg 的部分数量,我将索引放在每个部分的右侧,如图所示,svg 中有 8 个(从 0 开始)子图,我们可以直观第 0 层和第 7 层是相同的,所以如果我们隐藏第 7 层,给我们:
class NumberCreature(Scene):
def construct(self):
creature = SVGMobject('creature.svg').set_height(FRAME_HEIGHT-1)
index = VGroup()
colors = it.cycle([YELLOW,GREEN,PURPLE,PINK,GRAY,TEAL])
for i in range(len(creature)):
text = Text(f"{i}",color=WHITE,stroke=0,font="Arial").set_height(0.4)
color = next(colors)
creature[i].set_color(color)
creature[i].set_stroke(color,2)
text.next_to(creature[i],RIGHT,buff=0)
index.add(text)
creature.set_stroke(BLUE,2)
creature[-1].fade(1)
self.add(creature,index)
self.wait()
知道了这一点,不难直观地了解正在发生的事情,并且您必须进行设计。
重要提示:默认情况下,manim会从 svg中删除样式和颜色,因此您必须手动配置它们,如下所示:
class NumberCreatureS(Scene):
def construct(self):
creature = SVGMobject('creature.svg').set_height(FRAME_HEIGHT-1)
creature[0].set_style(fill_opacity=1,stroke_width=0,stroke_opacity=0,fill_color=RED_A)
creature[7].set_style(fill_opacity=0,stroke_width=30,stroke_opacity=1,stroke_color=RED)
creature[3].set_stroke(RED,20)
black_rectangle = Rectangle(
width=get_norm(creature[0].get_corner(UL)-creature[0].get_corner(UR)),
height=creature[1].get_height()/2,
fill_opacity=1,
stroke_opacity=0,
fill_color=BLACK,
)
black_rectangle.next_to(creature[0],UP,buff=0.15)
self.add(creature,black_rectangle)
self.wait()