inkext.inkext

Inkscape extension helper class.

inkext.inkext.DEBUG = False

True if debug layer or debug logging is enabled.

exception inkext.inkext.ExtensionError

Exception thrown to abort extension.

class inkext.inkext.InkscapeExtension

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

add_options(parser: ArgumentParser) None

Add CLI option.

Subclasses override this to add any option arguments that are specific to the extension.

Parameters:

parser – An instance of argparse.ArgumentParser

effect() None

Extensions override this method to do the actual work.

Raises:

ExtensionError if the extension needs to abort for any reason.

main(flip_debug_layer: bool = False) None

Main entry point for the extension.

Parameters:

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.

post_process_options() None

Fix CLI option values after parsing SVG document.

Options values that are of type ‘docunits’ will be converted to SVG user units.

run(argv: list | None = None, output: str | PathLike | None = None, flip_debug_layer: bool = False) None

Legacy Inkex entry point for the extension.

Parameters:
  • argv – Command line options, default is sys.argv[1:]

  • output – Output file name. Default output is stdout.

  • 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.

selected_elements(selected_only: bool = False) Iterator[TElement]

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 etree.Elements.

selected_pathnode_points() Iterator[geom2d.P]

Get selected path nodes as point coordinates.

Returns:

An iterable of (x,y) points.

selected_pathnodes() Iterator[tuple[TElement, int, int]]

Get selected path nodes.

Returns:

An iterable of tuples of the form [(path-element, subpath-index, node-index), …]

inkext.inkext.csscolor(value: str | int) str

Inkscape-argparse CSS color type.

Convert an Inkscape color widget value or form text value to a CSS hex color value (ie ‘#ffffff’ or ‘none’).

inkext.inkext.degrees(value: str | float) float

Argparse type: Convert an angle specified in degrees to radians.

inkext.inkext.docunits(value: str | float) _DocUnits

Inkscape-argparse document unit value type.

Value will be converted to document units after the document has been parsed and the document units can be determined. The DocUnit type is just a memo so that the post processing step can find the cli options of type docunits.

inkext.inkext.errormsg(*args: Any, exit_status: int | None = None, **kwargs: Any) None

Write an error msg to stderr.

Intended for end-user-visible messages (usually error conditions). Inkscape displays stderr output in a dialog after the extension runs.

inkext.inkext.inkbool(value: str | int | bool) bool

Inkscape-argparse boolean type.

Convert a string boolean (ie ‘True’ or ‘False’) to Python boolean.

inkext.inkext.inkrgb_to_styles(fill: tuple[float, ...] | None = None, stroke: tuple[float, ...] | None = None, stroke_width: str | float | None = None) dict

Convert Inkscape INX RGBA values to a style dictionary.

inkext.inkext.output_path(path: str | PathLike, auto_incr: bool = False, ndigits: int = 4, default_parent: str | Path | None = None, default_stem: str = 'output', default_suffix: str | None = None) Path

Generates an absolute file path name based on the specified path.

The pathname can optionally have an auto-incrementing numeric suffix.

Parameters:
  • path – Name or path of output file.

  • auto_incr – Append an auto-incrementing numeric suffix to the file name if True. Default is False.

  • ndigits – Number of digits (zero padding) for auto-increment number.

  • default_parent – Default parent directory if filepath does not have one. Default is none (current directory).

  • default_stem – Default filename stem. Default is ‘output’.

  • default_suffix – Default file extension if filepath does not have one. Default is no extension.

Returns:

An absolute Path.

inkext.inkext.percent(value: str | float) float

Argparse type: Convert a percentage specified as 0-100 to a float 0-1.0.

Parameters:

value – a number in the range 0-100

inkext.inkext.resolve_path(path: str | PathLike) Path

Perform HOME (~) expansion and path resolution.

Creates absolute path and also removes the snap components from a path (if any) if it is relative to HOME. This only makes sense on Ubuntu.

Resolves relative paths relative to the user’s HOME, not to Inkscape’s extension location. Inkscape’s file picker always creates absolute paths, and if a user types in a relative path Inkscape assumes it is relative to the CWD which is the extension directory, which is aggravating. So this tries to deal with hand-typed file paths…

inkext.inkext.rgbacolor(value: str | int) tuple

Inkscape-argparse type.

Convert an Inkscape color widget value to a RGBA tuple (R, G, B, A).