0
$\begingroup$

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.

enter image description here

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.

$\endgroup$
6
  • $\begingroup$ Pretty sure you need to add context.collection.objects.link(new_object) to the end of your createPilesAxis function. 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$ Commented Aug 22, 2022 at 15:01
  • $\begingroup$ Thank you very much for your reply. However, I don't think that is the problem. The code I have shown is clipped. Just below the line 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$ Commented Aug 22, 2022 at 15:22
  • $\begingroup$ Then can you possibly make like a zip folder with the blend file with your script and a sample CSV so we can debug it on our own computers? $\endgroup$ Commented Aug 22, 2022 at 16:47
  • $\begingroup$ Yes, sure. How can I share the folder with you? I'm new here and I don't know how. $\endgroup$ Commented Aug 23, 2022 at 10:08
  • $\begingroup$ I would just use google drive or dropbox or something, since you have to include more that just the blend file. $\endgroup$ Commented Aug 23, 2022 at 19:49

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.