0
$\begingroup$

Hello fellow Blender snakehandlers,

I have a list of coordinates in a csv file and I would like to create a mesh with only vertices from it.

When I run it, it throws the following error, which I can't decipher. Any ideas on how to make this work? Thank you!

line 54, in <module>
  File "C:\Program Files\Blender Foundation\Blender\2.79\scripts\modules\bpy_types.py", line 429, in from_pydata
    self.vertices.foreach_set("co", tuple(chain.from_iterable(vertices)))
TypeError: couldn't access the py sequence

CSV

id,x,y,z,r,g,b
0,0.30778491,0.12740749,0.04903458,174.0,84.0,118.0

PYTHON

import bpy
import csv, os, bmesh, math


#filepaths
filepath = bpy.data.filepath
directory = os.path.dirname(filepath)

blendname = bpy.path.basename(bpy.data.filepath)
blendname = os.path.splitext(blendname)[0]
if blendname:
    bpy.context.scene.render.filepath = blendname
print(blendname)


some_dict = {}

# DO POINTS

csvpoints= directory+"/csv/3d-points.csv"

pointsReader = csv.reader(open(csvpoints, newline=''), delimiter=',')


#bpy.ops.object.mode_set(mode='EDIT')

obj = bpy.context.object

verts = []
edges = []
faces = []

for idx, row in enumerate(pointsReader):
    #print(idx, row)
    id = row[0]
    print("id = "+ id)
    x = row[1]
    print("x = " + x)
    y = row[2]
    print("y = " + y)
    z = row[3]
    print("z = " + z)
    vert = (x,y,z) 
    verts.append(vert)


#create mesh and object
mesh = bpy.data.meshes.new("wave")
object = bpy.data.objects.new("wave",mesh)

#create mesh from python data
mesh.from_pydata(verts,[],[])
mesh.update(calc_edges=True)



#set mesh location
object.location = bpy.context.scene.cursor_location
bpy.context.scene.objects.link(object)
$\endgroup$
1
$\begingroup$

The manuals suggest using the csvreader in a with block.

You made two mistakes.

  1. You have to skip the first csv line, since it doesn't hold any values. (Or treat it differently.)
  2. You have to parse the numeric values. float(row[0])

Working example.

import bpy
import csv, os, bmesh, math

#filepaths
filepath = bpy.data.filepath
directory = os.path.dirname(filepath)

verts = []
edges = []
faces = []

csvpoints= directory+"/points.csv"
pointsReader = csv.reader(open(csvpoints, newline=''), delimiter=',')   

with open(csvpoints, 'rt', encoding="utf8") as csvfile:
    pointsReader = csv.reader(csvfile, delimiter=',', quotechar='|')
    for idx, row in enumerate(pointsReader):
        if (idx > 0):
            vert = (float(row[0]), float(row[1]), float(row[2])) 
            verts.append(vert)

obj = bpy.context.object

#create mesh and object
mesh = bpy.data.meshes.new("wave")
object = bpy.data.objects.new("wave",mesh)

#create mesh from python data
mesh.from_pydata(verts,[],[])
mesh.update(calc_edges=True)


#set mesh location
object.location = bpy.context.scene.cursor_location
bpy.context.scene.objects.link(object)
$\endgroup$
  • $\begingroup$ Ah that works! Which manual do you refer to? I googled and came up with nothing suitable. A lot of code examples parse csvs with a for loop... $\endgroup$ – anf Dec 7 '17 at 13:57
  • $\begingroup$ @anf Just various python docs. $\endgroup$ – Leander Dec 7 '17 at 13:58
  • $\begingroup$ I have another csv that lists edges like so: source,target 214,0 how do i create edges between the verts in the mesh? a nested for loop? with itertools? with open(csvedges, 'rt', encoding="utf8") as csvfile2: edgesReader = csv.reader(csvfile2, delimiter=',', quotechar='|') for idy, row in enumerate(pointsReader): if (idy > 0): edge = mesh.verts.[edgereader.row[0]], mesh.verts[edgereader.row[1]]) edges.append(edge) #print(idy) mesh.edges.new((mesh.verts[0], bm.verts[1])) $\endgroup$ – anf Dec 7 '17 at 14:35
  • $\begingroup$ @anf Go ahead and ask a new question. =) $\endgroup$ – Leander Dec 7 '17 at 14:39
  • $\begingroup$ blender.stackexchange.com/questions/96070/… Vielen Dank / thank you for your help! :) $\endgroup$ – anf Dec 7 '17 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.