_ _ | |_ ___ __| |____ ___ _ _ | __/ _ \/ _` |_ / / _ \ | | | | || __/ (_| |/ / | __/ |_| | \__\___|\__,_/___(_)___|\__,_|
This is an example Inkscape extension that creates a path element in the current layer of the document.
Two files are required to create the extension:
I placed both files in "~/.config/inkscape/extensions/" and restarted Inkscape to add them to the extensions menu (under the "Ted" submenu). Note that Inkscape does not need to be restarted each time test.py is edited.
This is the file test.inx:
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>Test</name>
<id>ted.test</id>
<param name="test_param" type="float" min="0.01" max="100" gui-text="Test parameter:">10.0</param>
<param name="test_option" type="optiongroup" appearance="combo" gui-text="Select one...">
<option value="option1">First option</option>
<option value="option2">Second option</option>
</param>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu name="Ted"/>
</effects-menu>
</effect>
<script>
<command location="inx" interpreter="python">test.py</command>
</script>
</inkscape-extension>
This is the file test.py:
import inkex
class Test(inkex.EffectExtension):
def add_arguments(self, pars):
pars.add_argument("--test_param", type=float, default=10.0, help="Test parameter")
pars.add_argument("--test_option", default="option1", help="Select one of these")
def effect(self):
current_layer = self.svg.get_current_layer()
offset = self.svg.namedview.center # centre of canvas
#inkex.utils.errormsg(str(offset))
self.options.test_param = self.svg.unittouu(str(self.options.test_param) + "px")
stroke_width = self.svg.unittouu(str(self.options.test_param) + "px")
# Create an example list of node coordinates
nodes = []
nodes.append((0,0))
nodes.append((10,0))
nodes.append((10,10))
nodes.append((20,10))
nodes.append((20,20))
# Create a path new element using the list of node coordinates
elem = current_layer.add(inkex.PathElement())
style = {"stroke": "#000000", "stroke-width": str(stroke_width), "fill": "none"}
name = "My path"
node = nodes[0]
nodes = nodes[1:]
d = "M " + str(node[0]+offset[0]) + "," + str(node[1]+offset[1]) + " "
for node in nodes:
d += "L " + str(node[0]+offset[0]) + "," + str(node[1]+offset[1]) + " "
elem.update( **{ "style": style, "inkscape:label": name, "d": d, })
if __name__ == "__main__":
Test().run()