Duplicating objects in a Python extension
Hi all,
Given a selected object (e.g. firstObject = self.selected[self.options.ids[0]]) in a Python extension, how can I duplicate it? I've checked a couple of code snippets (e.g. pathscatter.py as well as some online content) and stumbled upon copy.deepcopy(...) and append() for layers, but I don't have it working properly yet.
Usually, I add a new object (e.g. a path) using
newObject = inkex.etree.SubElement(self.current_layer, inkex.addNS('path','svg'), pathAttribs),
where pathAttribs has been defined before. Can I add the duplicated object in a similar way? When I substitute 'pathAttribs' with 'firstObject' or a deepcopy() of it, I get the following FutureWarning —
FutureWarning: The behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead. newObject = inkex.etree.SubElement(self.current_layer, inkex.addNS('path','svg'), firstObject)
As an exercise I'm trying to write an extension that generates a circular array of a selected (path) object. The approach could be to first duplicate the object and subsequently transform it (i.e. rotate and possibly translate) using simpletransform.composeTransform () and simpletransform.applyTransformToPath().
As a follow-up question, is it possible to clone an object from within a Python extension? With 'clone' I mean the operation that's available in Inkscape through Edit → Clone → Create Clone.
Thanks! Best, Pieter
Hi Pieter,
Are you using the old inkscape 0.92 extensions or the new inkscape 1.0 extensions API (in trunk and gitlab.com/inkscape/extentions/)
The basics are right, you need to duplicate the object and any children (for instance in case of text). Transforms can be applied ontop of any existing transformation.
Best Regards, Martin Owens
On Fri, 2018-11-02 at 00:11 +0100, Pieter Barendrecht wrote:
Hi all,
Given a selected object (e.g. firstObject = self.selected[self.options.ids[0]]) in a Python extension, how can I duplicate it? I've checked a couple of code snippets (e.g. pathscatter.py as well as some online content) and stumbled upon copy.deepcopy(...) and append() for layers, but I don't have it working properly yet.
Usually, I add a new object (e.g. a path) using
newObject = inkex.etree.SubElement(self.current_layer, inkex.addNS('path','svg'), pathAttribs),
where pathAttribs has been defined before. Can I add the duplicated object in a similar way? When I substitute 'pathAttribs' with 'firstObject' or a deepcopy() of it, I get the following FutureWarning —
FutureWarning: The behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead. newObject = inkex.etree.SubElement(self.current_layer, inkex.addNS('path','svg'), firstObject)
As an exercise I'm trying to write an extension that generates a circular array of a selected (path) object. The approach could be to first duplicate the object and subsequently transform it (i.e. rotate and possibly translate) using simpletransform.composeTransform () and simpletransform.applyTransformToPath().
As a follow-up question, is it possible to clone an object from within a Python extension? With 'clone' I mean the operation that's available in Inkscape through Edit → Clone → Create Clone.
Thanks! Best, Pieter
Inkscape-devel mailing list Inkscape-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/inkscape-devel
Hi Martin,
I'm using 0.92 at the moment. New extensions API, is there already some documentation about that? On one of the Inkscape forums I also read something about C++ extensions being supported in the (near) future?
Back to the question, I'm not sure why I'm getting that FutureWarning. I assumed that the last argument of inkex.etree.SubElement is a node (formatted as a dictionary), and that self.selected[self.options.ids[0]] gives me such an object. Apparently, one of these assumptions is not correct?
Best, Pieter
On Fri, 2 Nov 2018 at 02:13, <doctormo@...400...> wrote:
Hi Pieter,
Are you using the old inkscape 0.92 extensions or the new inkscape 1.0 extensions API (in trunk and gitlab.com/inkscape/extentions/)
The basics are right, you need to duplicate the object and any children (for instance in case of text). Transforms can be applied ontop of any existing transformation.
Best Regards, Martin Owens
On Fri, 2018-11-02 at 00:11 +0100, Pieter Barendrecht wrote:
Hi all,
Given a selected object (e.g. firstObject = self.selected[self.options.ids[0]]) in a Python extension, how can I duplicate it? I've checked a couple of code snippets (e.g. pathscatter.py as well as some online content) and stumbled upon copy.deepcopy(...) and append() for layers, but I don't have it working properly yet.
Usually, I add a new object (e.g. a path) using
newObject = inkex.etree.SubElement(self.current_layer, inkex.addNS('path','svg'), pathAttribs),
where pathAttribs has been defined before. Can I add the duplicated object in a similar way? When I substitute 'pathAttribs' with 'firstObject' or a deepcopy() of it, I get the following FutureWarning —
FutureWarning: The behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead. newObject = inkex.etree.SubElement(self.current_layer, inkex.addNS('path','svg'), firstObject)
As an exercise I'm trying to write an extension that generates a circular array of a selected (path) object. The approach could be to first duplicate the object and subsequently transform it (i.e. rotate and possibly translate) using simpletransform.composeTransform () and simpletransform.applyTransformToPath().
As a follow-up question, is it possible to clone an object from within a Python extension? With 'clone' I mean the operation that's available in Inkscape through Edit → Clone → Create Clone.
Thanks! Best, Pieter
Inkscape-devel mailing list Inkscape-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/inkscape-devel
On Fri, 2018-11-02 at 11:04 +0100, Pieter Barendrecht wrote:
Hi Martin,
I'm using 0.92 at the moment. New extensions API, is there already some documentation about that? On one of the Inkscape forums I also read something about C++ extensions being supported in the (near) future?
We support C++ contrib features (I don't know why we call them extensions, they have nothing to do with the extensions in python).
Back to the question, I'm not sure why I'm getting that FutureWarning. I assumed that the last argument of inkex.etree.SubElement is a node (formatted as a dictionary), and that self.selected[self.options.ids[0]] gives me such an object. Apparently, one of these assumptions is not correct?
That would be an lxml question specifically. I haven't seen this specific warning when rewriting the new API, but it could be related to the version of lxml. I would like to incorporate the general feature of cloning elements into the new API once you've worked out how to do it without warnings.
Best Regards, Martin Owens
Any other thoughts on the above? Duplicating is a fairly basic/common operation, it would be good to include it on the Python extension Wiki pages (I wouldn't mind writing it up once I have it figured out).
Pieter
On Fri, 2 Nov 2018 at 14:52, <doctormo@...400...> wrote:
On Fri, 2018-11-02 at 11:04 +0100, Pieter Barendrecht wrote:
Hi Martin,
I'm using 0.92 at the moment. New extensions API, is there already some documentation about that? On one of the Inkscape forums I also read something about C++ extensions being supported in the (near) future?
We support C++ contrib features (I don't know why we call them extensions, they have nothing to do with the extensions in python).
Back to the question, I'm not sure why I'm getting that FutureWarning. I assumed that the last argument of inkex.etree.SubElement is a node (formatted as a dictionary), and that self.selected[self.options.ids[0]] gives me such an object. Apparently, one of these assumptions is not correct?
That would be an lxml question specifically. I haven't seen this specific warning when rewriting the new API, but it could be related to the version of lxml. I would like to incorporate the general feature of cloning elements into the new API once you've worked out how to do it without warnings.
Best Regards, Martin Owens
participants (2)
-
unknown@example.com
-
Pieter Barendrecht