General Gist
The general gist of inkscapeMadeEasy is:
inkscapeMadeEasy_Base.inkscapeMadeEasy
inheritsinkex.Effect
, therefore has access to allinkex.Effect
member functions plus all methods defined ininkscapeMadeEasy_Base.py
Your extension is a class that inherits
inkscapeMadeEasy_Base.inkscapeMadeEasy
. Therefore you will have access to all member functions ofinkscapeMadeEasy_Base.inkscapeMadeEasy
plus the methods you define in your extension.If you need anything from
inkscapeMadeEasy_Draw
orinkscapeMadeEasy_Plot
, you can import them into your project.
The basic structure of your plugin should be the following:
1 import inkscapeMadeEasy.inkscapeMadeEasy_Base as inkBase
2 import inkscapeMadeEasy.inkscapeMadeEasy_Draw as inkDraw
3 import inkscapeMadeEasy.inkscapeMadeEasy_Plot as inkPlot
4
5 class MinimalExample(inkBase.inkscapeMadeEasy):
6
7 def __init__(self):
8 inkBase.inkscapeMadeEasy.__init__(self)
9 ...
10 initialization procedure, argument parser
11 ...
12
13 def effect(self):
14 ...
15 The method 'effect()' is the only method called directly by inkscape.
16 Consider this function to be the main function of your plugin.
17 ...
18
19 def fooBar(self):
20 ...
21 You can create other methods if you want. These can be called by other
22 methods inside your plugin but inkscape will NEVER call these directly.
23 ...
24
25
26 if __name__ == '__main__':
27 myPlugin = MinimalExample()
28 myPlugin.run()
Note
The best way of learning how to use it is by looking at real life examples. Check my other repositories.
Minimal example
The following is a minimal example using inkscapeMadeEasy.
The structure of inkscape extensions directory should be as follows:
inkscape
┣━━ extensions
┋ ┣━━ inkscapeMadeEasy <-- folder with inkscapeMadeEasy files
┃ ┣━━ inkscapeMadeEasy_Base.py
┃ ┣━━ inkscapeMadeEasy_Draw.py
┃ ┣━━ inkscapeMadeEasy_Plot.py
┃ ┗━━ basicLatexPackages.tex
┃
┣━━ minimalExample <-- folder with minimal example files
┃ ┣━━ minimalExample.py
┃ ┗━━ minimalExample.inx
┋
minimalExample.inx
1<?xml version="1.0" encoding="UTF-8"?>
2<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
3 <name>Minimal Example</name>
4 <id>minimalExample</id>
5 <dependency type="executable" location="inx">minimalExample.py</dependency>
6 <dependency type="executable" location="extensions">inkscapeMadeEasy/inkscapeMadeEasy_Base.py</dependency>
7 <dependency type="executable" location="extensions">inkscapeMadeEasy/inkscapeMadeEasy_Draw.py</dependency>
8 <dependency type="executable" location="extensions">inkscapeMadeEasy/inkscapeMadeEasy_Plot.py</dependency>
9
10 <param name="drawCircle" type="bool" gui-text="Draw Circle">false</param>
11 <param name="drawPlot" type="bool" gui-text="Draw Plot">false</param>
12
13 <effect>
14 <object-type>all</object-type>
15 <effects-menu></effects-menu>
16 </effect>
17 <script>
18 <command location="inx" interpreter="python">minimalExample.py</command>
19 </script>
20</inkscape-extension>
minimalExample.py
1#!/usr/bin/python
2
3import inkscapeMadeEasy.inkscapeMadeEasy_Base as inkBase
4# in this example I am going to use both inkscapeMadeEasy_Draw.py and inkscapeMadeEasy_Plot functions, so I have to load them.
5import inkscapeMadeEasy.inkscapeMadeEasy_Draw as inkDraw
6import inkscapeMadeEasy.inkscapeMadeEasy_Plot as inkPlot
7
8
9# your plugin must inherit inkBase.inkscapeMadeEasy
10class MinimalExample(inkBase.inkscapeMadeEasy):
11 # the __init__ must initiate inkBase.inkscapeMadeEasy and build your argument parser
12 def __init__(self):
13 # initiate inkBase.inkscapeMadeEasy
14 inkBase.inkscapeMadeEasy.__init__(self)
15
16 # argument parser
17 self.arg_parser.add_argument("--drawCircle", type=self.bool, dest="drawCircle", default=False)
18 self.arg_parser.add_argument("--drawPlot", type=self.bool, dest="drawPlot", default=False)
19
20 # the effect function will be the main function of your plugin. This function will 'do the stuff'
21 def effect(self):
22 # parse the arguments.
23 so = self.options
24
25 # get the coords of the center of the view. In this case I am calling a function inside inkex.
26 position = [self.svg.namedview.center[0], self.svg.namedview.center[1]]
27
28 # parent of all elements
29 root_layer = self.document.getroot()
30
31 # creates a group. Note 'createGroup' is an inkscapeMadeEasy_Base.inkscapeMadeEasy method.
32 # However, MinimalExample inherits all the methods and properties from inkBase.inkscapeMadeEasy so you can call it with ``self.[SOME_METHOD]``
33 group = self.createGroup(root_layer, 'myGroup')
34
35 # the next elements will be created inside the group
36 if so.drawCircle:
37 # draw a circle, showing how to use inkscapeMadeEasy_Draw.py
38 center = [0, 0]
39 radius = 2
40 # creates a line style for the circle
41 self.lineStyle = inkDraw.lineStyle.setSimpleBlack(1.5)
42 inkDraw.circle.centerRadius(group, center, radius, offset=position, lineStyle=self.lineStyle)
43
44 if so.drawPlot:
45 # draw a cartesian plane, showing how to use inkscapeMadeEasy_Plot.py
46 inkPlot.axis.cartesian(self, group, xLim=[0.0, 2.0], yLim=[0.0, 2.0], position=position, xLabel='x axis', yLabel='y axis')
47
48
49if __name__ == '__main__':
50 myPlugin = MinimalExample()
51 myPlugin.run()
testingMinimalExample.py
You can even run the plugin without inkscape! Create a .py file in the minimalExample
directory with the following contents
1#!/usr/bin/python
2import os
3
4import minimalExample as myExtension
5
6#loads the extension
7myExt = myExtension.MinimalExample()
8
9#runs the plugin. Remember to change the paths of the svg files.
10# ``existing_file.svg`` is one existing file that will be modified by the example.
11# ``new_file.svg`` will be created with the result of the example.
12myExt.run([r'--drawCircle=True', r'--drawPlot=True', r'/path/to/existing_file.svg'], output=os.devnull)
13
14#save the result
15myExt.document.write('/path/to/new_file.svg')
and run python from the console.
python3 path/to/minimalExample/testingMinimalExample.py
Tip
Why would you want to do this? Debugging your code! Inkscape neither has a stdout for you to dump stuff and inspect nor has a debug tool to run step by step or add break points. Running independently from inkscape allow you to run via PyCharm or other python IDEs. Use it now and thank me later. =)