Sorting Vertices on a path / 1st attempt to scripting
What I have: A mesh object in the form of a path. The indices of the individual vertices and edges correspond to the order of creation, but not to the direction of the path. What I want: The vertices and edges should be in the order of the path's direction. While playing around with KDTree(), I discovered that it doesn't take the path itself into account, but only considers the distances between the individual vertices. Unfortunately, that wasn't what I needed. So I put together my own script that proceeds as follows: 1. Create a list with (number of edges plus 1) entries. Each entry is 0, and the index corresponds to the index of the vertices of the unordered path. 2. Iterate over all edges and increment the value in the list corresponding to the index by 1. This gives me a list of all vertices, along with the value of how many edges are connected to each vertex. The two indices, each containing only the value 1, represent the starting and ending points. 3. Take the first index, which has the value 1, and determine the corresponding index of the edge. Since the index of one vertex is already known, the other vertex becomes the new starting point. 4. Iterate over all edges again, ignoring the first edge, and find the other vertex each time. Write the result in a new list. 5. Repeat step 4 for all remaining edges. 6. The new list contains the indices of the vertices in the direction of the path. From this, the coordinates can be read, and a new path can be created in the same order, with the vertices in the order of the path's direction. I'm sure there are much simpler and more elegant ways to do this. There may even be ready-made methods in Blender for this. This was my first attempt, and it actually works. But how can I do it more elegantly? What are things i should not do? (When I learned to program more than 40 years ago, spaghetti code was common—and that's exactly what my script looks like.)