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

**URL:** <https://blenderartists.org/t/how-would-i-export-the-vertex-groups-data-from-the-spreadsheet-option-in-blender-as-a-csv-file/1529049>\
**Category:** Python Support\
**Tags:** blender-io, spreadsheet\
**Created:** [May 8, 2024, 5:30pm UTC](https://blenderartists.org/t/how-would-i-export-the-vertex-groups-data-from-the-spreadsheet-option-in-blender-as-a-csv-file/1529049 "2024-05-08T17:30:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![morgansmith2738](https://blenderartists.org/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@morgansmith2738](https://blenderartists.org/u/morgansmith2738)\
**Post date:** [May 8, 2024, 5:30pm UTC](https://blenderartists.org/t/how-would-i-export-the-vertex-groups-data-from-the-spreadsheet-option-in-blender-as-a-csv-file/1529049/1 "2024-05-08T17:30:36Z")

</div>

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](https://blenderartists.org/t/how-do-i-export-an-objects-vertex-positions/1397962/16)

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

![image](https://blenderartists.org/uploads/default/original/4X/5/7/7/577124bc83061ebaa2c7df0b284b950337875d7b.png)

- ^^ The table I would like to export

```auto
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.

---

<div class="post-metadata">

**Author:** ![slartibart](https://blenderartists.org/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@slartibart](https://blenderartists.org/u/slartibart)\
**Post date:** [May 15, 2024, 6:18am UTC](https://blenderartists.org/t/how-would-i-export-the-vertex-groups-data-from-the-spreadsheet-option-in-blender-as-a-csv-file/1529049/2 "2024-05-15T06:18:27Z")

</div>

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.

```auto
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](https://blender.stackexchange.com/a/65052)

---

<div class="post-metadata">

**Author:** ![morgansmith2738](https://blenderartists.org/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@morgansmith2738](https://blenderartists.org/u/morgansmith2738)\
**Post date:** [May 23, 2024, 3:28pm UTC](https://blenderartists.org/t/how-would-i-export-the-vertex-groups-data-from-the-spreadsheet-option-in-blender-as-a-csv-file/1529049/3 "2024-05-23T15:28:12Z")

</div>

I am able to get pull up the weights for each group using the code you suggested, thank you. However, I am still having a hard time incorporating your suggestion into the current code to receive the weight values for each vertex. I am able to export all of the vertices labeled as x,y, and z but not the corresponding weight values for the vertex groups.

I have attached screenshots below. Do you have any suggestions on what I am doing wrong in my pipeline?

**Code: Your suggestion + Code for vertices and exporting to .csv**

> import os  
> import bpy
> 
> def write\_verts():
> 
> ```
> GROUP_NAME="Ears"
> 
> obj=bpy.context.active_object
> data=obj.data
> verts=data.vertices
> if obj is None or obj.type != "MESH":
> return
>   
> obj_eval = obj.evaluated_get(bpy.context.view_layer.depsgraph)
> filepath = ("monkeygroups.csv")
>   
> with open(filepath, "w") as file:
> file.write("pos x, pos y, pos z, Ears, Mouth, Eyes, Face\n")
>       
> for v in verts:
> i=v.index
> try:
> weight = obj.vertex_groups[GROUP_NAME].weight(i)
> file.write(f",".join(f"{c:3f}" for c in v.co) + "\n")
> print(weight)
> print(f"File was written to {os.path.join(os.getcwd(), filepath)}")
> except RuntimeError:
> pass                
> 
> ```
> 
> if **name** == “ **main** ”:
> 
> ```
> write_verts()
> 
> ```

**Resulting .csv from running script**

 ![image](https://blenderartists.org/uploads/default/original/4X/0/c/9/0c948132428a0f6cf0a66fb045f628e6d7815225.png)

---

<div class="post-metadata">

**Author:** ![slartibart](https://blenderartists.org/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@slartibart](https://blenderartists.org/u/slartibart)\
**Post date:** [May 23, 2024, 4:29pm UTC](https://blenderartists.org/t/how-would-i-export-the-vertex-groups-data-from-the-spreadsheet-option-in-blender-as-a-csv-file/1529049/4 "2024-05-23T16:29:44Z")

</div>

I think you are missing the line where the vertex weights are supposed to be written to the file?

The line

```auto
file.write(f",".join(f"{c:3f}" for c in v.co) + "\n")

```

only writes the vertex coordinates. If you want to have more you would need to add the value of the vertex weight as well, e.g. something like:

```auto
file.write(f",".join(f"{c:3f}" for c in v.co) + f" {weight}"\n")

```

One more comment:  
This `try/except` approach was a demo only. for multiple vertex groups I would do something like this which also solves the problem, that vertices that are not part of the vertex group get no weight value. In the snippet below the value zero is assigned to them. Storing the data + writing the CSV is outsourced to Numpy which is great for such things.

```auto
import os
import bpy
import numpy as np

GROUP_NAMES=["Ears", "Eyes", "Mouth"]
filepath = (r"d:\monkeygroups.csv")

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

# if you want to use all vertex groups assigned to the object
all_group_names = [group.name for group in obj.vertex_groups]

nb_verts = len(verts)
coord_weight_array = np.zeros((nb_verts, 3+len(GROUP_NAMES)))
xyz_coord = np.empty(nb_verts * 3, dtype=np.float64)

verts.foreach_get("co", xyz_coord)
coord_weight_array[:,:3] = np.reshape(xyz_coord, (nb_verts, 3))

for group_idx, this_group in enumerate(GROUP_NAMES):
    array_col = group_idx + 3
    for vert_idx, v in enumerate(verts):
        try:
            weight = obj.vertex_groups[this_group].weight(v.index)
            coord_weight_array[vert_idx, array_col] = weight
        except Exception:
            pass
        
header_str = ";".join(["pos x", "pos y", "pos z"] + GROUP_NAMES)
np.savetxt(filepath, coord_weight_array, delimiter = ';', fmt='%.8e', newline='\n', header=header_str)

```

---

<div class="post-metadata">

**Author:** ![morgansmith2738](https://blenderartists.org/letter_avatar_proxy/v4/letter/m/e9a140/32.png) [@morgansmith2738](https://blenderartists.org/u/morgansmith2738)\
**Post date:** [May 23, 2024, 6:18pm UTC](https://blenderartists.org/t/how-would-i-export-the-vertex-groups-data-from-the-spreadsheet-option-in-blender-as-a-csv-file/1529049/5 "2024-05-23T18:18:53Z")

</div>

This worked perfectly! Thank you for your suggestions
