I am trying to draw meshes in Blender by importing data from several CSV files at the same time. I could import the files one by one, but it would be quite a slow process, since there are so many files. So far, with the code I’ve written, I’ve managed to select all the CSV files I need to extract the information from at once. The problem is that Blender draws the first mesh correctly, but the remaining ones, although they appear in the program tree, are not displayed. That is, I understand that it is iterating all the files, but it is only extracting the information from the first one.
I show you here the code that I have written so far:
import bpy, os, csv
from bpy_extras.io_utils import ImportHelper
from bpy.types import Operator, OperatorFileListElement
from bpy.props import (BoolProperty,
FloatProperty,
StringProperty,
EnumProperty,
CollectionProperty
)
# Lists to store the reading file data
verts = []
edges = []
faces = []
def createPilesAxis(context, filepath):
"""Reads the file and extracts the vertices"""
with open(filepath, 'r', encoding='utf-8-sig') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
vert = (float(row[0]), float(row[2]), float(row[1]))
verts.append(vert)
# Joins the vertices
for i, vert in enumerate(verts):
startpoint = i
if i < len(verts)-1:
endpoint = i+1
edge = startpoint, endpoint
edges.append(edge)
"""Creates pile axis"""
global pile_curve
# Creates the arch axis mesh
new_mesh = bpy.data.meshes.new(name='axis_mesh')
new_mesh.from_pydata(verts, edges, faces)
## Creates the object mesh
new_object = bpy.data.objects.new('pile_axis_curve', new_mesh)
...
return {'FINISHED'}
class OT_ImportFilebrowser(Operator, ImportHelper):
bl_idname = "test.import_filebrowser"
bl_label = "Import file"
bl_description = "Allows importing an arch axis coordinate dataset"
filter_glob: StringProperty(
default='*.txt;*.csv',
options={'HIDDEN'}
)
files: CollectionProperty(type=bpy.types.PropertyGroup)
def execute(self, context):
folder = (os.path.dirname(self.filepath))
for j, i in enumerate(self.files):
path_to_file = (os.path.join(folder, i.name))
createPilesAxis(context, self.filepath)
return {'FINISHED'}
classes = [
OT_ImportFilebrowser,
]
...
The image shows what is described above. In the 3D view a single mesh is drawn, but in the collections tree of the scene there are seven meshes, corresponding to the seven CSV files that I “have imported” into the program.
I am using Blender 3.1 version.
I hope someone can help me, because I have given it a lot of thought and I can’t solve it.
Thanks in advance.

context.collection.objects.link(new_object)to the end of yourcreatePilesAxisfunction. Right now all you're doing is adding the objects to the database, but you still have to (with Python) essentially "drag and drop" it from the outliner into the active collection. $\endgroup$new_object = bpy.data.objects.new('pile_axis_curve', new_mesh)I have written what you comment. That is the reason why in the image all the axes appear inside the "axes_collection". $\endgroup$