inkex.elements._base module#

Provide extra utility to each svg element type specific to its type.

This is useful for having a common interface for each element which can give path, transform, and property access easily.

class inkex.elements._base.BaseElement[source]#

Bases: inkex.interfaces.IElement.IBaseElement

Provide automatic namespaces to all calls

classmethod is_class_element(elem: lxml.etree.Element) bool[source]#

Hook to do more restrictive check in addition to (ns,tag) match

New in version 1.2: The function has been made public.

tag_name = ''#
property TAG#

Return the tag_name without NS

classmethod new(*children, **attrs)[source]#

Create a new element, converting attrs values to strings.

property NAMESPACE#

Get namespace of element

PARSER = <lxml.etree.XMLParser object>#

A reference to the inkex.elements._parser.SVG_PARSER

WRAPPED_ATTRS: Tuple[Tuple[Any, ...], ...] = (('transform', <class 'inkex.transforms.Transform'>), ('style', <class 'inkex.styles.Style'>), ('classes', 'class', <class 'inkex.styles.Classes'>))#

A list of attributes that are automatically converted to objects.

property wrapped_attrs#

Map attributes to property name and wrapper class

property wrapped_props#

Map properties to attribute name and wrapper class

property typename#

Type name of the element

property xml_path#

XPath representation of the element in its tree

New in version 1.1.

property desc#

The element’s long-form description (for accessibility purposes)

New in version 1.1.

property title#

The element’s short-form description (for accessibility purposes)

New in version 1.1.

_root: Optional[inkex.interfaces.IElement.ISVGDocumentElement] = None#
get(attr, default=None)[source]#

Get element attribute named, with addNS support.

set(attr, value)[source]#

Set element attribute named, with addNS support

update(**kwargs)[source]#

Update element attributes using keyword arguments

Note: double underscore is used as namespace separator, i.e. “namespace__attr” argument name will be treated as “namespace:attr”

Parameters

kwargs – dict with name=value pairs

Returns

self

pop(attr, default=None)[source]#

Delete/remove the element attribute named, with addNS support.

add(child1: inkex.elements._base.BaseElement, child2: inkex.elements._base.BaseElement, *children: inkex.elements._base.BaseElement) Tuple[inkex.elements._base.BaseElement][source]#
add(child: inkex.elements._base.T) inkex.elements._base.T

Like append, but will do multiple children and will return children or only child

tostring()[source]#

Return this element as it would appear in an svg document

set_random_id(prefix: Optional[str] = None, size: Optional[int] = None, backlinks: bool = False, blacklist: Optional[List[str]] = None)[source]#

Sets the id attribute if it is not already set.

The id consists of a prefix and an appended random integer of length size. :param prefix: the prefix of the new ID. Defaults to the tag name. :type prefix: str, optional :param size: number of digits of the second part of the

id. If None, the length is chosen based on the amount of existing objects. Defaults to None.

Changed in version 1.2: The default of this value has been changed from 4 to None.

Parameters
  • backlinks (bool, optional) – Whether to update the links in existing objects that reference this element. Defaults to False.

  • blacklist (List[str], optional) –

    An additional list of ids that are not allowed to be used. This is useful when bulk inserting objects. Defaults to None.

    New in version 1.2.

set_random_ids(prefix: Optional[str] = None, levels: int = - 1, backlinks: bool = False, blacklist: Optional[List[str]] = None)[source]#

Same as set_random_id, but will apply also to children

The id consists of a prefix and an appended random integer of length size. :param prefix: the prefix of the new ID. Defaults to the tag name. :type prefix: str, optional :param levels: the depth of the tree traversion, if negative, no

limit is imposed. Defaults to -1.

Parameters
  • backlinks (bool, optional) – Whether to update the links in existing objects that reference this element. Defaults to False.

  • blacklist (List[str], optional) –

    An additional list of ids that are not allowed to be used. This is useful when bulk inserting objects. Defaults to None.

    New in version 1.2.

property eid#

Property to access the element’s id; will set a new unique id if not set.

get_id(as_url=0) str[source]#

Get the id for the element, will set a new unique id if not set.

as_url - If set to 1, returns #{id} as a string

If set to 2, returns url(#{id}) as a string

Parameters

as_url (int, optional) –

  • If set to 1, returns #{id} as a string

  • If set to 2, returns url(#{id}) as a string.

Defaults to 0.

New in version 1.1.

Returns

formatted id

Return type

str

set_id(new_id, backlinks=False)[source]#

Set the id and update backlinks to xlink and style urls if needed

property root: inkex.interfaces.IElement.ISVGDocumentElement#

Get the root document element from any element descendent

get_or_create(xpath, nodeclass=None, prepend=False)[source]#

Get or create the given xpath, pre/append new node if not found.

Changed in version 1.1: The nodeclass attribute is optional; if not given, it is looked up using find_class()

descendants()[source]#

Walks the element tree and yields all elements, parent first

Changed in version 1.1: The *types attribute was removed

ancestors(elem=None, stop_at=())[source]#

Walk the parents and yield all the ancestor elements, parent first

Parameters
  • elem (BaseElement, optional) –

    If provided, it will stop at the last common ancestor. Defaults to None.

    New in version 1.1.

  • stop_at (tuple, optional) –

    If provided, it will stop at the first parent that is in this list. Defaults to ().

    New in version 1.1.

Returns

list of ancestors

Return type

ElementList

_ancestors(elem, stop_at)[source]#

Get elements which link back to this element, like ancestors but via xlinks

xpath(pattern, namespaces=NSS)[source]#

Wrap xpath call and add svg namespaces

findall(pattern, namespaces=NSS)[source]#

Wrap findall call and add svg namespaces

findone(xpath)[source]#

Gets a single element from the given xpath or returns None

delete()[source]#

Delete this node from it’s parent node

remove_all(*types)[source]#

Remove all children or child types

New in version 1.1.

replace_with(elem)[source]#

Replace this element with the given element

copy()[source]#

Make a copy of the element and return it

duplicate()[source]#

Like copy(), but the copy stays in the tree and sets a random id on the duplicate.

Changed in version 1.2: A random id is also set on all the duplicate’s descendants

property href#

Returns the referred-to element if available

Changed in version 1.1: A setter for href was added.

property label#

Returns the inkscape label

is_sensitive()[source]#

Return true if this element is sensitive in inkscape

New in version 1.1.

set_sensitive(sensitive=True)[source]#

Set the sensitivity of the element/layer

New in version 1.1.

property unit#

Return the unit being used by the owning document, cached

New in version 1.1.

static to_dimensional(value, to_unit='px')[source]#

Convert a value given in user units (px) the given unit type

New in version 1.2.

static to_dimensionless(value)[source]#

Convert a length value into user units (px)

New in version 1.2.

uutounit(value, to_unit='px')[source]#

Convert a unit value to a given unit. If the value does not have a unit, “Document” units are assumed. “Document units” are an Inkscape-specific concept. For most use-cases, to_dimensional() is more appropriate.

New in version 1.1.

unittouu(value)[source]#

Convert a unit value into document units. “Document unit” is an Inkscape-specific concept. For most use-cases, viewport_to_unit() (when the size of an object given in viewport units is needed) or to_dimensionless() (when the equivalent value without unit is needed) is more appropriate.

New in version 1.1.

unit_to_viewport(value, unit='px')[source]#

Converts a length value to viewport units, as defined by the width/height element on the root (i.e. applies the equivalent transform of the viewport)

New in version 1.2.

viewport_to_unit(value, unit='px')[source]#

Converts a length given on the viewport to the specified unit in the user coordinate system

New in version 1.2.

add_unit(value)[source]#

Add document unit when no unit is specified in the string.

New in version 1.1.

cascaded_style()[source]#

Returns the cascaded style of an element (all rules that apply the element itself), based on the stylesheets, the presentation attributes and the inline style using the respective specificity of the style.

see https://www.w3.org/TR/CSS22/cascade.html#cascading-order

New in version 1.2.

Returns

the cascaded style

Return type

Style

specified_style()[source]#

Returns the specified style of an element, i.e. the cascaded style + inheritance, see https://www.w3.org/TR/CSS22/cascade.html#specified-value.

Returns

the specified style

Return type

Style

New in version 1.2.

presentation_style()[source]#

Return presentation attributes of an element as style

New in version 1.2.

composed_transform(other=None)[source]#

Calculate every transform down to the other element if none specified the transform is to the root document element

_add_to_tree_callback(element)[source]#
static _remove_from_tree_callback(oldtree, element)[source]#
__element_adder(element: inkex.elements._base.BaseElement, add_func: Callable[[inkex.elements._base.BaseElement], None])#
addnext(self, element)[source]#

Adds the element as a following sibling directly after this element.

This is normally used to set a processing instruction or comment after the root node of a document. Note that tail text is automatically discarded when adding at the root level.

addprevious(self, element)[source]#

Adds the element as a preceding sibling directly before this element.

This is normally used to set a processing instruction or comment before the root node of a document. Note that tail text is automatically discarded when adding at the root level.

append(self, element)[source]#

Adds a subelement to the end of this element.

clear(self, keep_tail=False)[source]#

Resets an element. This function removes all subelements, clears all attributes and sets the text and tail properties to None.

Pass keep_tail=True to leave the tail text untouched.

extend(self, elements)[source]#

Extends the current children by the elements in the iterable.

insert(self, index, element)[source]#

Inserts a subelement at the given position in this element

remove(self, element)[source]#

Removes a matching subelement. Unlike the find methods, this method compares elements based on identity, not on tag value or contents.

replace(self, old_element, new_element)[source]#

Replaces a subelement with the element passed as second argument.

_abc_impl = <_abc._abc_data object>#
description(**kwargs)#

Use elem.desc = value

Deprecated since version 1.1.

class inkex.elements._base.ShapeElement[source]#

Bases: inkex.elements._base.BaseElement

Elements which have a visible representation on the canvas

_abc_impl = <_abc._abc_data object>#
composed_style(**kwargs)#
Calculate the final styles applied to this element

This function has been deprecated in favor of BaseElement.specified_style()

Deprecated since version 1.2.

property path: inkex.paths.path.Path#

Gets the outline or path of the element, this may be a simple bounding box

property clip#

Gets the clip path element (if any). May be set through CSS.

New in version 1.1.

get_path() inkex.paths.path.Path[source]#

Generate a path for this object which can inform the bounding box

set_path(path)[source]#

Set the path for this object (if possible)

to_path_element()[source]#

Replace this element with a path element

effective_style()[source]#

Without parent styles, what is the effective style is

bounding_box(transform: Optional[inkex.transforms.Transform] = None) Optional[inkex.transforms.BoundingBox][source]#

BoundingBox of the shape

Changed in version 1.1: result adjusted for element’s clip path if applicable.

shape_box(transform: Optional[inkex.transforms.Transform] = None) Optional[inkex.transforms.BoundingBox][source]#

BoundingBox of the unclipped shape

New in version 1.1: Previous bounding_box() function, returning the bounding box without computing the effect of a possible clip.

is_visible()[source]#

Returns false if this object is invisible

Changed in version 1.3: rely on cascaded_style() to include CSS and presentation attributes include visibility attribute with check for inherit include ancestors

New in version 1.1.

_is_visible(inherit_visibility=True)[source]#
get_line_height_uu()[source]#

Returns the specified value of line-height, in user units

New in version 1.1.

class inkex.elements._base.ViewboxMixin[source]#

Bases: object

Mixin for elements with viewboxes, such as <svg>, <marker>

parse_viewbox(vbox: Optional[str]) Optional[List[float]][source]#

Parses a viewbox. If an error occurs during parsing, (0, 0, 0, 0) is returned. If the viewbox is None, None is returned.

New in version 1.3.