Using csv files

Hi Everyone,

I hope this is the right place to be posting.
Since discovering what python can do in Blender helping me with my work, I’ve been hooked.

I have a .csv file (see attached). What I want to achieve is to add a cube at each of those locations. So the 1st column would be the x co-ordinate. The top row would be the y co-ordinate and then the corresponding value in the table would be the z co-ordinate.

So as per the .csv the 1st cube would be added at (1, 0, 19.445) and then the 2nd cube at (1, 1, 19.485) and so on and then move on to the next row where the x value would be 2.

I honestly couldn’t figure it out because I had to skip the 1st row where the ‘y’ values are located. For all the .csv files I’ll be using the y value will increase by 1 every time in any case so I tried to make that happen with a while loop.

I went scouring the internet and stealing other peoples code but I couldn’t piece together something that could work. I got it to a point where the 1st column could get done but I couldn’t get it to move on to the next column.

So whether it builds by going column to column or row to row doesn’t matter.

Here is my last laughable attempt that has the 1st column but added 29 times over itself, but now in a triangle!


import bpy, csv

path = "C:\CSVImport\C-Scan_CAP_Useme.txt" # this is where I stored the .csv on my PC

with open(path) as csvfile:
    content = csv.reader(csvfile, delimiter='\t', dialect='excel') # the .csv is seperated by tabs
    for i,row in enumerate(content):
        if i == 0: continue            # As far as I understand this skips the 1st line
        a = 1
        x = row [0]
        y = x
        z = row [a]
        while i > 0:            #I used a while loop. So while there are still rows the script should keep adding cubes?
                                #'float' makes the text an integer I think...
            bpy.ops.mesh.primitive_cube_add(location = (float(x), float(y) ,float(z)))
            a = a + 1
            y = float(y) + 1
            i -= 1

        


If you could take the time to review my problem I’d be forever grateful.

C-Scan_CAP_Useme.txt (37.0 KB)

Hi Kyle,

Have you seen Chris P’s vid on importing csv files? This might give you the info you needed.

Cheers,
Paul

The data formatting in the sample file is awkward and as such the only real info you get is the z-position. The x and y positions are calculated along the way getting the z position data.

In the future you may want to start with a smaller data set. Also since you are creating exact copies of an object just in different locations you should use the copy object method which will save a lot of time generating the cubes.

import bpy
import csv


# this is where I stored the .csv on my PC
path = "C:\CSVImport\C-Scan_CAP_Useme.txt"
locations = []
with open(path) as csvfile:
    # the .csv is seperated by tabs
    content = csv.reader(csvfile, delimiter='\t', dialect='excel')
    for y,row in enumerate(content):
        if y == 0:
            # As far as I understand this skips the 1st line
            continue
        for x, z in enumerate(row):
            if x == 0:
                continue
            try:
                locations.append((int(x), int(y), float(z)))
            except ValueError:
                pass


bpy.ops.mesh.primitive_cube_add(location = locations[0])
cube = bpy.context.scene.objects['Cube']

# Add all other cubes
for loc in locations[1:]:
    dupliCube = cube.copy()
    dupliCube.location = loc
    bpy.context.collection.objects.link(dupliCube)

I thank you both for your inputs! I will wrestle with this some more and provide feedback.

Thanks again.

Thanks again for your inputs. I was able to get the result I was aiming for.
Also found this gem to skin a point cloud:

Keep well!