-1
$\begingroup$

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

csvfile = open(r'C:\Users\%username%\Desktop\Deep-lizard\x_y_z coor.csv')

inFile = csv.reader(csvfile)
# skip header
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:

enter image description here

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

$\endgroup$
  • 2
    $\begingroup$ 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] $\endgroup$ – brockmann Sep 17 '19 at 18:48
  • 4
    $\begingroup$ 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. $\endgroup$ – Leander Sep 17 '19 at 19:41
0
$\begingroup$

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]
| improve this answer | |
$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Hazzaldo Sep 17 '19 at 19:59
  • $\begingroup$ Does this happen with the first method (manually parsing the floats)? $\endgroup$ – Leander Sep 17 '19 at 20:04
  • $\begingroup$ In the error it says: line 17, in <module>, which is the line: inFile.__next__(). Sorry is this what you meant? $\endgroup$ – Hazzaldo Sep 17 '19 at 20:13
  • $\begingroup$ 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. $\endgroup$ – Hazzaldo Sep 17 '19 at 20:38

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