4
$\begingroup$

I have created at model that I want to export into CSV file and a txt file (for a quick review).

Using this post (below) I am able to do export the vertex information to and CSV and txt file (by just naming it .csv or .txt). This will output just to location of each point in the format x y z.

How to export Blender spreadsheet data into .csv file without applying GN?

How could this code be modified to include other column found in the table. I have a custom column called G0 and want to export this (and potentially more in the future).

Table below:

enter image description here

Code from link for quick reference:

import bpy
import csv
import os

m = bpy.context.object.evaluated_get(bpy.context.evaluated_depsgraph_get()).to_mesh()

# Get the path to the current Blender file
blend_file_path = bpy.data.filepath

# Get the directory of the Blender file
blend_dir = os.path.dirname(blend_file_path)

# Construct the absolute path to the "data.csv" file
data_file_path = os.path.join(blend_dir, "data.csv")


def append_to_csv(filepath, x, y, z):
    # Check if the file exists
    file_exists = os.path.isfile(filepath)

    # Open the file in append mode
    with open(filepath, 'a', newline='') as file:
        writer = csv.writer(file)

        # If the file doesn't exist, write the header
        if not file_exists:
            writer.writerow(['x', 'y', 'z'])

        # Write the values to the file
        writer.writerow([x, y, z])
        

for i in range(0, len(m.vertices)):
    v = m.vertices[i].co
    append_to_csv(data_file_path,v[0],v[1],v[2])

I am not very familiar with coding so any help would be greatly appreciated.

$\endgroup$
2
  • 1
    $\begingroup$ "...other column found in the take." - What does that mean? What is it? What's "the take"? $\endgroup$ Commented May 18 at 13:45
  • $\begingroup$ Sorry it is meant to say table not take. $\endgroup$
    – ninjafish3
    Commented May 18 at 16:04

1 Answer 1

2
$\begingroup$

This will export any set of chosen attributes on the active mesh to a CSV file.

The attributes for export are listed at the top in the attr_names variable. I've set "position" and "G0" for you. Change these or add as many as you want. They must, however, all have the same domain (eg. you can't mix Vertex and Face attributes).

If your .blend file is foo.blend, the CSV file is written to foo.blend.export.csv. Change the csv_path variable if you want it written somewhere else.

import bpy
import csv
import os

# Attributes to export
attr_names = [
    "position",
    "G0",
]

# Path to the CSV file
csv_path = bpy.data.filepath + ".export.csv"

mesh = bpy.context.object.evaluated_get(bpy.context.evaluated_depsgraph_get()).to_mesh()
attrs = [mesh.attributes[attr_name] for attr_name in attr_names if attr_name in mesh.attributes]

assert attrs, "No attributes found"
assert all(attr.domain == attrs[0].domain for attr in attrs), "Atrributes must all have same domain"

with open(csv_path, 'w', newline='') as file:
    writer = csv.writer(file)

    header = []
    for attr in attrs:
        name = attr.name
        if attr.data_type in ('FLOAT', 'INT', 'INT_8', 'BOOLEAN', 'STRING'):
            header.append(name)
        elif attr.data_type in ('FLOAT_VECTOR',):
            header += [name + ".x", name + ".y", name + ".z"]
        elif attr.data_type in ('FLOAT_COLOR', 'BYTE_COLOR'):
            header += [name + ".r", name + ".g", name + ".b", name + ".a"]
        elif attr.data_type in ('FLOAT2', 'INT32_2D'):
            header += [name + ".x", name + ".y"]
        elif attr.data_type in ('QUATERNION',):
            header += [name + ".w", name + ".x", name + ".y", name + ".z"]
    writer.writerow(header)

    if attrs[0].domain == 'POINT':
        dom = mesh.vertices
    elif attrs[0].domain == 'CORNER':
        dom = mesh.loops
    elif attrs[0].domain == 'FACE':
        dom = mesh.polygons
    elif attrs[0].domain == 'EDGE':
        dom = mesh.edges
    else:
        assert False, "unsupported domain"

    for i in range(len(dom)):
        row = []
        for attr in attrs:
            if attr.data_type in ('FLOAT', 'INT', 'INT_8', 'BOOLEAN', 'STRING'):
                row.append(attr.data[i].value)
            elif attr.data_type in ('FLOAT_VECTOR', 'FLOAT2'):
                row += attr.data[i].vector[:]
            elif attr.data_type in ('FLOAT_COLOR', 'BYTE_COLOR'):
                row += attr.data[i].color[:]
            elif attr.data_type in ('INT32_2D', 'QUATERNION'):
                row += attr.data[i].value[:]
        writer.writerow(row)
$\endgroup$
0

You must log in to answer this question.

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