How would I export the vertex groups data from the "spreadsheet" option in Blender as a .csv file?

I have several vertex groups separated on my 3D model and I would like to export the spreadsheet that reflects the x,y, and z data along with the vertex groups data. I have already been able to export only the x,y, and z data as a .csv file using a code I found on here. However, I haven’t been able to find any solutions to export the table shown below of the vertex groups. In this example listed below, I would want to export the area of the table with ‘Ears’, ‘Mouth’, ‘Eyes’, and ‘Face’ as a .csv file as well with the vertices.

The code that I have tried from Blender artists iceythe Kaoi can be found at: How do I Export an Object's Vertex Positions? - #16 by KickAir_8P

  • I was to successfully export the vertices themselves in csv form but not the corresponding vertex group information.

image

  • ^^ The table I would like to export
import bpy
import os


def write_verts():
    obj = bpy.context.object

    if obj is None or obj.type != "MESH":
        return

    # Output geometry
    obj_eval = obj.evaluated_get(bpy.context.view_layer.depsgraph)
    filepath = "vertices.csv"
    
    with open(filepath, "w") as file:
        # Write the header, pos x | pos y | pos z
        file.write("pos x,pos y,pos z\n")

        for v in obj_eval.data.vertices:
            # ":.3f" means to use 3 fixed digits after the decimal point.
            file.write(f",".join(f"{c:.3f}" for c in v.co) + "\n")

    print(f"File was written to {os.path.join(os.getcwd(), filepath)}")

if __name__ == "__main__":

    write_verts()
  • ^^The code from iceythe Kaoi that I have used.

Does this help? It gives you a way to check if a vertex is part of a group (weight is set) and then you can deal with the weight value and add it to your CSV.

import bpy

GROUP_NAME="Group"

obj=bpy.context.active_object
data=obj.data
verts=data.vertices

for vert in verts:
    i=vert.index
    try:
        weight = obj.vertex_groups[GROUP_NAME].weight(i)
        #if we get past this line, then the vertex is present in the group
        print(weight)
    except RuntimeError:
       # vertex is not in the group
       pass

Copied from here: https://blender.stackexchange.com/a/65052