3

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!

quellenform
  • 35,177
  • 10
  • 50
  • 133

1 Answers1

4

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) ```