# How to make a curve path from scratch given a list of (x, y, z) points?

I want to move a soft body object in my scene on a path (curve path). For the path, I want to make it myself from scratch. I basically have a Python List containing (x, y, z) points and want to create a curve path from scratch given these points using a Python script. However, it looks like the way the vertices are stored for paths is different than meshes as I cannot find something like nurbsPathObj.data.vertices. So I wonder, how can I manually add new vertices to a path or remove some of them via Blender's Python API?

• What kind of path? Do you mean a curve? Commented Oct 8, 2018 at 18:16
• @brockmann Edited the question. I think it should be more clear now
– Amir
Commented Oct 8, 2018 at 18:19
• Take a look on this: blender.stackexchange.com/questions/6750/… Commented Oct 8, 2018 at 18:43

import bpy
from mathutils import Vector

coords_list = [[0,1,2], [1,2,3], [-3,2,1], [0,0,-4]]

# make a new curve
crv = bpy.data.curves.new('crv', 'CURVE')
crv.dimensions = '3D'

# make a new spline in that curve
spline = crv.splines.new(type='NURBS')

# a spline point for each point

# assign the point coordinates to the spline points
for p, new_co in zip(spline.points, coords_list):
p.co = (new_co + [1.0]) # (add nurbs weight)

# make a new object with the curve
obj = bpy.data.objects.new('object_name', crv)


To add a point at the end it's simply:

spline = bpy.data.objects["object_name"].data.splines[0]
spline.points[-1].co = (1,1,5,1)


or to add more points at the end:

more_coords = [...list of more coords...]
for p, new_co in zip(spline.points[-len(more_coords):]:
p.co = new_co + [1] # if new_co is a list [x,y,z]


You can always get the coords of the points of the spline with

points_coords = [p.co for p in spline.points]


edit this list, i.g.

points_coords = points_coords[3:5] + [1.5,2,2.5,1] + points_coords[5:]


and then remove the spline or object and make a new one with the updated coordinates.

• how to make this working for version 2.8? Commented Jul 26, 2019 at 17:55
• Matthew posted an update below. Commented Mar 9, 2021 at 15:46

An update for Blender 2.8

bpy.context.scene.objects.link(obj)


is now

bpy.context.scene.collection.objects.link(obj)


and

spline.points.add()


needs to pass a number

spline.points.add(1)