Retrieve selected objects id ?

Discussion about writing code for Inkscape.
draftman
Posts: 39
Joined: Sun Jan 25, 2015 5:29 am

Retrieve selected objects id ?

Postby draftman » Fri Oct 09, 2015 10:58 am

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.

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: Retrieve selected objects id ?

Postby brynn » Fri Oct 09, 2015 11:47 pm

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

draftman
Posts: 39
Joined: Sun Jan 25, 2015 5:29 am

Re: Retrieve selected objects id ?

Postby draftman » Sat Oct 10, 2015 8:25 am

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.

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: Retrieve selected objects id ?

Postby Moini » Sat Oct 10, 2015 10:29 am

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.
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)

draftman
Posts: 39
Joined: Sun Jan 25, 2015 5:29 am

Re: Retrieve selected objects id ?

Postby draftman » Sun Oct 11, 2015 5:21 am

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?

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: Retrieve selected objects id ?

Postby Moini » Sun Oct 11, 2015 6:33 am

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?
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)

draftman
Posts: 39
Joined: Sun Jan 25, 2015 5:29 am

Re: Retrieve selected objects id ?

Postby draftman » Sun Oct 11, 2015 2:10 pm

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?

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: Retrieve selected objects id ?

Postby ~suv » Sun Oct 11, 2015 6:15 pm

Besides

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')))

draftman
Posts: 39
Joined: Sun Jan 25, 2015 5:29 am

Re: Retrieve selected objects id ?

Postby draftman » Mon Oct 12, 2015 9:17 am

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!

draftman
Posts: 39
Joined: Sun Jan 25, 2015 5:29 am

Re: Retrieve selected objects id ?

Postby draftman » Thu Oct 15, 2015 10:11 am

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.

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]

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: Retrieve selected objects id ?

Postby ~suv » Thu Oct 15, 2015 10:41 am

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'))))

draftman
Posts: 39
Joined: Sun Jan 25, 2015 5:29 am

Re: Retrieve selected objects id ?

Postby draftman » Sat Oct 17, 2015 3:29 am

Thanks, suv.
That well work.


Return to “Programming”