Python modules for extensions

From Inkscape Wiki
Revision as of 00:20, 5 November 2017 by Moini (talk | contribs)
Jump to navigation Jump to search

These modules are provided as part of Inkscape and can be found in /usr/share/inkscape/extensions on GNU/Linux, ... on Windows, and ... on Mac OS X along with the extensions bundled with Inkscape. They can be imported from an extension just like any other python module.

This module encapsulates the basic behavior of a script extension, allowing the author to concentrate on manipulating the SVG data. It relies on lxml.etree to work with the XML tree. was originally designed to provide the Effect (filter) extension type, but has been used for Input/Output extensions simply by overriding additional class methods.

Functions provides the following functions:

errormsg( msg )
End-user visible error message, it should always be used with translation: inkex.errormsg(_("This extension requires two selected paths"))
addNS( tag, ns=None )
Returns the selected tag, with the namespace applied. The namespace is selected from a list supplied with the module.

The Effect class

The most important part of is the Effect class. To implement an effect type extension in Python see PythonEffectTutorial


You should overwrite this method with your own, as shown in PythonEffectTutorial#Effect Extension Script
getElementById( id )
Returns the firs found element with given id, as a lxml element.
getParentNode( node )
Returns the parent of node. Probably the same as node.getparent() from lxml?
createGuide( x, y, angle )
Creates guide at position (x,y), with angle angle.
Actuate the script.
xpathSingle( path )
An xpath wrapper to return a single node.
uniqueId( old_id )
Return an id that is unique in the document given a proposed id, by appending random alphanumeric characters to it.
Return width of document, as a string.
Return height of document, as a string.
Return a string representing the default unit for the document. Full list of possible units is defined in the module.
unittouu( string )
Convert given value (as a string, e.g: "4px") to units used by the document. Returns float.
uutounit( value, unit )
Convert a value (float) in document default units to given units.


DOM document, as a lxml.etree.
A dict mapping ids to nodes, for all nodes selected in Inkscape.
A dict mapping ids to the constant 1, for all of the ids used in the original document. Does not get automatically updated when adding elements.
Options passed to the script.

Provides methods for dealing with css data embedded in SVG's style attribute. When a color is represented as integers they should be in the (0, 255) range, when represented as floats, they should be in the (0.0, 1.0) range.

parseStyle( string )
Create a dictionary of attribute-value pairs from the value of an inline style attribute.
formatStyle( dict )
Format an inline style attribute from a dictionary of attribute-value pairs, values are converted to strings by str().
isColor( c )
Determine if c is a valid color.
parseColor( c )
Creates a rgb int array. c can be any type of string representation of a color.
formatColoria( a )
Convert int array to #rrggbb string.
formatColorfa( a )
Convert float array to #rrggbb string.
formatColor3i( r, g, b )
Convert 3 ints to #rrggbb string.
formatColor3f( r, g, b )
Convert 3 floats to #rrggbb string.
A dictionary defining legal color names and corresponding color values.

Provides functions to round trip svg path d="" attribute data and a simple path format mimicking that datastructure, and additional functions for scaling translating and rotating path data.

parsePath( d )
Parse SVG path and return an array of segments. Removes all shorthand notation. Converts coordinates to absolute. Returns list of [ command, params ] list.
formatPath( l )
Format path data from a list. Returns the string representing the path, l should have the same format as returned by parsePath.
translatePath( p, x, y ), scalePath( p, x, y ), rotatePath( p, angle, cx=0, cy=0 )
Transforms path p.

An alternative path representation, accessing both handles of a node at once. Loses a path's open/closed identity.

CubicSuperPath( simplepath )
Given a path as a list returned by simplepath.parsePath, it returns a list of lists in [ [ h1_0, pt_0, h2_0 ], [ h1_1, pt_1, h2_1 ], ... ] format, where h1_n and h2_n are handles for the node at point pt_n. All points/handles are lists of two floats ([ x, y ]).
unCubicSuperPath( csp )
Given a path in the format returned by CubicSuperPath returns it in the format used by simplepath.
parsePath( d )
Parse a string representation directly.
formatPath( p )
Format path as a string.

Provides code to easily transform objects.

  • parseTransform
Takes a string such as rotate(10) and produces a transformation matrix. If you also supply an initial matrix, the new one will be composed with the old one.
Available commands: translate, scale, rotate, skewX, skewY, matrix. Other examples:
matrix = parseTransform('rotate(10)')
matrix = parseTransform('skewY(10)')
matrix = parseTransform('translate(10 10)')
matrix = parseTransform(' rotate(10)')
matrix = parseTransform('translate(700,210) rotate(-30)')

Provides turtle graphics primitives with svg path data output

Utility functions for working with bezier curves

Decompose a path into polylines


an obscure set of tools for dealing with musical scales.