7
$\begingroup$

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?

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

2 Answers 2

14
$\begingroup$

Maybe something like this? See also: Create NURBS surface with Python

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
spline.points.add(len(coords_list)-1) # theres already one point by default

# 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)
bpy.context.scene.objects.link(obj)

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

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

or to add more points at the end:

more_coords = [...list of more coords...]
spline.points.add(len(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.

$\endgroup$
2
  • 1
    $\begingroup$ how to make this working for version 2.8? $\endgroup$
    – Fabio
    Jul 26, 2019 at 17:55
  • $\begingroup$ Matthew posted an update below. $\endgroup$
    – Benedikt
    Mar 9, 2021 at 15:46
7
$\begingroup$

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)
$\endgroup$

You must log in to answer this question.

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