SVGMobject 无法正确显示图像
SVGMobject doesn't display the image correctly
我正在尝试使用 SVG 文件创建数字生物。但是当我 运行 动画和生物出现时,有一些缺失的部分,我不明白为什么。即使我只是尝试将 SVG 文件加载到 SVGMobject 上,它也无法正常工作。
Creature() 是子代 class,它继承了 NumberCreature class 的功能,只是重命名为 Alex()。
这是python代码:
class NumberCreature(Scene):
def construct(self):
creature = Creature()
self.add(creature)
self.wait()
这是原始的 SVG 代码:https://codeshare.io/aYM4Mn
这是预期的结果:
SVG original image
当我运行 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()
results from manim
如您所见,图像的某些部分缺失,即使我尝试使用 creature[index].set_color(original_hex_color) 正确着色所有内容,结果始终是与原版不同。
请帮助我,我不知道到底发生了什么,我还不是 manim 的菜鸟。
如果你打算在 manim 中使用 SVG,建议你学习一些基本的 SVG 课程,因为你肯定会经常遇到这种问题,你可以在 youtube 上找到很多,我建议您从 this.
开始
为了解决您的问题,我创建了一个脚本来分析发生了什么:
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 中删除 样式和颜色,因此 you将不得不手动配置它们,如下所示:
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()
我正在尝试使用 SVG 文件创建数字生物。但是当我 运行 动画和生物出现时,有一些缺失的部分,我不明白为什么。即使我只是尝试将 SVG 文件加载到 SVGMobject 上,它也无法正常工作。
Creature() 是子代 class,它继承了 NumberCreature class 的功能,只是重命名为 Alex()。
这是python代码:
class NumberCreature(Scene):
def construct(self):
creature = Creature()
self.add(creature)
self.wait()
这是原始的 SVG 代码:https://codeshare.io/aYM4Mn 这是预期的结果: SVG original image
当我运行 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()
results from manim
如您所见,图像的某些部分缺失,即使我尝试使用 creature[index].set_color(original_hex_color) 正确着色所有内容,结果始终是与原版不同。
请帮助我,我不知道到底发生了什么,我还不是 manim 的菜鸟。
如果你打算在 manim 中使用 SVG,建议你学习一些基本的 SVG 课程,因为你肯定会经常遇到这种问题,你可以在 youtube 上找到很多,我建议您从 this.
开始为了解决您的问题,我创建了一个脚本来分析发生了什么:
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 中删除 样式和颜色,因此 you将不得不手动配置它们,如下所示:
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()