In order to set a new definition I think I should pass something into get_template(). If someone has done this before and can share an example .. well that would be marvellous :-)
You shouldn't need to create a <defs> element if the file is open in Inkscape, as it will be automatically added ( you can see that in the Edit>XML Editor panel )
If you write a simple extension which deletes the <defs> element - Inkscape automatically regenerates it as soon as the extension exits.
Maybe this will do what you want:
import inkex
from lxml import etree
def add_to_defs(self, my_object):
my_object_id = my_object.get_id()
svg_defs = self.svg.xpath('//svg:defs')
svg_defs[0].append(my_object)
parent = self.svg
my_use_object = etree.SubElement(parent, inkex.addNS('use', 'svg'))
my_use_object.set('xlink:href', '#' + str(my_object_id))
class DefsMan(inkex.EffectExtension):
def effect(self):
my_selected = self.svg.selected
# Make sure something is selected
if len(my_selected) > 0:
for my_object in my_selected:
add_to_defs(self, my_object)
if __name__ == '__main__':
DefsMan().run()
Sorry it took a while to reply .. I got seriously distracted with another issue :-)
And thanks for sharing your code. It worked first time! But then I tried to extend it and Inkscape started crashing. Looking at the XML that is dumped prior to the crash, I can see that <defs /> contains the marker. Here is my amended version. Any idea what causes the crash?
I would like to make diagrams with arrow heads. Using Inkscape, with the Fill and Stroke tool I can generate XML that looks like
<defs id="defs11454">
<marker style="overflow:visible;" id="Arrow1Lend" ..
Programatically I can also read defs in Python with
def effect(self):
doc = self.get_template(width=w, height=h)
svg = doc.getroot()
defs = svg.defs
In order to set a new definition I think I should pass something into get_template(). If someone has done this before and can share an example .. well that would be marvellous :-)
Thanks
I've not made <defs> before:
You shouldn't need to create a <defs> element if the file is open in Inkscape, as it will be automatically added ( you can see that in the Edit>XML Editor panel )
If you write a simple extension which deletes the <defs> element - Inkscape automatically regenerates it as soon as the extension exits.
Maybe this will do what you want:
Sorry it took a while to reply .. I got seriously distracted with another issue :-)
And thanks for sharing your code. It worked first time! But then I tried to extend it and Inkscape started crashing. Looking at the XML that is dumped prior to the crash, I can see that
<defs />
contains the marker. Here is my amended version. Any idea what causes the crash?I think it might be because the <marker> you are adding is empty.
from inkex import Circle
I added this in the marker block
It's working now 😊
To finish the story here is a beautiful arrow and some ugly code.
Well the arrow was beautiful before the upload crushed it 😭
I'm glad it all worked out :)