inkex.base module

The ultimate base functionality for every Inkscape extension.

class inkex.base.InkscapeExtension[source]

Bases: object

The base class extension, provides argument parsing and basic variable handling features.

classmethod absolute_href(filename: str, default: str = '~/', cwd: Optional[str] = None) str[source]

Process the filename such that it’s turned into an absolute filename with the working directory being the directory of the loaded svg.

User’s home folder is also resolved. So ‘~/a.png` will be /home/bob/a.png

Default is a fallback working directory to use if the svg’s filename is not available, if you set default to None, then the user will be given errors if there’s no working directory available from Inkscape.

add_arguments(pars: argparse.ArgumentParser) None[source]

Add any extra arguments to your extension handle, use:

def add_arguments(self, pars):

pars.add_argument(”–num-cool-things”, type=int, default=3) pars.add_argument(”–pos-in-doc”, type=str, default=”doobry”)

static arg_class(options: List[Type]) Callable[[str], Any][source]

Used by add_argument to match an option with a class

Types to choose from are given by the options list Usage: pars.add_argument(”–class”, type=self.arg_class([ClassA, ClassB]), default=”ClassA”)

arg_method(prefix: str = 'method') Callable[[str], Callable[[Any], Any]][source]

Used by add_argument to match a tab selection with an object method

pars.add_argument(”–tab”, type=self.arg_method(), default=”foo”) … self.options.tab(arguments) … .. code-block:: python .. def method_foo(self, arguments): .. # do something

clean_up() None[source]

Clean up any open handles and other items

debug(msg: str) None[source]

Write a debug message

classmethod document_path() Optional[str][source]

Returns the saved location of the document

  • Normal return is a string containing the saved location

  • Empty string means the document was never saved

  • ‘None’ means this version of Inkscape doesn’t support DOCUMENT_PATH

DO NOT READ OR WRITE TO THE DOCUMENT FILENAME!

  • Inkscape may have not written the latest changes, leaving you reading old data.

  • Inkscape will not respect anything you write to the file, causing data loss.

effect() Any[source]

Apply some effects on the document or local context

classmethod ext_path() str[source]

Return the folder the extension script is in

extra_nss: Dict[str, str] = {}
classmethod get_resource(name: str, abort_on_fail: bool = True) str[source]

Return the full filename of the resource in the extension’s dir

has_changed(ret: Any) bool[source]

Return true if the output should be saved

load(stream: IO) str[source]

Takes the input stream and creates a document for parsing

load_raw() None[source]

Load the input stream or filename, save everything to self

static msg(msg: str) None[source]

Write a non-error message

multi_inx = False
property name

Return a fixed name for this extension

parse_arguments(args: List[str]) None[source]

Parse the given arguments and set ‘self.options’

run(args: Optional[List[str]] = None, output: Union[str, IO] = <_io.BufferedWriter name='<stdout>'>) None[source]

Main entrypoint for any Inkscape Extension

save(stream: IO) None[source]

Save the given document to the output file

save_raw(ret: Any) None[source]

Save to the output stream, use everything from self

classmethod svg_path(default: Optional[str] = None) Optional[str][source]

Return the folder the Returns None if there is no file.

class inkex.base.SvgInputMixin[source]

Bases: object

Expects the file input to be an svg document and will parse it.

load(stream: IO) etree[source]

Load the stream as an svg xml etree and make a backup

select_all: Tuple[Type[inkex.elements._base.BaseElement], ...] = ()
class inkex.base.SvgOutputMixin[source]

Bases: object

Expects the output document to be an svg document and will write an etree xml.

A template can be specified to kick off the svg document building process.

classmethod get_template(**kwargs)[source]

Opens a template svg document for building, the kwargs MUST include all the replacement values in the template, the default template has ‘width’ and ‘height’ of the document.

save(stream: IO) None[source]

Save the svg document to the given stream

template = '<svg viewBox="0 0 {width} {height}" width="{width}{unit}" height="{height}{unit}"\n        xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg"\n        xmlns:xlink="http://www.w3.org/1999/xlink"\n        xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"\n        xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">\n    </svg>'
class inkex.base.SvgThroughMixin[source]

Bases: inkex.base.SvgInputMixin, inkex.base.SvgOutputMixin

Combine the input and output svg document handling (usually for effects).

has_changed(ret: Any) bool[source]

Return true if the svg document has changed

class inkex.base.TempDirMixin(*args, **kwargs)[source]

Bases: object

Provide a temporary directory for extensions to stash files.

clean_up() None[source]

Delete the temporary directory

dir_prefix = 'inktmp'
dir_suffix = ''
load_raw() None[source]

Create the temporary directory