0
$\begingroup$

I have the following situation. I want to save Vertices of vertexgroups of different objects in one long list. So basically I use vertex groups to mark and weight areas on different objects. Now I want to choose randomly one vertex which is weighted by the weight of this vertex by giving the list with a list of the weights to a random function.

All of this works perfectly fine as long as I only use one Object with one vertex group. As so as I add a loop through all mesh objects and expand each list to one another, i receive a weird outcome and I don't know even how to search for that:

Basically each vertex has a number in the scene it looks like somehow the vertexnumbers are in some way appending to each other. This is the outcome of the print statements in the code below:

Print statements of selected code

#loop through all objects
for item in bpy.data.objects:
            # test if object is mesh object
            if item.type == 'MESH':
                # check if vertex group WeightPlacement exists
                if item.vertex_groups.get("WeightPlacement") is not None:
                    # Deselect all objects and activate only the wanted item
                    bpy.ops.object.select_all(action='DESELECT')
                    bpy.context.view_layer.objects.active = item

                    # Make sure you're in edit mode
                    bpy.ops.object.mode_set(mode='EDIT')

                    # Deselect all verts
                    bpy.ops.mesh.select_all(action='DESELECT')

                    # Make sure the active group is the one we want
                    bpy.ops.object.vertex_group_set_active(group="WeightPlacement")

                    # Select the verts
                    bpy.ops.object.vertex_group_select()

                    # Get the selected verts
                    selVerts = [v for v in item.data.vertices if v.select]

                    # Get the index of the required group
                    index = item.vertex_groups["WeightPlacement"].index

                    #print(len(weightList))
                    # Get the vert weights

                    wL = [v.groups[index].weight for v in selVerts]
                    vL = [v for v in selVerts]
                    print([vL[-1]])
                    weightList.extend(wL)
                    vertexList.extend(vL)

                    print(len(weightList))

                    # Make sure you're in edit mode
                    bpy.ops.object.mode_set(mode='OBJECT')

Anybody an idea how this could have happened? Can't even think of an internal process which leads to this outcome? For clarification, the last six print statements are the ones originating from the random process:

from random import choices

rdm_vtx = choices(vertexList, weights=weightList, k=1)
print(rdm_vtx) 
$\endgroup$

Your Answer

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

Browse other questions tagged or ask your own question.