0
$\begingroup$

I would like to create a script to import coordinates from a CSV and create a bezier curve along these coordinates. I have developed a script that imports the coordinates, but it creates a curve along only one coordinate point. All remaining points are ignored and the curve always starts at the coordinate origin (0,0,0). Unfortunately I don't have a good knowledge of Phython. Therefore it would be great if someone could help me. Thanks a lot!

from mathutils import Vector 

import bpy
import csv, os, bmesh, math

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

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

csvpoints= "NameofFilepath"
pointsReader = csv.reader(open(csvpoints, newline=''), delimiter=';')   
bpy.ops.curve.primitive_bezier_curve_add(enter_editmode=True)
with open(csvpoints, 'rt', encoding="utf8") as csvfile:
    pointsReader = csv.reader(csvfile, delimiter=';')
    for idx, row in enumerate(pointsReader):
        if (idx > 0):
            vert = (float(row[0]), float(row[1]), float(row[2])) 
            verts.append(vert)
            # Create curve and cache reference.
            curve = bpy.context.active_object
            bez_points = curve.data.splines[0].bezier_points
            bez_points = curve.data.splines[0].bezier_points

            # Set handles to desired handle type.
            for bez_point in bez_points:
                bez_point.handle_left_type = 'FREE'
                bez_point.handle_right_type = 'FREE'

                # Left point.
                bez_points[0].co = vert
[![Coordinates in CSV][1]][1]
$\endgroup$
1
  • 1
    $\begingroup$ "I have developed a script[...]I don't have a good knowledge of Phython" - therefore you didn't develop this script and misattributed the true author of this code. $\endgroup$ Jul 9 at 20:44

1 Answer 1

1
$\begingroup$

Well, as you say your script just suffers from some little mistakes. Try this version:

import bpy, csv

csv_filepath= "<NameofFilepath>" # 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
            control_points.append((float(row[0]), float(row[1]), float(row[2])))
        
    # 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  = control_points[i]
        bez_points[i].handle_right = control_points[i]

With this test csv file:

x;y;z;
1;0;0;
2;0;0;
2;1;0;
3;1;0;
3;1;1;
3;0;1;

i get this result:

enter image description here

It shows that the points from the file are set. Now you can go on and adjust handles, handle types ...

$\endgroup$
3
  • $\begingroup$ The coordinates from the CSV file are set as local coordinates of the bezier Points. Is there any possibility to set the coordinates from the CSV file as global coordinates of the bezier points? $\endgroup$
    – blenre231
    Jul 20 at 14:09
  • $\begingroup$ Not sure if i understand correctly. If the Bezier curve is at (0,0,0) there is no difference. If the Bezier is not at (0,0,0), you need to add its location to the csv points. You can get the location of the Bezier for example with bpy.data.objects["BezierCurve"].location, when your curve is named "BezierCurve". $\endgroup$
    – taiyo
    Jul 20 at 14:24
  • $\begingroup$ Exactly, my issue was that my curve was not at (0, 0, 0). Thank you very much for your help! $\endgroup$
    – blenre231
    Jul 22 at 13:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .