Page MenuHome

Various improvements in svg import: Viewbox center alignment with world origin
Needs ReviewPublic

Authored by Michael Soluyanov (crantisz) on Feb 7 2019, 12:43 PM.

Details

Summary

Various improvements in svg import: Viewbox center alignment with world origin

The viewBox attribute defines the position and dimension, in user space, of an SVG viewport. So it will be better, if blender uses this value instead 'document_height' and only in inkscape-generated files.

Center of imported svg are in document center:

Updated version / 2.79 / file in inkscape;

Old code is wrong, because document height and element sizes are in different units!

Diff Detail

Event Timeline

Jacques Lucke (JacquesLucke) requested changes to this revision.Feb 7 2019, 1:07 PM
  • Do you have some example files that show how this is better?
  • Not sure if the default scale should be changed, and definitely not as part of the other changes.
  • Maybe it would be better if you split this patch up so that it is easier to see which code changes are responsible for which functionality changes.
io_curve_svg/import_svg.py
1482

I don't see the need for this comment. Just calculate the number at runtime. The performance overhead should be negilible.

This revision now requires changes to proceed.Feb 7 2019, 1:07 PM
Michael Soluyanov (crantisz) retitled this revision from Various improvements in svg import: to Various improvements in svg import: Viewbox center alignment with world origin.Feb 7 2019, 1:25 PM
Michael Soluyanov (crantisz) edited the summary of this revision. (Show Details)
Michael Soluyanov (crantisz) updated this revision to Diff 13548.

Split into parts; this part is about viewbox.

I'm not quite sure how to determine if this is the right thing to do.
Can it fail in some cases?
Will this have an impact on non-inkscape-created files?

Also there is the parse_array_of_floats function that you might want to use.

If the old code is wrong, then remove it instead of commenting it out (including the comment).

using parse_array_of_floats() and check if 4 elements are exists

I'm not quite sure how to determine if this is the right thing to do.
Can it fail in some cases?

added an length checking

Will this have an impact on non-inkscape-created files?

Viewbox is fundamental and basic SVG thing

https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox

It defines what part of infinite svg space will be displayed in Image(svg)-viewer

Also there is the parse_array_of_floats function that you might want to use.

applied

If the old code is wrong, then remove it instead of commenting it out (including the comment).

removed

Functionality wise this seems good to me, but @Sergey Sharybin (sergey) should have the final word as he maintains this mainly.

io_curve_svg/import_svg.py
1813

Style: if len(viewbox) == 4:

1814

Style and remove unnecessary conversion: center_x = (viewbox[2] + viewbox[0]) / 2

1817

Style: matrix = matrix @ matrix.Translation(Vector((-center_x, -center_y, 0.0)))

Not sure whether it's really best idea. Thing is, it might be handy for some simple art or references, but SVG might also be used for some CAD things where you don't want any unrequested modifications to the coordinates. We also don't do this for other formats.

I'll probably be fine if that's optional, but that goes to a higher levels of I/O plugins in Blender, so wouldn't mind hearing from @Bastien Montagne (mont29) and @Campbell Barton (campbellbarton).

Anyway, if you try


you will get

objects are moved in long distance from world origin. I think it was an idea to move center from bottom left corner to bottom right corner. But this doesn't works, because Inkscape generates height with units and geometry without units:

?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
....
   height="297mm"
   viewBox="0 0 210 297"
....

to make this works, we need to get height from viewBox attribute, not from height attribute. Because viewBox define what part of image displays, not height

Images (import images as plane) and new objects usually placed at center (at 3d cursor actually)

But, if you don't want to include it, please, just remove part, that it replaces - it wrong