Take the 2-minute tour ×
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It's 100% free, no registration required.

I'm trying to create curves from a list of points, I have the xyz coordinates in text files. I could make it manually via the python console for a single curve, but I have to do it many times and I don't know how to correctly implement a loop.

How can I do that?

share|improve this question
add comment

1 Answer

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
share|improve this answer
2  
It creates a NURBS curve. The type for curveData.splines.new(type) can be chosen from ('POLY', 'BEZIER', 'BSPLINE', 'CARDINAL', 'NURBS'). –  pink vertex Feb 4 at 17:44
    
Thanks, but I already did something similar actually. Is there a way to make it a function that reads the coords from a text file passed as an argument? Something like CreatePolyLine('coords_file_name.txt')... –  Matteo Caffini Feb 4 at 20:15
    
If you install NumPy (which comes bundled with the Linux version), you could replace the fourth line with coords=np.loadtxt('filename.txt'), and add import numpy at the top... otherwise there's certainly other ways to read in a text file in Python. –  Garrett Feb 4 at 21:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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