Hey all, I have done a bit of Java/PHP/Javascript/Perl but never Python so getting into it now. Just trying to create a simple extension. I saw a couple simple extensions tutorials (both were outdated and used the older syntax).
My question is I'd like to loop through some objects, sort them by their X value, then copy them and translate their position.
For now, I'm just moving it 50 pixels down and 50 pixels to the right... But the X,Y values I get seem to be off (the bounding_box's center x,y points seems to be off). Here is the main part of my code:
This is after I create a list with the elements inside the group sorted by X location...
The 1st element it output has this value for x,y: 0... path; 26.06, 9.91; translate(76.06, 59.91)
so it outputs 26.06, 9.91 (which I then translate each coordinate +50)... But I would expect the x, y coordinates to be closer to this ~ (2, 9) value as seen in attached png:
I attached my extension code so far and the example .svg file that Im using to check the X,Y coordinates (think you have to click in the left Attachments button area to get them) ...
So don't pay attention to the GUI parameters in the Extension's dialog popup yet. I don't use them yet. Right now I'm just trying to get the accurate x,y coordinates and translate 50,50pixels. So you can select the top grouped object, and then goto Extensions->'Objects around Circle', then click Apply, which then just shows output on the current X,Y coordinates for each of the 88 objects in that top group (these values seem to be all wrong (i expect X values from around 2 to 608 but instead get values from around 26 to 186).
The bottom thing in the svg is the result that I want my extension to do based on some params (angles between each item). But I don't need help with that complex stuff yet. Just the simple stuff for now and I may be able to figure out the hard stuff :).
Thanks!, Ari
NOTE: I'm looking for the absolute x,y coordinates.... even if the container group has a transform/translate on it... I want to know the real x,y coordinates not what x/y attribute value is set on the svg's XML (since that x/y xml value has to be added onto any parent groups x/y/tranform/translate values... thought maybe the api had a simple way to just access the real coordinates).
Hey @inklinea, My question is even simpler than that... just how to get accurate X,Y coordinates :). I haven't even uncommented my sin/cos angle pseudo-code :) to rotate the object.
UPDATE: i think the weird thing is that Inkscape, in the Document Properties, says its in px. Yet the python code seems to say the document's units is in mm (checking via self.svg.unit)...
So I assume I need to convert to px ..... and I try to convert to pix via uutounit and back to mm as well via uutounit (since the document supposedly is in mm).
bbox = elem_list_item.bounding_box() # money maker
x, y = bbox.center # grab center
x_px = self.svg.uutounit(x,'px')
y_px = self.svg.uutounit(y,'px')
x_px_move = 0.0 #in pixels
y_px_move = 50.0 #in pixels
new_x = x_px + x_px_move
new_y = y_px + y_px_move
#convert back to mm since the document seems to use mm
new_x_mm = self.svg.uutounit(new_x,'mm')
new_y_mm = self.svg.uutounit(new_y,'mm')
#then try translating or setting x and y directly
#translate_cmd = 'translate(%.2f, %.2f)' % (new_x_mm, new_y_mm)
#elem_list_item_copy.set('transform', translate_cmd)
elem_list_item_copy.set('x', new_x)
elem_list_item_copy.set('y', new_y)
But it supposed to copy it 50px down (and not even modify the X coordinates), but it actually is way x and y off and looks a bit unusually spaced too (some circles seem off)...
Attached my latest code which duplicates/copies the element...
btw the issue I don't think is learning python3... you can translate any psuedo-code pretty quickly to a python equivalent without really knowing the language (like it took me a few hours to just make my current extension). I think my issue is just the inkex api doesn't work the way I expect with coordinates and may need a Inkscape Ext pro here to help understand what I'm doing wrong.
Maybe I just have to subtract my height from this Y value... will attempt modifying the code a bit later and see if that helps (at least for the Y side, not sure why the X would be off... may have to research this statement later: "Even if the user switch everywhere the unit to mm, inkscape converts it to px unit and writes it in the .svg file resulting inaccuracies.")
Maybe I just have to subtract my height from this Y value... will attempt modifying the code a bit later and see if that helps (at least for the Y side, not sure why the X would be off... may have to research this statement later: "Even if the user switch everywhere the unit to mm, inkscape converts it to px unit and writes it in the .svg file resulting inaccuracies.")
EDIT: I see some of my elements have random transform="scale(-1,1)" on some circle elements which throw things off for some of the circles. Trying to think how to get past that now... I just want the abs x,y coordinates if that is possible (but guessing with all the possibilities of transforms, cx/cy, vs x/y attributes, parent group elements x/y values influencing the final x/y value this might be hard to figure out).
Hey all,
I have done a bit of Java/PHP/Javascript/Perl but never Python so getting into it now. Just trying to create a simple extension.
I saw a couple simple extensions tutorials (both were outdated and used the older syntax).
I did find the documentation:
https://inkscape.gitlab.io/extensions/documentation/inkex.transforms.html?highlight=center_x
and this was the best newer syntax answer I found on the forum: https://inkscape.org/forums/extensions/accessing-and-changing-object-location/
My question is I'd like to loop through some objects, sort them by their X value, then copy them and translate their position.
For now, I'm just moving it 50 pixels down and 50 pixels to the right...
But the X,Y values I get seem to be off (the bounding_box's center x,y points seems to be off).
Here is the main part of my code:
This is after I create a list with the elements inside the group sorted by X location...
The 1st element it output has this value for x,y:
0... path; 26.06, 9.91; translate(76.06, 59.91)
so it outputs 26.06, 9.91 (which I then translate each coordinate +50)...
But I would expect the x, y coordinates to be closer to this ~ (2, 9) value as seen in attached png:
I attached my extension code so far and the example .svg file that Im using to check the X,Y coordinates (think you have to click in the left Attachments button area to get them) ...
So don't pay attention to the GUI parameters in the Extension's dialog popup yet. I don't use them yet. Right now I'm just trying to get the accurate x,y coordinates and translate 50,50pixels. So you can select the top grouped object, and then goto Extensions->'Objects around Circle', then click Apply, which then just shows output on the current X,Y coordinates for each of the 88 objects in that top group (these values seem to be all wrong (i expect X values from around 2 to 608 but instead get values from around 26 to 186).
The bottom thing in the svg is the result that I want my extension to do based on some params (angles between each item).
But I don't need help with that complex stuff yet. Just the simple stuff for now and I may be able to figure out the hard stuff :).
Thanks!,
Ari
NOTE: I'm looking for the absolute x,y coordinates.... even if the container group has a transform/translate on it... I want to know the real x,y coordinates not what x/y attribute value is set on the svg's XML (since that x/y xml value has to be added onto any parent groups x/y/tranform/translate values... thought maybe the api had a simple way to just access the real coordinates).
I'm learning python 3 too.
If you are working in degrees, does the code convert radians into degrees and vice versa.
math.
sin
(x) Return the sine of x radians.Hey @inklinea, My question is even simpler than that... just how to get accurate X,Y coordinates :). I haven't even uncommented my sin/cos angle pseudo-code :) to rotate the object.
UPDATE: i think the weird thing is that Inkscape, in the Document Properties, says its in px. Yet the python code seems to say the document's units is in mm (checking via self.svg.unit)...
So I assume I need to convert to px ..... and I try to convert to pix via uutounit and back to mm as well via uutounit (since the document supposedly is in mm).
But it supposed to copy it 50px down (and not even modify the X coordinates), but it actually is way x and y off and looks a bit unusually spaced too (some circles seem off)...

Attached my latest code which duplicates/copies the element...
btw the issue I don't think is learning python3... you can translate any psuedo-code pretty quickly to a python equivalent without really knowing the language (like it took me a few hours to just make my current extension).
I think my issue is just the inkex api doesn't work the way I expect with coordinates and may need a Inkscape Ext pro here to help understand what I'm doing wrong.
Btw I found this:
https://alpha.inkscape.org/vectors/www.inkscapeforum.com/viewtopic16ce.html?t=11620
https://wiki.inkscape.org/wiki/index.php/BlueprintRealUnit
"Inkscape use the SVG standard coordinate system, where the origin is at the top-left corner.
For humans the svg file is more readable if the coordinate system were at the bottom-left corner."
Maybe I just have to subtract my height from this Y value... will attempt modifying the code a bit later and see if that helps (at least for the Y side, not sure why the X would be off... may have to research this statement later: "Even if the user switch everywhere the unit to mm, inkscape converts it to px unit and writes it in the .svg file resulting inaccuracies.")
Btw I found this:
https://alpha.inkscape.org/vectors/www.inkscapeforum.com/viewtopic16ce.html?t=11620
https://wiki.inkscape.org/wiki/index.php/BlueprintRealUnit
"Inkscape use the SVG standard coordinate system, where the origin is at the top-left corner.
For humans the svg file is more readable if the coordinate system were at the bottom-left corner."
Maybe I just have to subtract my height from this Y value... will attempt modifying the code a bit later and see if that helps (at least for the Y side, not sure why the X would be off... may have to research this statement later: "Even if the user switch everywhere the unit to mm, inkscape converts it to px unit and writes it in the .svg file resulting inaccuracies.")
EDIT: I see some of my elements have random transform="scale(-1,1)" on some circle elements which throw things off for some of the circles. Trying to think how to get past that now... I just want the abs x,y coordinates if that is possible (but guessing with all the possibilities of transforms, cx/cy, vs x/y attributes, parent group elements x/y values influencing the final x/y value this might be hard to figure out).