ManimCommunity / manim

A community-maintained Python framework for creating mathematical animations.
https://www.manim.community
MIT License
20.06k stars 1.48k forks source link

Improve the SVG parser #31

Closed yoshiask closed 3 years ago

yoshiask commented 4 years ago

There are many weird quirks of the current custom SVG parser. For example, the colors white and black are swapped if the SVG element is a rect. This is some really odd behavior, and I think the best way to solve these issues is to use a full SVG parsing library. Either way, SVG support is pretty limited at the moment. (Also, I think I can add SkewX and SkewY transform support right now)

PgBiel commented 4 years ago

We need full and instantaneous color support for SVGs, i.e., we need to fix those color issues people often face when attempting to work with SVGs in manim.

xy-23 commented 4 years ago

Hi, I was trying to improve the svg parser sometime ago, but during my attempt, I found if we want to improve it (more stable and powerful), we need to deal with some problems first.

  1. We need to let SVGMobject support <text>, <image> and so on. But currently SVGMobject is a subclass of VMobject. I think we should let SVGMobject become a subclass of Group, which I think is more intuitive. But then you will find that Group doesn't support many existingAnimations e.g. Write, and you will also notice that there is a subclass of VMobject called VGroup, so far, I have made a new class called Image which is a subclass of VMobject/.../Rectangle and is able to be animated in those Animations, but I think it is a compromised way. Then we need to dig into the deepest process of manim, if you are interested, please tell me, because I think this is a little offtopic...

  2. We need to PARSE svg in a better way in order to let it support attributes like color, stroke and so on. I found a python package called cairosvg, which does this job almost perfectly, but it can't be used directly in manim, so I think we can probably look into the source code of it and use(copy) some of it such as its class called Tree

Because I'm busy with other things now and don't have much time to do it. If you want to improve it, I hope my experience can help a little...

yoshiask commented 4 years ago
  1. I don't see anything wrong with leaving SVGMobject as a VMobject. Maybe it should be a VGroup, but I don't think it makes much of a difference. Can you elaborate on what you've done with your Image class?
  2. Manim's parser can read those attributes, the problem isn't his parsing method. After all, the data for a path object is stored in an attribute. The problem is that Manim doesn't bother to read them. Adding color and stroke support doesn't require us to switch to a completely different parser.

Also, cairosvg is based on Cairo graphics, which this version of Manim won't use.

xy-23 commented 4 years ago
  1. I don't see anything wrong with leaving SVGMobject as a VMobject. Maybe it should be a VGroup, but I don't think it makes much of a difference. Can you elaborate on what you've done with your Image class?

ImageMobject is not a subclass of VMobject. They are parallel. Currently, you can't do something like this in manim: Transform(ImageMobject(...), VMobject(...)), that's because Transform can't deal with non-VMobject. Making images vectorizable is ridiculous, but we can adjust it's opacity to achieve some creating or decreating animations. The most change I have made is in camera.py to let Camera deal with a special VMobject which has a image_mobject.(Not a elegant way I know, just for testing). And Image is a subclass of Rectangle. Here is a demo. demo Tranform(Text(...), Image(...))

(off-topic) The reason why I want to make those Animations support ImageMobject is that I think the class Animation, Mobject, Sence and Camera are the fundamental classes of manim, and they should be parallel to each other, which means all the Animation can deal with all the Mobject rather than specific Mobject like VMobject. But this is just a dream now, which probably needs to rewrite...

xy-23 commented 4 years ago
  1. Manim's parser can read those attributes, the problem isn't his parsing method. After all, the data for a path object is stored in an attribute. The problem is that Manim doesn't bother to read them. Adding color and stroke support doesn't require us to switch to a completely different parser.

I probably described my opinion wrong. I mean exactly your opinion, that's why we need to take a look at the cairosvg, because its parsing method is excellent and we can use(copy) some of its method...

xy-23 commented 4 years ago

Also, cairosvg is based on Cairo graphics, which this version of Manim won't use.

Yeah, I noticed. But it's irrelevant to this topic I think.

btw(off-topic), I'm also dealing with the issues of Text, which probably end up need manim to switch pycairo into cairocffi.

kilacoda commented 4 years ago

Keep in mind that Image is also a part of PIL, so to minimise confusion, a better name should be chosen imo.

xy-23 commented 4 years ago

Keep in mind that Image is also a part of PIL, so to minimise confusion, a better name should be chosen imo.

I just use the name Image to distinguish the current ImageMobject, but I think using the name Image, Text, Tex, SVG, is simpler and more convince to use from user‘s perspective than ImageMobject, TextMobject, TexMobject, SVGMobject (though they are not the same thing currently).

The confusion of the PIL Image can probably be solved by using PIL.Image.

Maybe we can do something like this:

class ImageMobject():
    def __init__(self):
        pass

and create a abbreviation version

class Image(ImageMobject):
    pass

But this is not very important...

MysaaJava commented 3 years ago

Here is a SVG that cannot be read (I had to compress it to gz to upload it). I think that it is caused by the fading color. But the log seems to say that it is beccause of the groups. Here's the log:

Media will be written to ./media/. You can change this behavior with the --media_dir flag.
/home/mysaa/Videos/Dyschromatopsie/manim/manim/manimlib/mobject/svg/svg_mobject.py:125: UserWarning: g0-71 not recognized
  warnings.warn("%s not recognized" % ref)
/home/mysaa/Videos/Dyschromatopsie/manim/manim/manimlib/mobject/svg/svg_mobject.py:125: UserWarning: g0-109 not recognized
  warnings.warn("%s not recognized" % ref)
/home/mysaa/Videos/Dyschromatopsie/manim/manim/manimlib/mobject/svg/svg_mobject.py:125: UserWarning: g0-75 not recognized
  warnings.warn("%s not recognized" % ref)
/home/mysaa/Videos/Dyschromatopsie/manim/manim/manimlib/mobject/svg/svg_mobject.py:125: UserWarning: g0-66 not recognized
  warnings.warn("%s not recognized" % ref)
/home/mysaa/Videos/Dyschromatopsie/manim/manim/manimlib/mobject/svg/svg_mobject.py:125: UserWarning: g0-277 not recognized
  warnings.warn("%s not recognized" % ref)
/home/mysaa/Videos/Dyschromatopsie/manim/manim/manimlib/mobject/svg/svg_mobject.py:125: UserWarning: g0-96 not recognized
  warnings.warn("%s not recognized" % ref)
/home/mysaa/Videos/Dyschromatopsie/manim/manim/manimlib/mobject/svg/svg_mobject.py:125: UserWarning: g0-50 not recognized
  warnings.warn("%s not recognized" % ref)

File ready at /home/mysaa/Videos/Dyschromatopsie/manim/manim/media/videos/scenes/480p15/IntroScene.mp4

Played 1 animations

lumiere2.svg.gz

leotrs commented 3 years ago

@MysaaJava you seem to be using 3b1b manim, no?

yoshiask commented 3 years ago

@leotrs I told him to put it here because I'm fairly certain the Community version has the same bug.

leotrs commented 3 years ago

Ah thanks

On Wed, Sep 9, 2020, 4:21 PM Yoshi Askharoun notifications@github.com wrote:

@leotrs https://github.com/leotrs I told him to put it here because I'm fairly certain the Community version has the same bug.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ManimCommunity/manim/issues/31#issuecomment-689800553, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAILYAGCMA4F6IX2T3Q7E7LSE7PV3ANCNFSM4NF6AEMQ .

MysaaJava commented 3 years ago

I tried to run on community version but i can't run this version …

leotrs commented 3 years ago

Bummer. Have you tried the instructions? https://manimce.readthedocs.io/en/latest/installation.html

MysaaJava commented 3 years ago

Yes i did, but it fails with ModuleNotFoundError: No module named 'manimlib' … (is it the right place to talk about it ?)

leotrs commented 3 years ago

Right. The problem is that 3b1b/manim and manim CE are not entirely compatible. We still need to write a porting guide. Perhaps the changelog may help for the time being.

In particular, the line from manimlib.imports import * has changed to from manim import *.

But let's discuss on discord.

MysaaJava commented 3 years ago

Well i'm just dumb sorry. The errors still persists with differents number lines:


  warnings.warn("%s not recognized" % ref)
/usr/local/lib/python3.7/dist-packages/manim/mobject/svg/svg_mobject.py:120: UserWarning: g0-109 not recognized
  warnings.warn("%s not recognized" % ref)
/usr/local/lib/python3.7/dist-packages/manim/mobject/svg/svg_mobject.py:120: UserWarning: g0-75 not recognized
  warnings.warn("%s not recognized" % ref)
/usr/local/lib/python3.7/dist-packages/manim/mobject/svg/svg_mobject.py:120: UserWarning: g0-66 not recognized
  warnings.warn("%s not recognized" % ref)
/usr/local/lib/python3.7/dist-packages/manim/mobject/svg/svg_mobject.py:120: UserWarning: g0-277 not recognized
  warnings.warn("%s not recognized" % ref)
/usr/local/lib/python3.7/dist-packages/manim/mobject/svg/svg_mobject.py:120: UserWarning: g0-96 not recognized
  warnings.warn("%s not recognized" % ref)
/usr/local/lib/python3.7/dist-packages/manim/mobject/svg/svg_mobject.py:120: UserWarning: g0-50 not recognized
  warnings.warn("%s not recognized" % ref)```
leotrs commented 3 years ago

Yeah that's what I though. But at least you can now run Manim CE :sweat_smile:

You are not dumb at all! Thanks for reporting this bug.

leotrs commented 3 years ago

As we have currently no one working on improving SVG support, and it is not one of our current priorities, I am closing this issue for the time being.

This issue will be reopened when someone takes on the challenge to improve SVG support.

All issues (open or closed) related to SVG support can be found in the SVG project board.