I'm making a python extension for inkscape .91, and need to Retrieve a selected objects id.
I can't find an example of this, and don't know enough to figure it out.
Any help would be appreciated.
Thanks.
Retrieve selected objects id ?
Re: Retrieve selected objects id ?
Oh, I just heard about a tutorial for making extensions, that's written for non-developers. Maybe it would help? https://medium.com/@xaviju/inkscape-ext ... 72dda360fe
Otherwise, you could try the wiki articles listed here: https://inkscape.org/en/develop/#extns
An idea from my simple-minded....mind (hah!). Maybe you could look at how other similar extensions are written -- other extensions that retrieve the id?
Edit
When your extension is finished, would you please try to have it listed in the repository? http://wiki.inkscape.org/wiki/index.php ... Repository To do that, you just need to ask for edit permissions on the developer mailing list https://inkscape.org/en/community/mailing-lists/ Or at least post a link in the forum to the download?
And here's some more info from ~suv: viewtopic.php?f=5&t=8826#p32144
Otherwise, you could try the wiki articles listed here: https://inkscape.org/en/develop/#extns
An idea from my simple-minded....mind (hah!). Maybe you could look at how other similar extensions are written -- other extensions that retrieve the id?
Edit
When your extension is finished, would you please try to have it listed in the repository? http://wiki.inkscape.org/wiki/index.php ... Repository To do that, you just need to ask for edit permissions on the developer mailing list https://inkscape.org/en/community/mailing-lists/ Or at least post a link in the forum to the download?
And here's some more info from ~suv: viewtopic.php?f=5&t=8826#p32144
Basics - Help menu > Tutorials
Manual - Inkscape: Guide to a Vector Drawing Program
Inkscape Community - Inkscape FAQ - Gallery
Inkscape for Cutting Design
Manual - Inkscape: Guide to a Vector Drawing Program
Inkscape Community - Inkscape FAQ - Gallery
Inkscape for Cutting Design
Re: Retrieve selected objects id ?
Thanks, brynn.
I did find something in Python modules for extensions, self.doc_ids !
The problem is it returns all objects/paths ids in a given layer.
The good thing is it returns them all in a list.
Not sure how to limit the list to self selected objects, but at least I have a starting point.
If anyone has any better ideas,I'm all ears.
I did find something in Python modules for extensions, self.doc_ids !
The problem is it returns all objects/paths ids in a given layer.
The good thing is it returns them all in a list.
Not sure how to limit the list to self selected objects, but at least I have a starting point.
If anyone has any better ideas,I'm all ears.
Re: Retrieve selected objects id ?
Look into an extension that only modifies selected items, like measure.py.
I would think that
for id, node in self.selected.iteritems()
will be what you are looking for.
I would think that
for id, node in self.selected.iteritems()
will be what you are looking for.
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!
Inkscape FAQ - Learning Resources - Website with tutorials (German and English)
Inkscape FAQ - Learning Resources - Website with tutorials (German and English)
Re: Retrieve selected objects id ?
Thanks, Moini.
Yep, that's the ticket. It was sitting in front of my face the whole time.
for id, node in self.selected.iteritems():
if id == bla:
#do something
I had tried...
if id in self.selected.iteritems() == bla:
and it did not work.
Shouldn't this work too?
Yep, that's the ticket. It was sitting in front of my face the whole time.
for id, node in self.selected.iteritems():
if id == bla:
#do something
I had tried...
if id in self.selected.iteritems() == bla:
and it did not work.
Shouldn't this work too?
Re: Retrieve selected objects id ?
That can't work, unless "bla" is also a tuple (or list, or other data structure) consisting of an id and a node - you can't compare a string with a tuple and expect to get a match
If you want to know exactly what it is, you can try:
for thing in self.selected.iteritems():
print(type(thing))
Every item in self.selected seems to be a tuple (list, whatever) consisting of two single items, the id of the object and whatever node is (probably some XML node...? No idea, never wrote an extension ).
What will your extension do?
If you want to know exactly what it is, you can try:
for thing in self.selected.iteritems():
print(type(thing))
Every item in self.selected seems to be a tuple (list, whatever) consisting of two single items, the id of the object and whatever node is (probably some XML node...? No idea, never wrote an extension ).
What will your extension do?
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!
Inkscape FAQ - Learning Resources - Website with tutorials (German and English)
Inkscape FAQ - Learning Resources - Website with tutorials (German and English)
Re: Retrieve selected objects id ?
I've been working on some extensions for architectural plans.
The one I need this bit of code for, is so two extensions can work together.
The first extension creates an object and gives it a coded id.
Then the second extension can get selected object, read the id, and direct it to an appropriate action.
I'm going to try to use this to create elevation plans from floor plans.
Is there an easier way to do this?
The one I need this bit of code for, is so two extensions can work together.
The first extension creates an object and gives it a coded id.
Then the second extension can get selected object, read the id, and direct it to an appropriate action.
I'm going to try to use this to create elevation plans from floor plans.
Is there an easier way to do this?
Re: Retrieve selected objects id ?
Besides
there is also
and
Note that dictionaries are not sorted - if you depend for example on selection order, use self.options.ids (a sorted list of ids of selected objects).
For example:
Code: Select all
for id_, node in self.selected.iteritems():
inkex.debug('ID: {0}\nTag: {1}'.format(id_, node.tag))
there is also
Code: Select all
# iterate the keys of the dictionary entries
for id_ in self.selected.keys():
inkex.debug('ID: {0}'.format(id_))
and
Code: Select all
# iterate the values of the dictionary entries
for node in self.selected.itervalues():
inkex.debug('Tag: {0}'.format(node.tag))
Note that dictionaries are not sorted - if you depend for example on selection order, use self.options.ids (a sorted list of ids of selected objects).
For example:
Code: Select all
# look up XML node of the first selected object
if len(self.selected):
node = self.selected[self.options.ids[0]]
inkex.debug('First: {0}'.format(node.get('id')))
Re: Retrieve selected objects id ?
Thanks, for the examples.
I defiantly have a better idea of what I'm doing now.
Time to get to work, and see if I can fit all these pieces together!
I defiantly have a better idea of what I'm doing now.
Time to get to work, and see if I can fit all these pieces together!
Re: Retrieve selected objects id ?
It works!
But, I just realized that if you duplicate an object you get a whole new the id.
Noticed that a duplicated object keeps its original label, and was wondering if is possible to use inkscape:label.
If so, does anyone know how to get a selected items label?
Here's a sample of what I'm trying to do.
But, I just realized that if you duplicate an object you get a whole new the id.
Noticed that a duplicated object keeps its original label, and was wondering if is possible to use inkscape:label.
If so, does anyone know how to get a selected items label?
Here's a sample of what I'm trying to do.
Code: Select all
for id, node in self.selected.iteritems():
if id[0:3] == 'cat':
#do something with id[5:10]
if id[0:3] == 'dog':
#do something with id[11:20]
Re: Retrieve selected objects id ?
draftman wrote:Noticed that a duplicated object keeps its original label, and was wondering if is possible to use inkscape:label.
Code: Select all
for id_, node in self.selected.iteritems():
# get value of attribute 'inkscape:label' from current node
node_label = node.get(inkex.addNS('label', 'inkscape'), "No label set for this element")
# report back
inkex.debug("Label of object %s: %s" % (id_, node_label))
# if label starts with "foo", modify it
if node_label.startswith("foo"):
new_label = node_label[0:3] + "bar"
# set label to new value
node.set(inkex.addNS('label', 'inkscape'), new_label)
# report back current value of attribute 'inkscape:label'
inkex.debug("Label of object %s: %s" % (id_, node.get(inkex.addNS('label', 'inkscape'))))
Re: Retrieve selected objects id ?
Thanks, suv.
That well work.
That well work.