3
$\begingroup$

I need to create a set of smooth curves (several thousand curves, in fact) from raw data that is in plain text files (i.e. not 3d objects). Each curve is made up of about 100-200 data points in the format (x, y, z).

I am aware, of course, that blender objects and curves can be generated via scripting in python, so this should be doable.

I was wondering if somebody could give me a pointer regarding a good starting point to get this done: any help (snippet, example, etc) would be super appreciated! Thank you!

$\endgroup$
1
  • $\begingroup$ As a starting point, use the Python csv module. Read your points in from csv and use them to create spline curves. There are several questions about creating curves in Python with examples, like this old one that you can use. If you get stuck on a specific, ask a question here. $\endgroup$ Commented Jun 17, 2022 at 17:49

1 Answer 1

4
$\begingroup$

Just for completeness, I actually did some digging and found a solution that accepts a text file containing multiple lists of coordinates in the 'x y z' format, with different curves separated by a blank line. I have copy-pasted the example below in case it helps others!

import bpy  
import csv
from mathutils import Vector  

# we don't have to use the Vector() notation.  
listOfVectors = [(0,0,0),(1,0,0),(2,0,0),(2,3,0),(0,2,1)]  
  
def MakePolyLine(objname, curvename, cList): 
    #let's first start by opening the filename with data points.
    trackpoints = "/Users/daviddc/Dropbox/archive/listofpoints.txt"
    with open('/Users/daviddc/Dropbox/archive/listofpoints.txt') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=' ')
        line_count = 0
        singleTrack = []
        for row in csv_reader:
            width = len(row)
            if width > 0: 
                thisVec = (float(row[0]), float(row[1]), float(row[2]))
                singleTrack.append(thisVec)
            if width == 0:
                #ah but wait, this means the track is now finished! create curve with it
                print("Now drawing curve "+str(line_count))
                curvedata = bpy.data.curves.new(name=curvename+str(line_count), type='CURVE')  
                curvedata.dimensions = '3D'  
              
                objectdata = bpy.data.objects.new(objname+str(line_count), curvedata)  
                objectdata.location = (0,0,0) #object origin  
                bpy.context.collection.objects.link(objectdata)
              
                polyline = curvedata.splines.new('NURBS')  
                polyline.points.add(len(singleTrack)-1)  
                for num in range(len(singleTrack)):  
                    polyline.points[num].co = (singleTrack[num])+(1,)  
              
                polyline.order_u = len(polyline.points)-1
                polyline.use_endpoint_u = True
                line_count = line_count + 1
                singleTrack = []
    
  
MakePolyLine("NameOfMyCurveObject", "NameOfMyCurve", listOfVectors)
```
$\endgroup$

You must log in to answer this question.

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