无法将SVGMobject添加到场景中

时间:2019-09-08 18:09:03

标签: python xml animation svg manim

我正在尝试将SVG图像添加到场景中。这是我的SVG文件(名为“ AND_DIN”):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="100"
   height="80"
   id="svg2"
   sodipodi:version="0.32"
   inkscape:version="0.46"
   version="1.0"
   sodipodi:docname="AND DIN.svg"
   inkscape:output_extension="org.inkscape.output.svg.inkscape">
  <defs
     id="defs4">
    <inkscape:perspective
       sodipodi:type="inkscape:persp3d"
       inkscape:vp_x="0 : 15 : 1"
       inkscape:vp_y="0 : 1000 : 0"
       inkscape:vp_z="50 : 15 : 1"
       inkscape:persp3d-origin="25 : 10 : 1"
       id="perspective2714" />
    <inkscape:perspective
       sodipodi:type="inkscape:persp3d"
       inkscape:vp_x="0 : 0.5 : 1"
       inkscape:vp_y="0 : 1000 : 0"
       inkscape:vp_z="1 : 0.5 : 1"
       inkscape:persp3d-origin="0.5 : 0.33333333 : 1"
       id="perspective2806" />
    <inkscape:perspective
       id="perspective2819"
       inkscape:persp3d-origin="372.04724 : 350.78739 : 1"
       inkscape:vp_z="744.09448 : 526.18109 : 1"
       inkscape:vp_y="0 : 1000 : 0"
       inkscape:vp_x="0 : 526.18109 : 1"
       sodipodi:type="inkscape:persp3d" />
    <inkscape:perspective
       id="perspective2777"
       inkscape:persp3d-origin="75 : 40 : 1"
       inkscape:vp_z="150 : 60 : 1"
       inkscape:vp_y="0 : 1000 : 0"
       inkscape:vp_x="0 : 60 : 1"
       sodipodi:type="inkscape:persp3d" />
    <inkscape:perspective
       id="perspective3275"
       inkscape:persp3d-origin="50 : 33.333333 : 1"
       inkscape:vp_z="100 : 50 : 1"
       inkscape:vp_y="0 : 1000 : 0"
       inkscape:vp_x="0 : 50 : 1"
       sodipodi:type="inkscape:persp3d" />
    <inkscape:perspective
       id="perspective5533"
       inkscape:persp3d-origin="32 : 21.333333 : 1"
       inkscape:vp_z="64 : 32 : 1"
       inkscape:vp_y="0 : 1000 : 0"
       inkscape:vp_x="0 : 32 : 1"
       sodipodi:type="inkscape:persp3d" />
  </defs>
  <sodipodi:namedview
     id="base"
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1.0"
     inkscape:pageopacity="0.0"
     inkscape:pageshadow="2"
     inkscape:zoom="8"
     inkscape:cx="58.014966"
     inkscape:cy="30.17952"
     inkscape:document-units="px"
     inkscape:current-layer="layer1"
     showgrid="true"
     inkscape:grid-bbox="true"
     inkscape:grid-points="true"
     gridtolerance="10000"
     inkscape:window-width="1399"
     inkscape:window-height="874"
     inkscape:window-x="33"
     inkscape:window-y="0"
     inkscape:snap-bbox="true">
    <inkscape:grid
       id="GridFromPre046Settings"
       type="xygrid"
       originx="0px"
       originy="0px"
       spacingx="1px"
       spacingy="1px"
       color="#0000ff"
       empcolor="#0000ff"
       opacity="0.2"
       empopacity="0.4"
       empspacing="5"
       visible="true"
       enabled="true" />
  </sodipodi:namedview>
  <metadata
     id="metadata7">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1">
    <path
       style="fill:none;stroke:#000000;stroke-width:1.99999976;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
       d="M 70.166665,40 L 95.166663,40"
       id="path3059"
       sodipodi:nodetypes="cc" />
    <path
       style="fill:none;stroke:#000000;stroke-width:2;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
       d="M 35,22 L 4.999999,22"
       id="path3061"
       sodipodi:nodetypes="cc" />
    <path
       style="fill:none;stroke:#000000;stroke-width:2;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
       d="M 35,58 L 5,58"
       id="path3944"
       sodipodi:nodetypes="cc" />
    <path
       style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:3;stroke-linecap:butt;stroke-linejoin:miter;marker:none;stroke-miterlimit:4;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible;enable-background:accumulate"
       d="M 34,3.5 L 34,5 L 34,75 L 34,76.5 L 35.5,76.5 C 55.631594,76.5 72,60.131594 72,40 C 72,19.868406 55.631594,3.5 35.5,3.5 L 34,3.5 z M 37,6.65625 C 54.795557,7.4575527 68.5,22.005837 68.5,40 C 68.5,57.994163 54.795557,72.542447 37,73.34375 L 37,6.65625 z"
       id="path4775"
       sodipodi:nodetypes="cccccscccscc" />
  </g>
</svg>

这是我的场景文件(名为gates.py):

from manimlib.imports import *

class AndGate(SVGMobject):
    CONFIG = {
        "file_name" : "AND_DIN",
        "stroke_width" : 2,
        "fill_opacity" : 0,
        "height" : 0.5,
    }

class Shapes(Scene):
    def construct(self):
        gate = AndGate()
        self.add(gate)

它出现以下错误:

Traceback (most recent call last):
  File "/home/manul/animations/manim/manimlib/extract_scene.py", line 155, in main
    scene = SceneClass(**scene_kwargs)
  File "/home/manul/animations/manim/manimlib/scene/scene.py", line 53, in __init__
    self.construct()
  File "./mytry/gates.py", line 13, in construct
    gate = AndGate()
  File "/home/manul/animations/manim/manimlib/mobject/svg/svg_mobject.py", line 46, in __init__
    VMobject.__init__(self, **kwargs)
  File "/home/manul/animations/manim/manimlib/mobject/mobject.py", line 48, in __init__
    self.generate_points()
  File "/home/manul/animations/manim/manimlib/mobject/svg/svg_mobject.py", line 69, in generate_points
    mobjects = self.get_mobjects_from(svg)
  File "/home/manul/animations/manim/manimlib/mobject/svg/svg_mobject.py", line 87, in get_mobjects_from
    for child in element.childNodes
  File "/home/manul/animations/manim/manimlib/mobject/svg/svg_mobject.py", line 87, in <listcomp>
    for child in element.childNodes
  File "/home/manul/animations/manim/manimlib/mobject/svg/svg_mobject.py", line 81, in get_mobjects_from
    self.update_ref_to_element(element)
  File "/home/manul/animations/manim/manimlib/mobject/svg/svg_mobject.py", line 307, in update_ref_to_element
    new_refs = dict([(e.getAttribute('id'), e) for e in self.get_all_childNodes_have_id(defs)])
TypeError: 'Element' object is not iterable

并非所有图像都出现此错误。例如,此SVG可以工作:

<?xml version="1.0"?><svg width="640" height="480" xmlns="http://www.w3.org/2000/svg">
 <title>AND gate</title>
 <metadata id="metadata7">image/svg+xml</metadata>
 <g>
  <path fill="none" stroke="#000000" stroke-linecap="square" stroke-miterlimit="4" stroke-dashoffset="0" d="m19.370987,20.318518c-4.836363,0 -12.805312,0.000008 -12.805312,0.000008l0,-19.817436l12.805312,-0.001089c4.836365,0 8.76153,4.439348 8.76153,9.90926c0,5.469912 -3.925165,9.909258 -8.76153,9.909258zm8.76153,-9.909258l6.103685,0m-33.736202,-4.954623l6.065714,0m-6.065714,9.909251l6.065714,0" id="path6087"/>
  <path
   style="fill:none;fill-opacity:0.75;fill-rule:evenodd;stroke:black;stroke-width:3.125;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
   d="M 89.111392,80.140217 L 89.111392,276.68403 L 266.56332,276.68403"
   id="path3110" />
  <title>Layer 1</title>
 </g>
</svg>

我已经在互联网上查询了好几次,但我无法弄清楚。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果您从源代码运行manim,请从{p>更改def get_all_childNodes_have_id(self, element):函数中 svg_mobject.py 的第301行

return element

return [element]

可能会解决您的问题。