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 bpyimport csvimport os# Attributes to exportattr_names = ["position","G0",]# Path to the CSV filecsv_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)