Importing paths from SVG fails for some image files

I imported a path from external SVG files to the layer l by using the Python script below:

url = NSURL.alloc().initFileURLWithPath_(file_path)
l.importOutlinesFromURL_scale_error_(url, 1, None)
	
for p in l.paths:
	p.selected = True
	p.setAttribute_forKey_(10, "strokeWidth")
	p.setAttribute_forKey_(10, "strokeHeight")
	
# Referenced https://github.com/mekkablue/Glyphs-Scripts/blob/master/Spacing/Center%20Glyphs.py
o = l.selectionBounds.origin
sz = l.selectionBounds.size
	
for i in l.selection:
	if isinstance(i, GSNode):
		i.x += (0.5 * (l.width - sz.width) - o.x)

In most cases, the code works well. However, for some SVG files, they are not correctly imported as shown in the screenshot.

Since I’m a new user, I cannot attach any files here. Therefore, I put the sources of the SVG file causing the problem directly below.

<?xml version="1.0" ?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink" baseProfile="full" height="600px" version="1.1" viewBox="1653.8671245 2061.5147245000003 87.3647510000001 96.5015509999996" width="544px">
	<defs/>
	<path d="M 1664.732,2069.59 C 1664.732,2069.59 1666.972,2072.239 1669.315,2076.212 C 1671.6580000000001,2080.185 1674.102,2085.483 1674.51,2090.78 C 1675.327,2101.375 1671.615,2120.4460000000004 1671.615,2120.4460000000004 M 1698.366,2109.078 L 1698.366,2142.995 L 1698.366,2109.078 M 1661.181,2149.941 L 1733.9180000000001,2142.9939999999997 M 1673.384,2109.078 L 1711.851,2105.809 L 1708.172,2069.59 C 1708.172,2069.59 1697.392,2073.118 1691.3410000000001,2073.936 C 1685.2900000000002,2074.7540000000004 1669.315,2076.212 1669.315,2076.212" fill="none" stroke="#000000" stroke-width="0.08035099999999966"/>
</svg>

I’d like to know whether there is a problem with my script or the SVG file itself.
Also, I’m wondering how to make the script imports SVG files correctly.

Thank you.

In your script you seem to try to get the bounding rect from the layer selection, and then move the nodes’ x coordinates somehow in relation to that.

Why are you doing those 2 parts? What are you trying to achieve with the layer selection and with moving the nodes’s x coordinatses?

Wouldn’t it be enough to stop after setting the path attributes (in your case if you want the stroke weight and width, or even after just importing the outlines from url)?

I don’t know why your layer would have a selection set, after importing the svg, so that looks suspicious to me to use that selection (which might be none or a zero rect) for node positioning.

Thanks for the sample file. It has a unusual view box setup:

viewBox="1653.8671245 2061.5147245000003 87.3647510000001 96.5015509999996"

A rather big origin offset. Glyphs didn’t handle that correctly. I fixed it.