manim.mobject.svg.svg_mobject.SVGMobject

class SVGMobject(file_name=None, should_center=True, height=2, width=None, unpack_groups=True, stroke_width=4, fill_opacity=1.0, **kwargs)[source]

Bases: manim.mobject.types.vectorized_mobject.VMobject

A SVGMobject is a Vector Mobject constructed from an SVG (or XDV) file.

SVGMobjects are constructed from the XML data within the SVG file structure. As such, subcomponents from the XML data can be accessed via the submobjects attribute. There is varying amounts of support for SVG elements, experiment with SVG files at your own peril.

Examples

class Sample(Scene):
    def construct(self):
        self.play(FadeIn(SVGMobject("manim-logo-sidebar.svg")))
Parameters

file_name (str) – The file’s path name. When possible, the full path is preferred but a relative path may be used as well. Relative paths are relative to the directory specified by the –assets_dir command line argument.

Other Parameters
  • should_center (bool) – Whether the SVGMobject should be centered to the origin. Defaults to True.

  • height (float) – Specify the final height of the SVG file. Defaults to 2 units.

  • width (float) – Specify the width the SVG file should occupy. Defaults to None.

  • unpack_groups (bool) – Whether the hierarchies of VGroups generated should be flattened. Defaults to True.

  • stroke_width (float) – The stroke width of the outer edge of an SVG path element. Defaults to 4.

  • fill_opacity (float) – Specifies the opacity of the image. 1 is opaque, 0 is transparent. Defaults to 1.

Methods

attribute_to_float

A helper method which converts the attribute to float.

circle_to_mobject

Creates a Circle VMobject from a SVG <circle> command.

ellipse_to_mobject

Creates a stretched Circle VMobject from a SVG <circle> path command.

ensure_valid_file

Reads self.file_name and determines whether the given input file_name is valid.

flatten

A helper method to flatten the input_list into an 1D array.

generate_points

Called by the Mobject abstract base class.

get_mobjects_from

Parses a given SVG element into a Mobject.

handle_transforms

Applies the SVG transform to the specified mobject.

init_colors

Initializes the colors.

line_to_mobject

Creates a Line VMobject from an SVG <line> element.

move_into_position

Uses the SVGMobject’s config dictionary to set the Mobject’s width, height, and/or center it.

path_string_to_mobject

Converts a SVG path element’s d attribute to a mobject.

polygon_to_mobject

Constructs a VMobject from a SVG <polygon> element.

rect_to_mobject

Converts a SVG <rect> command to a VMobject.

use_to_mobjects

Converts a SVG <use> element to a collection of VMobjects.

Attributes

animate

Used to animate the application of a method.

depth

The depth of the mobject.

height

The height of the mobject.

width

The width of the mobject.

attribute_to_float(attr)[source]

A helper method which converts the attribute to float.

Parameters

attr (str) – An SVG path attribute.

Returns

A float representing the attribute string value.

Return type

float

circle_to_mobject(circle_element, style)[source]

Creates a Circle VMobject from a SVG <circle> command.

Parameters
  • circle_element (minidom.Element) – A SVG circle path command.

  • style (dict) – Style specification, using the SVG names for properties.

Returns

A Circle VMobject

Return type

Circle

ellipse_to_mobject(circle_element, style)[source]

Creates a stretched Circle VMobject from a SVG <circle> path command.

Parameters
  • circle_element (minidom.Element) – A SVG circle path command.

  • style (dict) – Style specification, using the SVG names for properties.

Returns

A Circle VMobject

Return type

Circle

ensure_valid_file()[source]

Reads self.file_name and determines whether the given input file_name is valid.

flatten(input_list)[source]

A helper method to flatten the input_list into an 1D array.

generate_points()[source]

Called by the Mobject abstract base class. Responsible for generating the SVGMobject’s points from XML tags, populating self.mobjects, and any submobjects within self.mobjects.

get_mobjects_from(element, inherited_style, within_defs=False)[source]

Parses a given SVG element into a Mobject.

Parameters
  • element (Element) – The SVG data in the XML to be parsed.

  • inherited_style (dict) – Dictionary of the SVG attributes for children to inherit.

  • within_defs (bool) – Whether element is within a defs element, which indicates whether elements with id attributes should be added to the definitions list.

Returns

A VMobject representing the associated SVG element.

Return type

List[VMobject]

handle_transforms(element, mobject)[source]

Applies the SVG transform to the specified mobject. Transforms include: matrix, translate, and scale.

Parameters
  • element (minidom.Element) – The transform command to perform

  • mobject (Mobject) – The Mobject to transform.

init_colors(propagate_colors=False)[source]

Initializes the colors.

Gets called upon creation. This is an empty method that can be implemented by subclasses.

line_to_mobject(line_element, style)[source]

Creates a Line VMobject from an SVG <line> element.

Parameters
  • line_element (minidom.Element) – An SVG line element.

  • style (dict) – Style specification, using the SVG names for properties.

Returns

A Line VMobject

Return type

Line

move_into_position(width, height)[source]

Uses the SVGMobject’s config dictionary to set the Mobject’s width, height, and/or center it. Use width, height, and should_center respectively to modify this.

path_string_to_mobject(path_string, style)[source]

Converts a SVG path element’s d attribute to a mobject.

Parameters
  • path_string (str) – A path with potentially multiple path commands to create a shape.

  • style (dict) – Style specification, using the SVG names for properties.

Returns

A VMobject from the given path string, or d attribute.

Return type

VMobjectFromSVGPathstring

polygon_to_mobject(polygon_element, style)[source]

Constructs a VMobject from a SVG <polygon> element.

Parameters
  • polygon_element (minidom.Element) – An SVG polygon element.

  • style (dict) – Style specification, using the SVG names for properties.

Returns

A VMobject representing the polygon.

Return type

VMobjectFromSVGPathstring

rect_to_mobject(rect_element, style)[source]

Converts a SVG <rect> command to a VMobject.

Parameters
  • rect_element (minidom.Element) – A SVG rect path command.

  • style (dict) – Style specification, using the SVG names for properties.

Returns

Creates either a Rectangle, or RoundRectangle, VMobject from a rect element.

Return type

Rectangle

use_to_mobjects(use_element, local_style)[source]

Converts a SVG <use> element to a collection of VMobjects.

Parameters
  • use_element (MinidomElement) – An SVG <use> element which represents nodes that should be duplicated elsewhere.

  • local_style (Dict) – The styling using SVG property names at the point the element is <use>`d. Not all values are applied; styles defined when the element is specified in the `<def> tag cannot be overriden here.

Returns

A collection of VMobjects that are a copy of the defined object

Return type

List[VMobject]