|
import csv |
|
|
|
node = hou.pwd() |
|
geo = node.geometry() |
|
|
|
# csv_node points to our table node, then we evaluate the file path in the table node |
|
# we then open the file and read it into memory so we can iterate through it and parse the data |
|
|
|
csv_node = hou.node("../tableimport2") # change path to your table import node |
|
file_path = csv_node.parm('file').eval() |
|
f = open(file_path, "r") |
|
csv_reader = csv.reader(f, delimiter=',') |
|
|
|
# We start by establishing some variables that calculate the row and column counts of our CSV |
|
|
|
row_count = sum(1 for row in csv_reader) |
|
f.seek(0) |
|
col_count = len(next(csv_reader)) |
|
|
|
# Initialize a list and fill it with 0s, totaling the number of columns in the CSV, |
|
# then iterate through each column and check each entry for letters. If column 0 has |
|
# any entry that contains letters, then the first entry in the list will be 'string', |
|
# and the for-loop will break to the next column. If column 0 has no entries with letters, |
|
# but has an entry with a ".", then it will store 'float' in our list and move to the next column. |
|
# If it has neither letters or ., then it is just whole numbers and 'int' will be stored in our list |
|
|
|
data_types = [] |
|
for column in range(col_count): |
|
data_types.append(0) |
|
|
|
for column in range(col_count): |
|
f.seek(0) |
|
next(csv_reader) |
|
for rows in csv_reader: |
|
current_col = rows[column] |
|
contains_letters = current_col.lower().islower() |
|
if contains_letters == True: |
|
data_types[column] = 'string' |
|
break |
|
elif "." in current_col: |
|
data_types[column] = 'float' |
|
break |
|
else: |
|
data_types[column] = 'float' |
|
#data_types[column] = 'int' |
|
|
|
# This looks at how many columns there are, creates that many attributes, |
|
# then loops through each attribute. It sets the column index, names the attribute (c0, c1, etc.), |
|
# sets the attribute type by referencing the data_types array created above, |
|
# and sets the attribute length to 1 (since we are not bothering with vectors) |
|
|
|
csv_node.parm('numattrib').set(col_count) |
|
|
|
for j in range(col_count): |
|
csv_node.parm('column%i'%(j+1)).set(j) |
|
csv_node.parm('attribname%i'%(j+1)).set('c%i'%j) |
|
csv_node.parm('attribtype%i'%(j+1)).set(data_types[j]) |
|
csv_node.parm('attriblen%i'%(j+1)).set(1) |
|
|
|
# This section adds a new attribute on this Python node called "column_header" |
|
# by looking at the first row in the CSV, then iterating through each column, |
|
# creating a point and setting the points attribute value to the value in the CSV |
|
|
|
geo.addAttrib(hou.attribType.Point, "column_header", 'test') |
|
f.seek(0) |
|
first_row = next(csv_reader) |
|
|
|
for column in range(col_count): |
|
point = geo.createPoint() |
|
point.setAttribValue("column_header", first_row[column]) |