Page MenuHome

Fix offset while import SVG files
ClosedPublic

Authored by Michael Soluyanov (crantisz) on Mon, Sep 9, 12:33 PM.

Details

Summary

Look at this files, they are similar:

But when they import in blender it looks like this:

The file saved as Inkscape SVG moved to the top, regular SVG stays in place. But why?

As you see in the code, blender checks if SVG have an special attribute inkscape:version, and if it has one, it move all SVG to the top. The idea is to match bottom right corner with world origin, instead top right corner:

if self._node.getAttribute('inkscape:version'):
    raw_height = self._node.getAttribute('height')
    document_height = SVGParseCoord(raw_height, 1.0)
    matrix = matrix @ matrix.Translation([0.0, -document_height , 0.0])

But why height is not equal the real height of the SVG? Well, because it's not a height of SVG itself, it is size of SVG on printing or displaying in web-page or in previewer. The real height of SVG in SVG-units is located in viewbox attribute:

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

So I suggest to move SVG content to real height of SVG located in viewbox attribute:

Next thing that I suggest in this diff, is thy inkscape-created files have this offset, but other files not? This 2 files identical

Every program will display it similar, but only Blender checks inkscape:version to moves graphics in viewport. All files should import in same algorithm because inkscape:version gives no difference to SVG-code!

As result all SVG files import identically, bottom right corner matches with world origin:

Diff Detail

Event Timeline

Michael Soluyanov (crantisz) edited the summary of this revision. (Show Details)

viewbox[1] with "-" is correct.

viewbox[1] specify offset of SVG. It it is almost never used

Not sure who is appropriate reviewer for this, add-on author says @jean-michel soler (jms) & @Sergey Sharybin (sergey)
Settings Add-ons as the reviewer will spam everyone with commit access it seems.

I'm working in big changes in this add-on to import SVG to Grease Pencil. I will test you code in my version to see if works.

Changed reviewers here, jms has not been active for some time, Sergey is listed in case he's interested in following.
Adding @Antonio Vazquez (antoniov) as he's rewriting the addon. No use to add a patch until he's ready to accept or not in the course of his rewrite.

Adding @Antonio Vazquez (antoniov) as he's rewriting the addon. No use to add a patch until he's ready to accept or not in the course of his rewrite.

In this case I added @Antonio Vazquez (antoniov) to this diff https://developer.blender.org/D4319 also

Comment Removed

@Antonio Vazquez (antoniov), can the Grease Pencil properly represent cubic/bezier splines (with proper handles, without tessellating them) ?

@Sergey Sharybin (sergey) Actually no, only can draw strokes using line segments. Now, to draw the beziers, the curve is converted to all line segments. We want work in the future in a new Curve type strokes using Bezier with the corresponding handles.

@Antonio Vazquez (antoniov), then i guess ideal scenario would be to use existing SVG addon as a base, and allow it to output either curves or grease pencil, and keep the underlying logic of transforms, instancing, etc shared across those.
Also means this patch is still relevant.

@Antonio Vazquez (antoniov), then i guess ideal scenario would be to use existing SVG addon as a base, and allow it to output either curves or grease pencil, and keep the underlying logic of transforms, instancing, etc shared across those.
Also means this patch is still relevant.

@Sergey Sharybin (sergey) This is exactly what I did, it's not a new addon...internally it uses the Curve conversion and then convert to GPencil strokes using the Convert Curve to GPencil operator code. The addon works in a two steps process: SVG->Curves->GPencil. Of course the curves and curve materials are removed in the conversion. Also, I keep the old SVG->Curve conversion. This is the reason why I implemented first an operator to convert Curves to GPencil. In this way all the conversion is done in C code and the addon only calls a RNA api to make the "real" conversion to strokes. We have tested with complex files and works very fast.

EDIT: And yes, this patch is relevant.

I have tested and works as expected. Not sure if the code has any issue. Anyway, the patch is ok with the code in development (no merge conflicts), so no problem to move it to master.

This revision is now accepted and ready to land.Tue, Sep 10, 11:11 AM

@Michael Soluyanov (crantisz) Do you have commit rights to apply the patch?

@Michael Soluyanov (crantisz) Do you have commit rights to apply the patch?

No, I haven't any commit rights

@Sergey Sharybin (sergey) If you agree, I can commit (setting the right author) D5727 and D4319.

@Antonio Vazquez (antoniov), yes please go ahead! I just didn't have time yet to commit. If you take care of this that'd be great :)