This should work:
import bpy
# sample data
coords = [(1,1,1), (2,2,2), (1,2,1)]
# create the Curve Datablock
curveData = bpy.data.curves.new('myCurve', type='CURVE')
curveData.dimensions = '3D'
curveData.resolution_u = 2
# map coords to spline
polyline = curveData.splines.new('NURBS')
polyline.points.add(len(coords))
for i, coord in enumerate(coords):
x,y,z = coord
polyline.points[i].co = (x, y, z, 1)
# create Object
curveOB = bpy.data.objects.new('myCurve', curveData)
# attach to scene and validate context
scn = bpy.context.scene
scn.objects.link(curveOB)
scn.objects.active = curveOB
curveOB.select = True
curveData.splines.new(type)can be chosen from('POLY', 'BEZIER', 'BSPLINE', 'CARDINAL', 'NURBS'). – pink vertex Feb 04 '14 at 17:44coords=np.loadtxt('filename.txt'), and addimport numpyat the top... otherwise there's certainly other ways to read in a text file in Python. – Garrett Feb 04 '14 at 21:05'BEZIER'for thecurveData.splinetype you'll need to use.bezier_points.add()and.bezier_points[i].coinstead of.points.add()andpoints[i].co. – Cody Reisdorf Dec 23 '14 at 00:02.bezier_points[i].handle_left = .bezier_points[i].handle_right = .bezier_points[i].co– rfabbri Jun 16 '16 at 22:52AttributeError: 'bpy_prop_collection' object has no attribute 'link'in Blender 2.8 or newer, then check this answer https://blender.stackexchange.com/a/162417/131771 – Silvan Mühlemann Apr 29 '22 at 19:57