Hello, I hope this list is the correct one for this kind of question: Has anyone managed to get http://wiki.inkscape.org/wiki/index.php/PythonEffectTutorial to work without major changes? It failed for me both on a gentoo system and on a pretty vanilla Ubuntu 8.04 box. I don't know if I will find the time to edit the wiki page according to the current lxml behaviour, but I might give it a try. Additionally Ishmal(I think) said on irc/jabber that lxml might not be used in the future anymore anyway.
Now for the list of problems I encountered: * Simply making the file executable and putting it into ~/.inkscape/extensions/ is not sufficient, it won't find inkex there, but no problem, simply add import sys sys.path.append('/usr/share/inkscape/extensions') # or another path, as appropriate for your installation to the top of it. A minor problem. * All the getElementsByTagName, getAttribute, createElement,... methods are unknown to lxml, at least in the versions here (1.3.6). Inkscape returns the python error message ending with: File "/home/dhornun/.inkscape/extensions/helloworld.py", line 32, in effect svg = self.document.getElementsByTagName('svg')[0] AttributeError: 'etree._ElementTree' object has no attribute 'getElementsByTagName' If this is not a problem on the distributions' side, those can probably be fixed by adapting the code to reflect lxml's current API.
Thank you very much for any useful advice! Daniel
Daniel Hornung wrote:
Hello, I hope this list is the correct one for this kind of question: Has anyone managed to get http://wiki.inkscape.org/wiki/index.php/PythonEffectTutorial to work without major changes? It failed for me both on a gentoo system and on a pretty vanilla Ubuntu 8.04 box. I don't know if I will find the time to edit the wiki page according to the current lxml behaviour, but I might give it a try. Additionally Ishmal(I think) said on irc/jabber that lxml might not be used in the future anymore anyway.
Now for the list of problems I encountered:
- Simply making the file executable and putting it into ~/.inkscape/extensions/ is not sufficient, it won't find inkex there, but no problem, simply add import sys sys.path.append('/usr/share/inkscape/extensions') # or another path, as appropriate for your installation
Yes, this is the best solution. Copying needed modules to the private extensions dir also works, but is much dirtier.
to the top of it. A minor problem.
- All the getElementsByTagName, getAttribute, createElement,... methods are unknown to lxml, at least in the versions here (1.3.6). Inkscape returns the python error message ending with: File "/home/dhornun/.inkscape/extensions/helloworld.py", line 32, in effect svg = self.document.getElementsByTagName('svg')[0] AttributeError: 'etree._ElementTree' object has no attribute 'getElementsByTagName'
If this is not a problem on the distributions' side, those can probably be fixed by adapting the code to reflect lxml's current API.
You are 100% correct. This tutorial was written before we began using lxml. It teaches the pyxml api not an older lxml api. Off the top of my head (without checking the api docs to be sure):
svg = self.document.getElementsByTagName('svg')[0] width = inkex.unittouu(svg.getAttribute('width')) svg.setAttribute('width', width * 2) text = self.document.createElement('text') value = self.document.createTextNode('Hello %s!' % (what)) text.appendChild(value) svg.appendChild(text)
becomes:
svg = self.document.xpath('//svg:svg',namespaces=inkex.NSS)[0] width = inkex.unittouu(svg.get('width')) svg.set('width', width * 2) text = inkex.etree.Element('text') text.text = 'Hello %s!' % (what) svg.append(text)
if you just want the root node you can replace the xpath with:
svg = self.document.getroot()
Additionally, something like:
text = inkex.etree.Element('text') text.setAttribute('x', '200') svg.append(text)
can be simplified with:
inkex.etree.SubElement(svg, 'text', {'x':'200'})
and actually 'text' probably needs to be inkex.addNS('text','svg') so that it occurs in the proper namespace.
Aaron Spike
Thank you very much for your advice, I'll use the following tips as a cheat sheet and try to get the same results for myself :-) Oh, and maybe you could add a small note to http://www.ekips.org/comp/inkscape/ that most things there are included already and/or not the current state of the art? Your page is google's #1 for "inkscape python".
On Wednesday 30 July 2008, Aaron Spike wrote:
You are 100% correct. This tutorial was written before we began using lxml. It teaches the pyxml api not an older lxml api. Off the top of my head (without checking the api docs to be sure):
svg = self.document.getElementsByTagName('svg')[0] width = inkex.unittouu(svg.getAttribute('width')) svg.setAttribute('width', width * 2) text = self.document.createElement('text') value = self.document.createTextNode('Hello %s!' % (what)) text.appendChild(value) svg.appendChild(text)
becomes:
svg = self.document.xpath('//svg:svg',namespaces=inkex.NSS)[0] width = inkex.unittouu(svg.get('width'))
Either that or svg.attrib['width'] it's probably just a matter of taste
svg.set('width', width * 2) text = inkex.etree.Element('text') text.text = 'Hello %s!' % (what) svg.append(text)
Rather: layer.append(text)
if you just want the root node you can replace the xpath with:
svg = self.document.getroot()
yes, simpler in this case, they should be mentioned both in the tutorial.
Additionally, something like:
text = inkex.etree.Element('text') text.setAttribute('x', '200') svg.append(text)
can be simplified with:
inkex.etree.SubElement(svg, 'text', {'x':'200'})
So the attributes come as a dictionary? Great!
and actually 'text' probably needs to be inkex.addNS('text','svg') so that it occurs in the proper namespace.
Ok, I found out about the necessity of namespaces the hard way, because lxml complained about the colons in 'inkscape:label', luckily http://sourceforge.net/mailarchive/message.php?msg_name=1204520414.7055.4.ca... pointed to the correct line in dimension.py.
I'll try to find the time to update the wiki page a bit in a week or so, I probably won't have time the next days.
Is there something I can read up about changes to the python scripting interface (inkex.py and a few other modules, afaict at the moment) that might be planned or will it stay more or less as it is for the near future?
Thank you very much again for your help!
Daniel, who doubled his knowledge about xml APIs during the last week
Daniel Hornung wrote:
Thank you very much for your advice, I'll use the following tips as a cheat sheet and try to get the same results for myself :-) Oh, and maybe you could add a small note to http://www.ekips.org/comp/inkscape/ that most things there are included already and/or not the current state of the art? Your page is google's #1 for "inkscape python".
Good idea. I've done so now. I've been reluctant to delete the pages because they are so well indexed by google and most of the concepts involved have remained the same.
On Wednesday 30 July 2008, Aaron Spike wrote:
You are 100% correct. This tutorial was written before we began using lxml. It teaches the pyxml api not an older lxml api. Off the top of my head (without checking the api docs to be sure):
svg = self.document.getElementsByTagName('svg')[0] width = inkex.unittouu(svg.getAttribute('width')) svg.setAttribute('width', width * 2) text = self.document.createElement('text') value = self.document.createTextNode('Hello %s!' % (what)) text.appendChild(value) svg.appendChild(text)
becomes:
svg = self.document.xpath('//svg:svg',namespaces=inkex.NSS)[0] width = inkex.unittouu(svg.get('width'))
Either that or svg.attrib['width']
Yes. I think I read somewhere that attrib collects all the attributes into a dictionary. get() and set() operate on the attributes without that initial step. I suppose attrib may be slower if there are a large number of attributes or a large number of objects for which you want to change attributes. Some one should profile it and tell us. :-)
it's probably just a matter of taste
svg.set('width', width * 2) text = inkex.etree.Element('text') text.text = 'Hello %s!' % (what) svg.append(text)
Rather: layer.append(text)
Well layer.append() if you are following the tutorial. I altered the example to try to pack in as much lxml as possible in a small space.
if you just want the root node you can replace the xpath with:
svg = self.document.getroot()
yes, simpler in this case, they should be mentioned both in the tutorial.
Additionally, something like:
text = inkex.etree.Element('text') text.setAttribute('x', '200') svg.append(text)
can be simplified with:
inkex.etree.SubElement(svg, 'text', {'x':'200'})
So the attributes come as a dictionary? Great!
and actually 'text' probably needs to be inkex.addNS('text','svg') so that it occurs in the proper namespace.
Ok, I found out about the necessity of namespaces the hard way, because lxml complained about the colons in 'inkscape:label', luckily http://sourceforge.net/mailarchive/message.php?msg_name=1204520414.7055.4.ca... pointed to the correct line in dimension.py.
I'll try to find the time to update the wiki page a bit in a week or so, I probably won't have time the next days.
That would be much appreciated. I wonder if we should make a copy of that old page and update the copy for the lxml api.
Is there something I can read up about changes to the python scripting interface (inkex.py and a few other modules, afaict at the moment) that might be planned or will it stay more or less as it is for the near future?
I don't think there are any major changes planned unless someone with a lot of energy steps forward. Many people have said they wanted to clean up and extend the python scripts, but thus far no one has followed through.
Aaron Spike
Hello!
On Friday 01 August 2008, Aaron Spike wrote:
I'll try to find the time to update the wiki page a bit in a week or so, I probably won't have time the next days.
That would be much appreciated. I wonder if we should make a copy of that old page and update the copy for the lxml api.
http://wiki.inkscape.org/wiki/index.php/PythonEffectTutorial should work now with lxml, I'd appreciate it if someone could test it independently and report issues, either here on on that site's discussion page.
I don't think there are any major changes planned unless someone with a lot of energy steps forward. Many people have said they wanted to clean up and extend the python scripts, but thus far no one has followed through.
Same problem as with all projects, not enough developers and time? ;-) Who knows, I might find some time in a few months to at least enhance the current behaviour a bit.
Daniel Hornung
participants (2)
-
Aaron Spike
-
Daniel Hornung