In today’s tutorial, I will show you how to create your own extension for the Inkscape graphics program.
This extension will be added to the main menu at Extensions – Extensions by catafest – catafest test – 001.
Open the Inkscape software and from the menu choose: Edit – Preferences – System and look for the location where the Inkscape extensions are stored. In Inkscape, looks like this:
I created a folder called catafest and with Visual Code I created two files named: catafest_extension.inx, and catafest_extension.py.
For catafest_extension.inx the source code is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?xml version="1.0" encoding="UTF-8"?> <inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension"> <name>catafest - test 001</name> <id>org.catafest.catafest-001</id> <effect> <object-type>all</object-type> <effects-menu> <submenu name="Extensions by catafest"/> </effects-menu> </effect> <script> <command location="inx" interpreter="python">catafest_extension.py</command> </script> </inkscape-extension> |
For catafest_extension.py the source code is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #!/usr/bin/env python # coding=utf-8 # # Copyright (C) 2023 Catalin George Festila, catafest@yahoo.com # """ Simple test extension for inkscape """ import inkex # add by me from lxml import etree def draw_SVG_square(w,h, x,y, parent): style = { 'stroke' : 'none', 'stroke-width' : '1', 'fill' : '#0000FF' } attribs = { 'style' : str(inkex.Style(style)), 'height' : str(h), 'width' : str(w), 'x' : str(x), 'y' : str(y) } patrat = etree.SubElement( parent, inkex.addNS('rect','svg'), attribs ) return patrat class MyExtension(inkex.Effect): def __init__(self): super().__init__() def effect(self): self.msg("This is an empty extension created by catafest !") parent = self.svg.get_current_layer() draw_SVG_square(100,100, 0,0, parent) if __name__ == '__main__': MyExtension().run() |
I restarted Inkscape software and in the main menu this extension was visible, like this:
I clicked on that button named catafest – test 001 and it displayed something like this:
I clicked on the Ok button and it added a blue square as I defined it in the python source code.