Using boolean operations in an Inkscape extension

2025-11-22

inkscape python tip

I've wasted 3 days doing this, so I will at least publish my findings on the web so you don't have to waste another 3 days. I wasn't able to find a good example online and the docs are not well-made (not saying that they should be a priority), so figuring it out does require some code research.

The inkex module provides lots of things, but boolean operations aren't one of them. Thus, you will have to call Inkscape yourself; luckily, inkex does provide that.

Even though they are listed as arguments, no special processing is done to the values, which are still strings.

I leave a minimal example:

import inkex
import io

class UnionTwoPaths(inkex.EffectExtension):
   def effect(self):
       selected = list(self.svg.selection)

       if len(selected) != 2:
           inkex.errormsg("Select exactly two paths!")
           return

       # Make a string of the IDs
       ids = ",".join([elem.get_id() for elem in selected])

       # Call Inkscape to make a union
       union_svg_bytes = inkex.command.inkscape_command(
           self.document,
           select=ids,
           actions="path-union"
       )

       # Send the drawing back
       self.document = inkex.load_svg(union_svg_bytes)


if __name__ == "__main__":
   UnionTwoPaths().run()
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
   <name>Union of two paths</name>
   <id>org.inkscape.boolean.union_two_paths</id>
   <effect>
       <!-- Enable only on path objects (could use 'all' if preferred) -->
       <object-type>path</object-type>
       <effects-menu>
           <submenu name="Generate from Path"/>
       </effects-menu>
   </effect>
   <script>
       <command reldir="extensions" interpreter="python">union_two_paths.py</command>
   </script>
</inkscape-extension>