I have been using this script to export my mesh to a CSV file. The problem is that every time I select a new object, the file is overwritten, so ideally when selecting a new object I need it to write a new CSV file, perhaps like this. "mesh_001.csv", "mesh_002.csv" etc. Please could someone assist.

import bpy

outputFile = 'C:/My_Objects/mesh.csv'

verts = [ bpy.context.object.matrix_world @ v.co for v in bpy.context.object.data.vertices ]

csvLines = [ ";".join([ str(v) for v in co ]) + "\n" for co in verts ]

f = open( outputFile, 'w' )
f.writelines( csvLines )

1 Answer 1


Use the name of the object as the file name

import bpy
import os

name = bpy.context.object.name
outputFile  = bpy.path.abspath("//csv_export\\" + name + ".csv")


If I save my file in D:\ and my object is named Cube, the outputFile will be D:\csv_export\Cube.csv.

Increase a suffix number for each export

import bpy
import os

index = 0
while True:
    path = "//csv_export\\mesh" + str(index).zfill(4) + ".csv"
    outputFile = bpy.path.abspath(path)
    if not os.path.exists(outputFile):
    index += 1


In this script, we initially construct a path to a file with the suffix 0 => mesh0000.csv. While a file already exists at that location, we increase the counter.

  • $\begingroup$ It doesn't seem to create the new directory D:/csv_export and if I create it myself nothing is written into it when I run the script. My .blend file is saved in the same D:/ directory. Maybe I'm doing something wrong? $\endgroup$ Mar 12, 2020 at 10:02
  • $\begingroup$ What code did you run? Was is this? $\endgroup$
    – Leander
    Mar 12, 2020 at 10:08
  • $\begingroup$ Using the name of the object as file name. $\endgroup$ Mar 12, 2020 at 11:15
  • $\begingroup$ Do you comprehend the script you posted? $\endgroup$
    – Leander
    Mar 12, 2020 at 11:21
  • $\begingroup$ I am not an expert at python, sorry. I sort of understand a few things. I am learning as I go. $\endgroup$ Mar 12, 2020 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.