How to write an Inkscape extension?

Discussion about writing code for Inkscape.
theozh
Posts: 437
Joined: Fri Mar 23, 2012 6:30 pm

How to write an Inkscape extension?

Postby theozh » Thu Nov 20, 2014 5:38 pm

Since I am missing essential functions for my purpose, I want to start writing my own extension.

So, I tried the "hello_world" example given here.

The hello_world gives an error message and nothing happens.
I am using Inkscape 0.91pre2_64bit r13532
What's wrong here?
I wanted to attach a screenshot of the error message, but obviously it's not allowed to attach any of png,jpg,bmp here.

Code: Select all

Inkscape has received additional data from the script executed.  The script did not return an error, but this may indicate the results will not be as expected.

Traceback (most recent call last):
  File "hello_world.py", line 70, in <module>
    effect.affect()
  File "C:\Program Files\Inkscape\share\extensions\inkex.py", line 261, in affect
    self.effect()
  File "hello_world.py", line 45, in effect
    width  = inkex.unittouu(svg.get('width'))
AttributeError: 'module' object has no attribute 'unittouu'

Furthermore, there is a link to an Effect reference, however, there I could not find how to do boolean operations. And the link in the top is not exisiting anymore.

Is there any up-to-date step-by-step tutorial or complete reference manual how to write an Inkscape extension?

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: How to write an Inkscape extension?

Postby ~suv » Thu Nov 20, 2014 6:37 pm

theozh wrote:So, I tried the "hello_world" example given here.

The hello_world gives an error message and nothing happens.
I am using Inkscape 0.91pre2_64bit r13532
What's wrong here? (…)

Code: Select all

Inkscape has received additional data from the script executed.  The script did not return an error, but this may indicate the results will not be as expected.

Traceback (most recent call last):
  File "hello_world.py", line 70, in <module>
    effect.affect()
  File "C:\Program Files\Inkscape\share\extensions\inkex.py", line 261, in affect
    self.effect()
  File "hello_world.py", line 45, in effect
    width  = inkex.unittouu(svg.get('width'))
AttributeError: 'module' object has no attribute 'unittouu'

You chose to use an early pre-release version (0.91pre2) of the next major release (0.91) - this means that things might break or change, or be in an unfinished state, without being fully documented yet (since the release process is still on-going) …

The error you came across when trying to follow the tutorial in the Inkscape wiki is caused by a breaking change for extensions in the next major release 0.91, as described in the release notes (draft version):
Extensions -> Units: Breaking change.

Right now (the tutorial is unlikely to get updated before 0.91 is actually released (*); it works ok with the current stable release 0.48), I would recommend to either figure out on your own how to adapt the script (based on the information in the release notes), or to use the current stable version (0.48.5) for your initial steps towards writing your own extension.

theozh
Posts: 437
Joined: Fri Mar 23, 2012 6:30 pm

Re: How to write an Inkscape extension?

Postby theozh » Thu Nov 20, 2014 6:54 pm

Thanks. Sorry, I wasn't aware of these changes.
In 0.48.5 it works :-)
Do you have a link to an example how to realize boolean operations with extension/python script?
Win7/64, Inkscape 0.92.2

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: How to write an Inkscape extension?

Postby ~suv » Thu Nov 20, 2014 7:12 pm

theozh wrote:Do you have a link to an example how to realize boolean operations with extension/python script?

No. It's not trivial to realize with an extension: extension scripts don't have access to internal Inkscape functions (e.g. path operations), so either you redo the complex math for the operations yourself in the python script, or launch another inkscape process from the extension script itself which applies a corresponding verb to a list of object ids passed as command line arguments (which isn't trivial either because you first need to save the content of the SVG file obtained by the extension script to another tmp file, which then is modified by the second inkscape process, then read back the modified content of that second tmp file, before the extension script finalizes the effect. On top of that the user experience wouldn't be nice because it is not possible to disable the GUI when verbs are used as command line arguments, so a second Inkscape window opens and closes again whenever the extension is executed).

I'll attach an example (unfinished, based on a similar question asked on the devel mailing list, and the initial version of such an extension linked in Ryan Lerch's message): Subject: Multiple difference boolean ops extension. The version attached here was only briefly tested with current trunk on OS X 10.7.5, and served mainly to explore how the temp file creation and external process could be handled by the script.
Attachments
multipledifference.zip
(1.41 KiB) Downloaded 320 times

theozh
Posts: 437
Joined: Fri Mar 23, 2012 6:30 pm

Re: How to write an Inkscape extension?

Postby theozh » Thu Nov 20, 2014 7:46 pm

Thanks a lot for these detailed explanations.
I tried the MultipleDifference in 0.48.5 and 0.91pre2
It looks like it just cuts the top object from the bottom object and ignores the other objects. It also ignores groups and clones.
Well, you said it's unfinished. But if you as an expert say it's not trivial, I do not see me as a beginner accomplishing anything here in the short term.

... sorry, correction...
the top object is subtracted from all underlaying objects.
Well, I was looking for the case that all above laying objects are subtracted from the bottom object.
It takes quite some time for a few objects. I haven't tested how long this will take for thousands of objects...
Win7/64, Inkscape 0.92.2


Return to “Programming”