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?
Asked
Active
Viewed 6,602 times
7
Amir
- 3,074
- 2
- 29
- 55
-
What kind of path? Do you mean a curve? – brockmann Oct 08 '18 at 18:16
-
@brockmann Edited the question. I think it should be more clear now – Amir Oct 08 '18 at 18:19
-
2Take a look on this: https://blender.stackexchange.com/questions/6750/poly-bezier-curve-from-a-list-of-coordinates – Yann Masoch Oct 08 '18 at 18:43
2 Answers
14
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.
Benedikt
- 406
- 3
- 7
7
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)
Robert Gützkow
- 25,622
- 3
- 47
- 78
Matthew.MC
- 83
- 2
- 5