Version 2, last updated by wycc at September 28, 2010 20:46 UTC

Introduction to inkscape python extention

Inkscape Extention

The inkscape allows us to add some extentions by using script language, such as Python and Perl. In this article, I will explain the internals of the python extentions. Others will be similiar to it.

 

The architecture

Instead of publishing API to the script language, the inkscape choices to implement the plugin as UNIX-like filters. Each plugins will receive a number of arguments. According to these arguments, the plugin must read data from the standard input and write the result to the standard output. Therefore, it is very simple to write a new language binding. The disadvantage is that we can access the document only. If we need any GUI during the plugin, the plugin is responsible to do it bythemselves outside the inkscape.

This strure is very simple and userful for implementing "effect" or "filter", which is the target of the inkscape plugins.

 

The starting point

From the point view of the inkscape, when users select an extention, it will ask users to fill and form according to the INX file. An INX file is an XML file which can render a GUI form for users to input. The data filled in this form will be passed to the extention as arguments that mention previously.

Therefore, we can use any python code here to translate the input to the output by using these arguments. This is all you need to know when you write an extention.

However, the inkscape provides an framework to simplify your jobs. It is not required, but an obvious chocie to import inkex in your python extention. The inkex module defines an plugin framework, which process argument and input into python data structure so that we can access them directly. In addition, it will generate the output for the inkscape in the endof the process. All we need to worry is to define a "filter" or "effect" method.

You are not required to use inkex. However, I can not think any reason not to do so.

Internals of inkex

This file is in /usr/share/inkscape/extentions/inkex.py.

The Effect class define the application framework. Every extention must derived a class from it and override the effect method.

class Effect:
    """A class for creating Inkscape SVG Effects"""
    def __init__(self, *args, **kwargs):
        self.id_characters = '0123456789abcdefghijklmnopqrstuvwkyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
        self.document=None
        self.ctx=None
        self.selected={}
        self.doc_ids={}
        self.options=None
        self.args=None
        self.OptionParser = optparse.OptionParser(usage="usage: %prog [options] SVGfile",option_class=InkOption)
        self.OptionParser.add_option("--id",
                        action="append", type="string", dest="ids", default=[],
                        help="id attribute of object to manipulate")

There are three important variables here.

  • self.document: This is the DOM tree of the XML document. We can access the SVG file from it.
  • self.selected: This is all the objects selected inside the inkscape. We should apply the filter to these objects only.
  • self.options,self.args: This is all options specified in the arguments. We can acquire the options defined in the INK file here.

The process procedure

The affect method is the core of the framework. Let's check it.

    def affect(self):
        """Affect an SVG document with a callback effect"""
        self.getoptions()
        self.parse()
        self.getposinlayer()
        self.getselected()
        self.getdocids()
        self.effect()
        self.output()

All lines before effect is used to read data frm the standard input and arguments. The effect is called to process the data and then write the data to the standard output. Therefore, every plugin should looks like

 

    class MyPlugin(Effect):

        def effect():

            ....

    p=MyPlugin()

    p.affect()

 

The INX document

[TBC]

Some convienent utilities

  • simplestyle.py