I have a list of vertices:
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].
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
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, e.vertices)): vecount.append(0) vecount[e.vertices] += 1 vecount[e.vertices] += 1 startidx = vecount.index(1)
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==curridx or e.vertices==curridx: if not e.vertices in doneidxs: toidx= e.vertices break; else: if not e.vertices in doneidxs: toidx= e.vertices 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 = edges[e.index] e.vertices = edges[e.index] 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)...