I am trying to import Bezier curves from CSV into Blender using a script. In this question I could find the solution, but without importing the coordinates of the control points but only the coordinates of the actual points.
So I'm trying to customise this script so that it also imports the control points from the CSV, but I'm doing something wrong so that it applies the coordinates of the last control points from the CSV to all points.
What am I doing wrong? Many thanks in advance!
import bpy, csv
csv_filepath= "C:\PathTests\PathPoints.csv" # replace with a real path
# read all control points from csv file
with open(csv_filepath) as csv_file: # work with the opened file
# first read the complete file (we need to know the amount of points)
csv_reader = csv.reader(csv_file, delimiter=',')
control_points = []
for idx, row in enumerate(csv_reader):
if idx != 0: # ignore csv header
PointX = float(row[0])
PointY = float(row[1])
PointZ = float(row[2])
PointC1_X = float(row[3]) #Handle Left
PointC1_Y = float(row[4])
PointC1_Z = float(row[5])
PointC2_X = float(row[6]) #Handle Right
PointC2_Y = float(row[7])
PointC2_Z = float(row[8])
control_points.append((PointX, PointY, PointZ))
# create bezier curve and add enough control points to it
bpy.ops.curve.primitive_bezier_curve_add()
curve = bpy.context.active_object
bez_points = curve.data.splines[0].bezier_points
# note: a created bezier curve has already 2 control points
bez_points.add(len(control_points) - 2)
# now copy the csv data
for i in range(len(control_points)):
bez_points[i].co = control_points[i]
bez_points[i].handle_left_type = 'FREE'
bez_points[i].handle_right_type = 'FREE'
# just for illustration (screenshot),
# add your correct handle locations here
bez_points[i].handle_left = ((PointC1_X, PointC1_Y, PointC1_Z))
bez_points[i].handle_right = ((PointC2_X, PointC2_Y, PointC2_Z))