0

I know this was already addressed in this post: How to read a csv file and use the values as x and y points in blender? but I don't have enough reputation yet to comment on it to keep the thread together. Sorry for the noise.

I'm running:`

# Populate a volume in Blender with spheres positioned from a CSV

import bpy, csv

path = "insertpathhere" 

with open(path) as csvfile:
    content = csv.reader(csvfile, delimiter=',', dialect='excel')
    for i,row in enumerate(content):
        if i == 0: continue            # skip header
        x,y,z = row [0], row[1], row[2]

bpy.ops.mesh.primitive_uv_sphere_add(location = (float(x), float(y) ,float(z)))

` I'm using a csv with a x,y,z column set up with header.

The problem is that upon execution I'm only getting the last row of data showing up as a single object (in the right spot at least) rather than all rows populating as a new object. The failure to make an object per each iteration suggested changing indentation of bpy.ops.mesh.primitive_uv_sphere_add(location = (float(x), float(y) ,float(z))) but that didn't work. So here I am. Let me know where the heck this is failing at.

Thanks in advance!

S.Now
  • 5
  • 1
  • 3

1 Answers1

1

The last line in your code will only execute once, unless it’s level of indentation is increased enough to be part of the for-loop.

The level of indentation is very important in Python.

Edit: Didn’t see the part about indentation in your question until now, but still; the line will only execute one time unless the indentation is corrected.

Edit 2: Added a possible solution (not tested):

# Populate a volume in Blender with spheres positioned from a CSV

import bpy, csv

path = "insertpathhere" 

with open(path) as csvfile:
    content = csv.reader(csvfile, delimiter=',', dialect='excel')
    for i,row in enumerate(content):
        if i == 0: continue            # skip header
        x,y,z = row [0], row[1], row[2]
        bpy.ops.mesh.primitive_uv_sphere_add(location = (float(x), float(y) ,float(z)))
Tobias Einarsson
  • 2,202
  • 14
  • 18