1

我正在尝试使用 SVG 文件创建一个数字生物。但是当我运行动画并且生物出现时,有一些缺失的部分,我不知道为什么。即使我只是尝试在 SVGMobject 上加载 SVG 文件,它也无法正常工作。

Creature() 是继承 NumberCreature 类功能的子类,只是重命名为 Alex()。

这是python代码:

class NumberCreature(Scene):
def construct(self):       
    creature = Creature()
    self.add(creature)
    self.wait()

这是原始 SVG 代码:https ://codeshare.io/aYM4Mn 这是预期结果: SVG 原始图像

当我运行 python 代码时,这是真正的结果:SVG generated image by manim

我也尝试运行这个 python 代码:

class NumberCreature(Scene):
def construct(self):       
creature = SVGMobject('/home/usr/manim/assets/svg_images/NumberCreature_plain.svg')    
    self.add(creature)
    self.wait()

来自 manim 的结果

如您所见,图像的某些部分丢失了,即使我尝试使用 bio[index].set_color(original_hex_color) 正确着色所有内容,结果也总是与原始结果不同。

请帮助我,我不知道到底发生了什么,我对 manim 还很陌生。

4

1 回答 1

4

如果你打算在 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()

在此处输入图像描述

于 2020-02-03T21:56:41.307 回答