inkscape - Inkscape extension package

inkscape.inkext

Inkscape extension boilerplate class.

class inkscape.inkext.ExtOption(*opts, **attrs)[source]

Subclass of optparse.Option that adds additional type checkers for handling Inkscape-specific types. This should be used in lieu of optparse.Option for Inkscape extensions.

class inkscape.inkext.InkscapeExtension[source]

Base class for Inkscape extensions. This does not depend on Inkscape being installed and can be invoked as a stand-alone application. If an input document is not specified a new blank SVG document will be created.

This replaces inkex.Effect which ships with Inkscape.

See also

inkex.Effect

options = None

Parsed command line option values available to the extension

svg = None

SVG context for this extension

debug_svg = None

Debug SVG context if a debug layer has been created

main(optionspec=None, flip_debug_layer=False, debug_layer_name=u'inkext_debug')[source]

Main entry point for the extension.

Parameters:
  • optionspec – An optional list of optarg.Option objects.
  • flip_debug_layer – Flip the Y axis of the debug layer. This is useful if the GUI coordinate origin is at the bottom left. Default is False.
run()[source]

Extensions override this method to do the actual work.

get_elements(selected_only=False)[source]

Get selected document elements.

Tries to get selected elements first. If nothing is selected and selected_only is False then <strike>either the currently selected layer or</strike> the document root is returned. The elements may or may not be visible.

Parameters:selected_only – Get selected elements only. Default is False.
Returns:A (possibly empty) iterable collection of elements.
errormsg(msg)[source]

Intended for end-user-visible error messages. Inkscape displays stderr output with an error dialog.

create_log(log_path=None, log_level=u'DEBUG')[source]

Create a log file for debug output.

Parameters:
  • log_path – Path to log file. If None or empty the log path name will be the command line invocation name (argv[0]) with a ‘.log’ suffix in the user’s home directory.
  • log_level – Log level: ‘DEBUG’, ‘INFO’, ‘WARNING’, ‘ERROR’, or ‘CRITICAL’. Default is ‘DEBUG’.

inkscape.inksvg

A simple library for SVG output - but more Inkscape-centric.

inkscape.inksvg.inkscape_ns(tag)[source]

Prepend the inkscape namespace to an element tag.

inkscape.inksvg.sodipodi_ns(tag)[source]

Prepend the sodipodi namespace to an element tag.

inkscape.inksvg.utlco_ns(tag)[source]

Prepend the utlco namespace to an element tag

class inkscape.inksvg.InkscapeSVGContext(document, *args, **kwargs)[source]
doc_name = None

Inkscape document name

doc_units = None

Inkscape GUI document units

cliprect = None

Document clipping rectangle

get_document_size()[source]

Return width and height of document in user units as a tuple (W, H).

margin_cliprect(mtop, *args)[source]

Create a clipping rectangle based on document bounds with the specified margins. Margin argument order follows CSS margin property rules.

Parameters:
  • mtop – Top margin (user units)
  • mright – Right margin (user units)
  • mbottom – Bottom margin (user units)
  • mleft – Left margin (user units)
Returns:

A geom.Box clipping rectangle

get_document_name()[source]

Return the name of this document. Default is ‘untitled’.

get_document_units()[source]

Return the Inkscape document unit string (‘in’, ‘mm’, etc.). This is the document unit used for the UI (dialogs etc.). It might not be the same as the document user unit.

get_selected_layer()[source]

Get the currently selected Inkscape layer element.

Returns:The currently selected layer element or None if no layers are selected.
find_layer(layer_name)[source]

Find an Inkscape layer by Inkscape layer name.

If there is more than one layer by that name then just the first one will be returned.

Parameters:layer_name – The Inkscape layer name to find.
Returns:The layer Element node or None.
create_layer(name, opacity=None, clear=True, incr_suffix=False, flipy=False, tag=None)[source]

Create an Inkscape layer or return an existing layer.

Parameters:
  • name – The name of the layer to create.
  • opacity – Layer opacity (0.0 to 1.0).
  • clear – If a layer of the same name already exists then erase it first if True otherwise just return it. Default is True.
  • incr_suffix – If a layer of the same name already exists and it is non-empty then add an auto-incrementing numeric suffix to the name (overrides clear).
  • flipy – Add transform to flip Y axis.
  • tag (str) – A layer tag added as an extended attribute. Uses utlco namespace. This can be used to tag layers with a custom label.
Returns:

A new layer or an existing layer of the same name.

set_layer_name(layer, name)[source]

Rename an Inkscape layer.

get_layer_name(layer)[source]

Return the name of the Inkscape layer.

get_parent_layer(node)[source]

Return the layer that the node resides in. Returns None if the node is not in a layer.

layer_is_locked(layer)[source]
Returns:True if the layer is locked, otherwise False.
find(path)[source]

Find an element in the current document.

Parameters:path – XPath path.
Returns:The first matching element or None if not found.
get_visible_layers()[source]

Get a list of visible layers

get_layer_elements(layer)[source]

Get document elements by layer.

Returns all the visible child elements of the given layer.

Parameters:layer – The layer root element.
Returns:A (possibly empty) list of visible elements.
is_layer(node)[source]

Determine if the element is an Inkscape layer node.

get_shape_elements(rootnode, shapetags=(u'path', u'rect', u'line', u'circle', u'ellipse', u'polyline', u'polygon'), parent_transform=None, skip_layers=None, accumulate_transform=True)[source]

Traverse a tree of SVG nodes and flatten it to a list of tuples containing an SVG shape element and its accumulated transform.

This does a depth-first traversal of <g> and <use> elements.

Hidden elements are ignored.

Parameters:
  • rootnode – The root of the node tree to traverse and flatten. This can be the document root, a layer, or simply a list of element nodes.
  • shapetags – List of shape element tags that can be fetched. Default is (‘path’, ‘rect’, ‘line’, ‘circle’, ‘ellipse’, ‘polyline’, ‘polygon’). Anything else is ignored.
  • parent_transform – Transform matrix to add to each node’s transforms. If None the node’s parent transform is used.
  • skip_layers – A list of layer names (as regexes) to ignore
  • accumulate_transform – Apply parent transform(s) to element node if True. Default is True.
Returns:

A possibly empty list of 2-tuples consisting of SVG element and accumulated transform.

inkscape.inksvg.create_inkscape_document(width, height, doc_units=u'px', doc_id=None, doc_name=None, layer_name=None, layer_id=u'defaultlayer')[source]

Create a minimal Inkscape-compatible SVG document.

Parameters:
  • width – The width of the document in user units.
  • height – The height of the document in user units.
  • doc_units – The user unit type (i.e. ‘in’, ‘mm’, ‘pt’, ‘em’, etc.) By default this will be ‘px’.
  • doc_id – The id attribute of the enclosing svg element. If None (default) then a random id will be generated.
  • doc_name – The name of the document (i.e. ‘MyDrawing.svg’).
  • layer_name – Display name of default layer. By default no default layer will be created.
  • layer_id – Id attribute value of default layer. Default id is ‘defaultlayer’.
Returns:

An lxml.etree.ElementTree