Get UV's from boundary loop

I’m trying to write a plugin that exports the UV layout to a .SVG file, but only the boundary edges.

It will be part of my seams to sewingpatterns addon, so I’m sure that there will be only one possible UV ‘edge’ for each of those outer edges.

Right now I’m getting the boundary loop (mesh.regiontoloop()), and I get each link_loops[0] associated with that edge. It gets me all the points correctly:

image
(screenshot from inkscape)

But not in the right order:
image

What should I do here?

import bpy
import bmesh

svgstring = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1">'
svgstring += '\n<defs><style>.seam{}</style></defs>'
svgstring += '\n<g>'
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_mode(type="EDGE")
bpy.ops.mesh.select_all(action='SELECT')

obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)

bpy.ops.mesh.region_to_loop()

boundary_loop = [e for e in bm.edges if e.select]

uv_layer = bm.loops.layers.uv.active
svgstring += '<path d="m'

for e in boundary_loop:

    uv1 = uv = e.link_loops[0][uv_layer].uv
    svgstring += str(uv1.x)
    svgstring += ','
    svgstring += str(uv1.y)
    svgstring += ' '

svgstring += '"/>'

svgstring += '\n</g></svg>'
print(svgstring)

Cheers,
Thomas.

the code you’ve written will select all boundary mesh edges, not UV boundaries. region_to_loop is not a UV operator. there is a uv operator to mark all UV boundaries as seams, from there you could just look for e.seam being true.

I think I have figured it out.
Getting the link_loop[0] of any BMEdge will result in the first loop of that edge. (in my case, because it’s a boundary edge, it will contain exactly one loop)
Getting link_loop[0].link_loop_next gets me the second loop of that edge.

These 2 loops contain the UV coordinates of the first and second vertex of that edge.
With this collection of UV pairs I can make edge segments, which I export as SVG

My next step would be to group these loose segments into “ring” groups, to chain them one after another.
This way I can export each SVG shape as a group instead of loose paths.

yes, this is technically the “correct” way to do it, walking bmloops- but I can tell you from personal experience that it is significantly slower than just using the built in operators. you may not notice on lightweight meshes but on dense meshes the difference is staggering.