Curves
Curves are connected points in a linear fashion.
Types
- Curve: Base Type.
- Curve Operators: Various tools to edit curves.
- Spline (curve.splines): Element of a curve, either NURBS, Bezier or Polyline
- SplinePoint (spline.points): Point on a spline without handles, Poly and NURBS only.
- BezierSplinePoint (spline.bezier_points): Element of a Bezier curve
Details
Curves consist of multiple splines. Each spline can have its own type: Poly, Bezier or NURBS. All of those types can exist in one curve object!
Poly and NURBS curves use points, Bezier curves only use Bezier points.
obj=bpy.context.active_object
if obj.type=="CURVE":
for spline in obj.data.splines:
if spline.type=="POLY" or spline.type=="NURBS":
print(f"Points: {len(spline.points)}")
elif spline.type=="BEZIER":
print(f"Bezier Points: {len(spline.bezier_points)}")
It's possible to convert a spline type:
bpy.ops.curve.spline_type_set(type='BEZIER')
Warning: If you convert from bezier to NURBS/Poly, you will lose size and twist data!
Each point has a coordinate as a Vector array, a radius and a tilt.
# Position:
print(obj.data.splines[0].points[0].co)
# Radius, Tilt:
print(obj.data.splines[0].points[0].radius)
print(obj.data.splines[0].points[0].tilt)
Spline points and bezier spline points are not the same! Converting SPs into BSPs isn't possible directly.
data = bpy.context.active_object.data
# get active spline
spl = data.splines.active
# iterate over splines
for spline in data.splines:
if spline.type in ["NURBS", "POLY"]:
# selected points
sel = [p for p in spline.points if p.select]
for p in sel:
print(p.co)
else:
# selected bezier points
sel = [p for p in spline.bezier_points if p.select]
for p in sel:
print(p.co)
Creating Curves from scratch
This is the very basic way of creating curves:
import bpy
from bpy_extras import object_utils
# curve data
coords = [(1,0,1), (2,0,0), (3,0,1)]
# create the Curve Datablock
curveData = bpy.data.curves.new('myCurve', type='CURVE')
curveData.dimensions = '3D'
curveData.resolution_u = 2
# map coords to spline
polyline = curveData.splines.new('POLY')
polyline.points.add(len(coords)-1)
for i, coord in enumerate(coords):
x,y,z = coord
polyline.points[i].co = (x, y, z, 1)
# create Object
curveOB = bpy.data.objects.new('myCurve', curveData)
# add the curve as an object into the scene with this utility module
object_utils.object_data_add(bpy.context, curveData)