One piece of information which Inkscape sends to an extension is selected elements. The extension receives ids of selected elements as command line arguments from Inkscape.
For example, we have a drawing with 4 selected elements as shown below. When we click
apply button on an extension user interface, the extension receives 4 “--id=…”
values as shown in the
sys.argv variable below.
sys argv: ['hello.py', '--id=rect1125', '--id=path1149', '--id=path1253', '--id=path1358', '--name=inkscape', '/tmp/ink_ext_XXXXXX.svgUBQJ80']
The id values are the id attributes of selected elements. Because the id attribute is unique for each element, the extension knows which elements are selected.
argparse Python standard library module to parse arguments. The
code is in the
__init__ method of
SvgInputMixin class (inkex/base.py module).
self.arg_parser.add_argument( "--id", action="append", type=str, dest="ids", default=, help="id attribute of object to manipulate") self.arg_parser.add_argument( "--selected-nodes", action="append", type=str, dest="selected_nodes", default=, help="id:subpath:position of selected nodes, if any")
The selected ids will become a list which is accessed as
self.options.ids in an
self.options : Namespace(input_file='/tmp/ink_ext_XXXXXX.svgUBQJ80', output=None, name='inkscape', ids=['rect1125', 'path1149', 'path1253', 'path1358'], selected_nodes=)
inkex loads an SVG file, it will set an instance variable
SvgDocumentElement class object. The
selection variable is of type
ElementList and its value is set in the
load method of
self.svg = document.getroot() self.svg.selection.set(*self.options.ids)
ElementList class is defined in the
Even though it is called
ElementList, it subclasses
OrderedDict so it’s a
dictionary. When we iterate through an instance variable, it iterates through
the values of the dictionary and the class acts like a list. The values of
the dictionary are the selected element objects.
We would think that keys of the dictionary are the ids of selected elements and
the values are the corresponding element objects. However, the keys are actually
xml_path of selected element. The
xml_path is a property defined in the
BaseElement class and it calls the
getpath method of
The lxml documentation
describes that “[getpath method] returns a structural, absolute XPath expression to find the
ElementList key is a string value. It’s the value before the colon as shown below.
The first part
svg element. The second part
/* refers to
g layer element nested inside
g element is listed after
The third part
/* refers to the rect element, which is the first
one nested under
# ELementList dict format: xml_path to element /*/*/* : rect /*/*/* : ellipse /*/*/* : path /*/*/* : path
ElementList class also defines an
ids instance variable, which is a
xml_path. The class also has a method
which returns a dictionary mapping
element. Why does the class choose
xml_path value as the dictionary key? It is probably for the
method which returns a list of selected elements by z-order (bottom to top).
One very useful method in the
ElementList class is
filter. We can
filter out elements by type. The method return a new
containing only elements of certain type. The code below shows an
example, and the return value
selected_elems only contains path elements.
from inkex import PathElement select_elems = self.svg.selection.filter(PathElement)
bounding_box method returns a
BoundingBox object for selected elements.
It’s useful when we need to know the size of selected elements such as
Inkscape itself preserves the selection order when passing the ids to an extension.
The first element in the
ElementList is the first selected element in Inkscape.
The extension system doesn’t provide a way to transmit selected elements back to
Inkscape, so we can’t modify selections in an Inkscape extension.
Similar to selected elements, Inkscape also passes selected nodes to extensions.
In inkscape we use the node selection tool (shortcut: F2) to select nodes on a path. For example,
the drawing below shows that the first two nodes are selected (blue square dots). When
we click the apply button on an extension interface, the extension receives several
The argument values are in a format shown below. The help message of
indicates it is in
id:subpath:position format. Notice the
values are both zero based here.
Searching through the system extension directory, the
are not used in any extensions. However, it might be useful when we need to
deal with path nodes in extensions.