0
$\begingroup$

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 )
f.close()
$\endgroup$

1 Answer 1

2
$\begingroup$

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")

print(outputFile)

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):
        break
    index += 1

print(outputFile)

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.

$\endgroup$
8
  • $\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$ Commented Mar 12, 2020 at 10:02
  • $\begingroup$ What code did you run? Was is this? $\endgroup$
    – Leander
    Commented Mar 12, 2020 at 10:08
  • $\begingroup$ Using the name of the object as file name. $\endgroup$ Commented Mar 12, 2020 at 11:15
  • $\begingroup$ Do you comprehend the script you posted? $\endgroup$
    – Leander
    Commented 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$ Commented Mar 12, 2020 at 11:33

You must log in to answer this question.

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