The nested for loop that you posted above, loops each mesh, like we would chose team mates from a pool of players at a sports game. Nobody, can be chosen twice!
If you have four Meshes, at the first iteration, the first mesh (i = 0) is compared to the 2nd (j = i+1 = 1), 3rd (j = 2), and 4th (j= 3) mesh. At the 2nd iteration, the 2nd mesh (i = 1) is compared to the 3rd (j = i+1 = 2), and 4th mesh (j=3). At the 3rd iteration, the 3rd mesh (i = 2) is compared to only the 3rd mesh (j = i+1 = 3). At the fourth and last iteration (i=3), there are no meshes left to chose from, since j = i + 1 = 4 and there is no item beyond index 3.
Now, the problem with the duplicate 2nd mesh (from the top) is caused, because - at the first iteration - it’s closest neighbouring mesh is the 2nd one directly below. So the script attracts both meshes to each other and stores the new meshes in
At the 2nd iteration, however, the unchanged 2nd Mesh from Meshes (that has already been attracted and saved in the previous iteration) is evaluated with the 3rd mesh, as its closest neighbour. It performs the kissing action and both meshes are saved into
pulled_meshes. Unfortunately, the 2nd mesh gets stored twice, and that’s where the duplicates come from. Thanks for pointing that out!
You want to calculate all the vertex movements, save them per mesh vertex and average them later, in order to find its average movement and avoid duplicates? Seems to be a great work around!
I’ll also look into it tonight.
Instead of performing the vertex movement in the
pull_mesh() method, you could try to refractor the code.
A new method could for instance only return the movement vectors between two compared meshes. You would call it in the lower nested loop portion and save all the movement vectors in some kind of structured nested list. You should also take count of how many times a given mesh has been evaluated, since you will probably need that later for averaging the movement vectors.
Another method, can then do the averaging and perform the vertex moving.