0
$\begingroup$

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!

$\endgroup$

1 Answer 1

1
$\begingroup$

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)))
$\endgroup$
4
  • $\begingroup$ Does the command out of the function needs intendation? $\endgroup$
    – Yash
    Commented Feb 7, 2019 at 8:40
  • 1
    $\begingroup$ @Yash I just updated my answer to clarify the fix. $\endgroup$ Commented Feb 7, 2019 at 8:45
  • $\begingroup$ Oh, so now python will take it inside the for loop and repeat as expected! $\endgroup$
    – Yash
    Commented Feb 7, 2019 at 8:47
  • $\begingroup$ Hi @TobiasEinarsson, Thanks! This worked when I tried it this time. It's what I had tried before and it didn't work?! Weird. I'm just glad to know I'm not losing my mind :) Thanks for your help! $\endgroup$
    – S.Now
    Commented Feb 7, 2019 at 20:29

You must log in to answer this question.

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