Inkscape.org
Creating New Extensions Document coordinates from mouse position
  1. #1
    gropiuswasright gropiuswasright @gropiuswasright

    Is there a way for an inkscape extension to know what document coordinates correspond to the current mouse position?

    I would like to obtain a similar effect as with Ctrl-v, where the object is pasted at the mouse coordinates, in order - as an example, but not exclusively - to create a dot where the mouse is.

    I am able to get mouse coordinates and window coordinates with command line tools, but cannot convert them to exact document coordinates because of toolbars etc, that offset the viewport coordinates.

    I am also thinking (but haven't tested) of pasting a 1x1 circle, getting its coordinates and deleting it, but it seems overly complex.

    Any ideas?

  2. #2
    inklinea inklinea @inklinea⛰️

    No it's not possible.

    Inkscape sends the svg file to the extension system as a custom element tree, the extension system sends it back after processing. 

    It's possible to send some actions directly to the gui before triggering an extension using dbus commands.

    However although there is 'cut' and 'copy' there is no 'paste' action.

    Otherwise, you could 'select-by-id' after creating a dummy object, 'copy' then 'paste' using dbus, then examine the centre of the bounding box of that dummy object to get the mouse position.

    There are python modules that relate to mouse position however, I don't know how you would relate screen / window position to zoom level etc.

     

  3. #3
    gropiuswasright gropiuswasright @gropiuswasright
    *

    thanks!

    that's too bad.

    I was thinking - screen info is passed to an extension via the sodipodi:namedview element, which by the way includes the zoom level and document center, so one could do some math and project the mouse position to document position. However that's messed up (i.e.: offset) by toolbars.

    Would it be a bad idea to pass mouse position converted to document position alongside that? or (but I guess that would break a lot of thing) as a parameter alongside selection and nodes?

    What would be the place to make such a feature request?

  4. #4
    inklinea inklinea @inklinea⛰️

    the sodipodi: and inkscape: namespaces are transparent to programs other than Inkscape. 

    (Unless a third party program is written to find them)

    However in this case it would be better just to attach the information to the element tree, and it would just be automatically discarded when the extension is returned.

    If you have a basic extension, if you add the following code at the top, just below the import statements:

     

    def get_attributes(self):

        attribute_string = ''
        for att in dir(self):
            try:
                attribute = (att, getattr(self, att))
                attribute_string = attribute_string + str(attribute) + '\n'
            except:
                None

        return attribute_string

    You can send any object to it and it will return a list of strings detailing methods and attributes of that object.

    You can print them in the Inkscape message box like so:

    inkex.errormsg(get_attributes(self.svg))

    for the svg for example 

    or

    inkex.errormsg(get_attributes(self.document))

    which contains extra document information

    If you send a path to the function you will see custom methods such as bounding_box()

    and so on.

     

Inkscape Inkscape.org Inkscape Forum Creating New Extensions Document coordinates from mouse position