I have made two small Python extensions for 0.45.1. When I run them in 0.46-pre3 they fail early on (trying to get the svg element using getElementsByTagName (code borrowed from the old hello-world.py)).
It seems much or all of the DOM API has been replaced by other calls. Is there a recommended way of keeping my extensions working with 0.45 but also working with 0.46? Or do I have to write my own wrapper functions to do that (perhaps if I wrap all XML elements in a class that provides the old DOM functions I used?)?
Python extensions for 0.45 and 0.46
Re: Python extensions for 0.45 and 0.46
0.45 used libxml and 0.46 uses lxml. All extensions had to be rewritten. The exception are the color effects. They don't do any DOM interaction on their own (it's all done by the coloreffect base class).
While writing a wrapper is surely possible, it's a lot of work. Porting your extensions over to lxml will be quicker.
While writing a wrapper is surely possible, it's a lot of work. Porting your extensions over to lxml will be quicker.
Re: Python extensions for 0.45 and 0.46
The way I'm doing it in my star field extension is like this:
This works fine, although it obviously increases the code length quite a bit.
I wouldn't do that, because lxml is nicer to use than the DOM stuff, and significantly faster than PyXML (at least for my extension, which typically creates hundreds of elements). Putting it in a DOM-like wrapper will just make it clunky and slow, like PyXML.
Code: Select all
try:
use_lxml = inkex.etree
use_lxml = True
except AttributeError:
use_lxml = False
if use_lxml:
# do it the lxml way
else:
# do it the PyXML way
This works fine, although it obviously increases the code length quite a bit.
pelle wrote:(perhaps if I wrap all XML elements in a class that provides the old DOM functions I used?)
I wouldn't do that, because lxml is nicer to use than the DOM stuff, and significantly faster than PyXML (at least for my extension, which typically creates hundreds of elements). Putting it in a DOM-like wrapper will just make it clunky and slow, like PyXML.
Re: Python extensions for 0.45 and 0.46
OK, thanks! I'll borrow that use_lxml code, and then have a look at the other extensions to see how to use lxml.
Re: Python extensions for 0.45 and 0.46
Is there a way to find out the directory where the SVG image file is saved (if it is saved...) from the effect? One of my effects require the user to input the name of a CSV file that is used as input for the effect, and I think especially to Windows users it would be a great improvement if the effect could look for the file in the directory where the SVG is by default, to work without absolute filenames. There does not seem to be a way to add a file selection parameter to the inx file (that would be the perfect solution to this problem!), so I'm using a simple string parameter.
In my latest unreleased version I have code that searches for the CSV file in the directory given as sodipodi:basedir, but today I noticed when reading the 0.46 release notes that that attribute is not suppored anymore. The only directory I can search reliably then for relative paths is the current directory as far as I know, but I'm not sure that wil be of much help to most users.
In my latest unreleased version I have code that searches for the CSV file in the directory given as sodipodi:basedir, but today I noticed when reading the 0.46 release notes that that attribute is not suppored anymore. The only directory I can search reliably then for relative paths is the current directory as far as I know, but I'm not sure that wil be of much help to most users.
Re: Python extensions for 0.45 and 0.46
By the way that effect is called "Create Countersheet" and used to create sheets of counters or cards for boardgames (or cardgames, obviously) from templates in the SVG file and a specification in a CSV file. Another effect I have done is called "Create Hexmap" and is used to create a grid of hexagons, with some added things in other layers (things useful when designing maps for many boardgames, like configurable coordinates in each hex and a small center dot in each hex). The latest version for Inkscape 0.46 can be downloaded from here if anyone wants to have a look or even use them (a few examples are included, with some descriptions in the SVG files to try to explain what is going on). I haven't considered if one of these effects could be made into something more generic useful beyond the limited domain of designing games...
Re: Python extensions for 0.45 and 0.46
pelle wrote:Is there a way to find out the directory where the SVG image file is saved (if it is saved...) from the effect?
The effect doesn't save the SVG file, it just writes it to stdout.
But Inkscape saves the SVG file for the effect to read, and you can get the directory for that in your effect class using the os.path module:
Code: Select all
os.path.dirname(self.args[-1])
However, this isn't likely to be a convenient directory for the user - on Windows it's usually something like C:\DOCUME~1\username\LOCALS~1\Temp
Re: Python extensions for 0.45 and 0.46
I'm after the directory where the SVG was last saved, if it was saved, not the temporary directory. That would be a good place to search for the CSV if the user did not input a full absolute path to it.
Edit: Saved by the user.
Edit: Saved by the user.