1
$\begingroup$

I have a list of vertices:

edge loop order

But the list looks like this: [0, 1, 2, 3, 4, 5, 6, 7].

I need a script to sort the list to be like the order in the loop itself: [3, 2, 1, 0, 4, 5, 7, 6].

$\endgroup$
2
$\begingroup$

What list are you referring to?

the numbers shown in the image are indexes of each vertex in the obj.data.vertices list; a list of these indexes: [0,1,2,3,4,5,6,7] (8 vertices).

In the edges in obj.data.edges list you'll find the indexes of vertices that define the edge; sample: [[0,1],[1,2],[2,3],[0,4],[4,5],[5,7],[6,7]] (8 edges).

It feels like you want to reorder edges so you can loop them based on their index in obj.data.edges; sample: [[3,2],[2,1],[1,0],[0,4],[4,5],[5,7],[7,6]].

In that case you'll need to search for a start vertex the loop. A vertex which belongs to only one edge is a fitting approach. There are 2 of those in your sample, in this code the first appearing in obj.data.vertices list is selected:

vecount = []
for e in obj.data.edges:
    while(len(vecount)-1 < max(e.vertices[0], e.vertices[1])):
        vecount.append(0)
    vecount[e.vertices[0]] += 1
    vecount[e.vertices[1]] += 1    
startidx = vecount.index(1)

Now obj.data.vertices[startidx] is the starting point of the loop Now you can create an array of edges, sorted from starting point towards end point:

edges = []
doneidxs = [startidx]
curridx = startidx
while (len(doneidxs ) != len(obj.data.vertices)):  
    for e in obj.data.edges:
        if e.vertices[0]==curridx or e.vertices[1]==curridx:
            if not e.vertices[0] in doneidxs:
                toidx= e.vertices[0]
                break;
            else:
                if not e.vertices[1] in doneidxs:
                    toidx= e.vertices[1]    
                    break;       
    edges.append([curridx, toidx])
    doneidxs.append(toidx)
    curridx = toidx

Now you can apply the new order of edges by updating vertex indexes in the edge vertices arrays:

for e in obj.data.edges:
    e.vertices[0] = edges[e.index][0]
    e.vertices[1] = edges[e.index][1]
obj.data.update()

Run this code having the mesh selected (3D View, OBJECT mode), starting with pointing out that's the object you want the code to work on:

obj = context.active_object
...(above code)...
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.