# Error creating Vector object from csv file in Blender (with python script) [closed]

I'm trying to create a Vector object (from mathutils library), whereby I have a csv file with 3 columns: x coordinate, y coordinate and z coordinate. All I want to do is import this CSV file in Blender and pass the x,y,z values to the Vector object, using python script. This is my code so far:

import bpy
import bmesh
from mathutils import Vector, Matrix
from itertools import product
import random

x_size = 512
y_size = 512
z_max = 256

inFile.__next__()

vertices = [Vector( (float(r[0]), float(r[1]), float(r[2])) for r in inFile]


However, when I run this script I get the error:

  File "C:\Users\%username%\Documents\new script.blend\Text", line 19
vertices = [Vector( (float(r[0]), float(r[1]), float(r[2])) for r in inFile]
^
SyntaxError: invalid syntax

location: <unknown location>:-1

Python script failed, check the message in the system console


The csv file content is in this format:

Not sure where I'm going wrong. Any help is greatly appreciated.

• Just a syntax error (see the error message), remove one of the parenthesis at the beginning of the list comprehension to: [Vector( float(r[0]), float(r[1]), float(r[2]) ) for r in inFile] – brockmann Sep 17 '19 at 18:48
• I'm voting to close this question as off-topic because it is about a programming syntax error. It would be better suited for stackoverflow. – Leander Sep 17 '19 at 19:41

The code snippet is missing the csv module import.

import csv


The syntax error is simply a missing closing bracket of the tuple.

vertices = [Vector((float(r[0]), float(r[1]), float(r[2]))) for r in inFile]


Since you actually know, that you will get a list of three floats, you can import the csv with the QUOTE_NONNUMERIC argument.

inFile = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC)
inFile.__next__()

vertices = [Vector(r) for r in inFile]

• Awesome thank you. Some of these mistakes are so silly like forgetting import csv. I can't believe I didn't notice that :S I just got this other error now: ValueError: could not convert string to float: 'y_coord', even though I used inFile.__next__(), which should've skipped the header. Do you know why it didn't work? – Hazzaldo Sep 17 '19 at 19:59
• Does this happen with the first method (manually parsing the floats)? – Leander Sep 17 '19 at 20:04
• In the error it says: line 17, in <module>, which is the line: inFile.__next__(). Sorry is this what you meant? – Hazzaldo Sep 17 '19 at 20:13
• The only way I can resolve it is by changing the line: csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC) back to just csv.reader(csvfile) and the line: vertices = [Vector(r) for r in inFile] back to: vertices = [Vector( (float(r[0]), float(r[1]), float(r[2]))) for r in inFile]. Thank you for your help in any case. Much appreciated. – Hazzaldo Sep 17 '19 at 20:38