2
$\begingroup$

How can I access the weight values of a polygon resp. it's vertices by the vertex group name?

ob = bpy.context.active_object
vertexGrp = ob.vertex_groups["Group1"]
for poly in ob.data.polygons:
  weight = 0                        
  for vert in poly.vertices:
      weight += ob.data.vertices[vert].groups[vertexGrp].weight

      # The commented lines didn't work either
      #weight += ob.data.vertices[vert].groups[1].weight
      #weight += ob.data.vertices[vert].groups["Group1"].weight

  #do something with the weight

It's about the last line, including the commented ones, that give me an error, it seems I don't use it right. Googled for hours, but didn't find an example I could understand. Please help.

$\endgroup$
1

1 Answer 1

5
$\begingroup$

OP (me) didn't realise that a weight value of 0 auto-removes a vertex from the group. Also you need to use the index for accessing the weight data. So this works:

import bpy

ob = bpy.context.active_object
vertexGrp = ob.vertex_groups["Group"].index
for poly in ob.data.polygons:
    weight = 0    
    try:                    
        for vert in poly.vertices:           
            weight += ob.data.vertices[vert].groups[vertexGrp].weight
        weight /= len(poly.vertices)
        print(weight)
    except:
        print("no vgroup")
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .