3

I have a mesh and I make automatic weighting. But the problem is that some vertices are not assigned to have any weight, and I can't locate these vertices due to the complexity of my mesh. I'm asking if there is a way where I can locate and assign auto-weights to these vertices? If anyone could please advise.

Tak
  • 6,293
  • 7
  • 43
  • 85

2 Answers2

4

I had this problem and came here so hopefully my script will save someone some time and headache looking through the blender API

# Usage: with a mesh selected, run the script. It will add a new 
# vertex group to the mesh called 'ProblemVertices'
# The script will add any vertices with a total weight != 1.0 
# to the 'ProblemVertices' group.

import bpy

print("Start script")

me = bpy.context.object.data                # the selected mesh
grps = bpy.context.object.vertex_groups     # all vertex groups in the mesh
grps.new(name="ProblemVertices")            # add a vertex group to the mesh
myvertexgroup = grps[-1]                    # the last vertex group in the mesh - our new group

# optional part which when uncommented deletes vertices 
# from groups where the vertex has weight 0 for that group.
#for vertex in me.vertices:
#    for grpelement in vertex.groups:                        # for each vertex group element in the mesh
#       if grpelement.weight == 0.0:                        # if the vertex is unweighted
#           grps[grpelement.group].remove([vertex.index])   #remove it from that group
#           print("Removed Vertex from group")
#       if len(vertex.groups) == 0:                            # if the vertex is not a member of any groups
#           myvertexgroup.add([vertex.index], 1.0, 'ADD')          # add it to the last group with weight 1
#           print("Unweighted vertex added to group")

for vertex in me.vertices:                              # for each vertex
    thisweight = 0.0                                    #keep track of total weight
    for grpelement in vertex.groups:                    # for each vertex group element in the mesh
        thisweight += grpelement.weight
    if thisweight < 0.9999 or thisweight > 1.0001:          # if the vertex weights don't add up to roughly 1.0
        myvertexgroup.add([vertex.index], 1.0, 'ADD')           # add the vertex to the last group with weight 1.0
        print("Vertex weight not 1.0 found")

print("Finished script")
3

This answer has some relevant code for finding vertices without weights: Checking if a vertex belongs to a vertex group in python

Based on that you might try something along these lines:

import bpy

auto_weights = 0.1                             
ob = bpy.data.objects['NameOfYourMeshObject']  

for v in ob.data.vertices:                     # for each vertex
    for g in v.groups:                         # for each vertex group in the mesh
        if g.weight == 0.0:                    # if the vertex is unweighted
            g.weight = auto_weight             # then set the weight

This probably isn't quite right, you might need another test to check if a vertex is in a vertex group or not, depending on the details of your problem.

Cody Reisdorf
  • 504
  • 3
  • 13