I am updating an old extension of mine for Inkscape 1.0. However I cannot figure out how do I replace the following code:
# This function adds an element to the document
def addElement(self, element_type, group, element_attributes):
inkex.etree.SubElement(
group,
inkex.addNS(element_type, 'svg'),
element_attributes)
It just adds a new element to the document. How do I add an element to the document with the new 1.0 API? I know I can do "from lxml import etree" to get the definition for etree but how do I add it anywhere?
# This function adds an element to the document
def addElement(self, element_type, group, element_attributes):
inkex.etree.SubElement( group, inkex.addNS(element_type, 'svg'), element_attributes)
As far as I can see then you are just creating a standalone etree object that is not added anywhere? At least nothing is appearing in the visible document by doing that.
Actually and according to the lxml documentation if will add the element created using inkex.addNS to the group node.. as you can see using the toy code below:
from lxml import etree root = etree.Element("root") etree.SubElement(root,"a") etree.tostring(etree.ElementTree(root))
which (should) return '<root><a/></root>' . I'm not an inkscape expert, but as far as I understand inkex.etree was just a proxy to the lxml.etree ..
The group node comes from (in the old working extension):
# Group to put all the elements in, center set in the middle of the view
group = inkex.etree.SubElement(self.current_layer, 'g', {
inkex.addNS('label', 'inkscape'): 'Encoder disk',
'transform': 'translate' + str(self.view_center)
})
Which I updated to:
# Group to put all the elements in, center set in the middle of the view
group = etree.SubElement(
self.svg.get_current_layer(),
'g',
{ inkex.addNS('label', 'inkscape'): 'Encoder disk',
'transform': 'translate' + str(self.svg.namedview.center) })
But this does not seem to insert anything in the document.
On my side, using Inkscape 1.0beta2/OSX, I got an error with the transform attribute (no 'center' attribute for NameView..) thus I can't go further.. I test your group inside layer implementation on one of my extension and it works nicely without any attributes (but I gess that the label stuff won't be an issue).
For creating elements, you should simply make the element objects available in `inkex/elements.py`
import inkex
from inkex.elements import Group, PathElement
class MyExtension(inkex.Extension):
# ...
def effect(self):
layer = self.svg.add(Group.new('my_label', is_layer=True))
layer.append(self.make_a_shape())
def make_a_shape(self):
my_shape = PathElement()
# You can set the path through many different methods.
# Lists of numbers, pythonic objects, Cubic curves etc.
my_shape.path = "M 10 10 L 20 20 C 20 20 10 10 10 0 z"
# Transform can be modified in many ways too
my_shape.transform.add_translate(self.svg.namedview.center)
return my_shape
In the example above you can see using both append and add, add is like append but it returns the object so you can chain things together.
Once 1.0 is out, I'll have some time to complete the video series and how-to documents which explain all of this. For now, your best hope is to ignore all old extensions and focus on the newer and fresher extensions in the gitlab repository (some of the gitlab extensions are still older, so beware, if you see etree then it's old)
Hello!
I am updating an old extension of mine for Inkscape 1.0. However I cannot figure out how do I replace the following code:
It just adds a new element to the document. How do I add an element to the document with the new 1.0 API? I know I can do "from lxml import etree" to get the definition for etree but how do I add it anywhere?
from my little experience, use etree from lxml:
As far as I can see then you are just creating a standalone etree object that is not added anywhere? At least nothing is appearing in the visible document by doing that.
Actually and according to the lxml documentation if will add the element created using inkex.addNS to the group node.. as you can see using the toy code below:
which (should) return '<root><a/></root>' . I'm not an inkscape expert, but as far as I understand inkex.etree was just a proxy to the lxml.etree ..
The group node comes from (in the old working extension):
Which I updated to:
But this does not seem to insert anything in the document.
I cannot see anything else in the code that could somehow insert anything in visible document. You can see the whole code at https://github.com/Hyvok/Inkscape-rotary-encoder-disk-generator/blob/master/encoder_disk_generator.py
On my side, using Inkscape 1.0beta2/OSX, I got an error with the transform attribute (no 'center' attribute for NameView..) thus I can't go further.. I test your group inside layer implementation on one of my extension and it works nicely without any attributes (but I gess that the label stuff won't be an issue).
For creating elements, you should simply make the element objects available in `inkex/elements.py`
In the example above you can see using both append and add, add is like append but it returns the object so you can chain things together.
Once 1.0 is out, I'll have some time to complete the video series and how-to documents which explain all of this. For now, your best hope is to ignore all old extensions and focus on the newer and fresher extensions in the gitlab repository (some of the gitlab extensions are still older, so beware, if you see etree then it's old)