A sorted ring of verts...

Is there an easy way to take a polyline and get an ordered list of connected points?

I’ve done it, but surely there is an internal way…


import bpy
import mathutils, math

print("

")

def poly_find_edge(o,v):
    for ed in o.data.edges:
        if ed.vertices[0]==v:
            return ed.index
        if ed.vertices[1]==v:
            return ed.index
    return

def poly_list_next_e(o,v,e):
    for ed in o.data.edges:
        if ed.vertices[0]==v:
            if ed.index!=e:
                return ed.index
        if ed.vertices[1]==v:
            if ed.index!=e:
                return ed.index

def poly_list_next_v(o,v,e):
    if o.data.edges[e].vertices[0]==v:
        return o.data.edges[e].vertices[1]
    return o.data.edges[e].vertices[0]  #   vertex

def poly_list(o):
    v_start=0
    v=0
    vv=[]
    vvp=[]
    
    e=poly_find_edge(o,v)
    print("e:",e)
    
    v=poly_list_next_v(o,v,e)   #   vertex
    print("v:",v)
    vv.append(v)
    vvp.append(o.data.vertices[v].co)
    e=poly_list_next_e(o,v,e)     #   edge
    print("e:",e)
    
    while v!=v_start:
        v=poly_list_next_v(o,v,e)   #   vertex
        vv.append(v)
        vvp.append(o.data.vertices[v].co)
        e=poly_list_next_e(o,v,e)     #   edge
    
    return (vv,vvp)

def conv_v2e(vvp):
    e=[]
    tot=len(vvp)
    for i in range(tot):
        e.append((vvp[i],vvp[(i+1)%tot]))
    return e

o=bpy.context.object
(vv,vvp)=poly_list(o)

print("vv:",vv)
print("vvp:",vvp)

e=conv_v2e(vvp)
xx=mathutils.geometry.tesselate_polygon(e)

print("List of tris:
",xx)

I also included a resort algorithm and the tesselate_polygon function, just because my searches for it yielded no results.

thanks for sharing…
all i know is the edge_loops_from_edges in bpy_extras… that works from a list of edges

rarebit, i wrote one polyline reorder algorithm, but it assumes the polyline is not closed. sorting-edge-keys-part-ii.html

Cheers all… not worked through everything yet (trying get something working to use in the Weekend Challenge, and yes it’d be quicker to do it by hand, but not as much fun!)

But here’s something from following up on liero’s reference:


import bpy
import bpy_extras
from bpy_extras import *
import mathutils, math

print("

")

def build_vlist(o,l):
    v=[]
    tot=len(l)
    for i in range(tot):
        v.append((o.data.vertices[e[i]].co,o.data.vertices[e[(i+1)%tot]].co))
    return v

o=bpy.context.object

el=bpy_extras.mesh_utils.edge_loops_from_edges(o.data)

tvl=[]

for e in el:
    vl=build_vlist(o,e)
    xx=mathutils.geometry.tesselate_polygon(vl)
    print("vl:
",vl)
    print("xx:
",xx)
    '''
    TODO:
    need to re-sort this new list of verts/edge numbers???
    
    use the 3 edges to add 6 vert indices to list then make unique
    '''