I need to count in my scene for each possible vertex group (in each mesh) the number of faces of that vertex group and its total area (the sum of the areas of its faces). I made this first solution even with your help but it is still not enough, in my addon 5-6 seconds are wasted to perform this function and I would like something faster, consider that the test was done on a mesh with 230,000 vertices and 500,000 faces, how can I make it more efficient?
def count_face_area(obj, num_groups):
group_faces = [0] * num_groups #list with the total number of faces for each vertex group
area = [0] * num_groups #list with the total area for each vertex group
vertices = obj.data.vertices
polygons = obj.data.polygons
for face in polygons:
group_counts = [0] * num_groups
for vertex_idx in face.vertices:
vertex = vertices[vertex_idx]
for vgroup in vertex.groups:
group_counts[vgroup.group] += 1
for group_idx, count in enumerate(group_counts):
if count == len(face.vertices):
group_faces[group_idx] += 1
area[group_idx] += face.area
break
return group_faces, area
Imagine that this function is inside a for
loop that iterates through all the meshes of my scene so the obj parameter will be a mesh at each loop (obj = bpy.data.objects["mymesh"]), num_groups
instead is the number of total vertex group
for
loop needs to be inside the mainfor
loop. but how deep inside should it be. should it be a fourth level loop? $\endgroup$