6
$\begingroup$

I've used mathutils.geometry.interpolate_bezier() in the past to create points for a curve using the bezier handle and knot coordinates.

from the docs:

interpolate_bezier(knot1, handle1, handle2, knot2, resolution)

My question is; given an existing curve in the scene, is there a neat way to get at the points that describe the curve without converting it to a mesh, or figuring out which handle (left or right) to use for each spline point.

curve = bpy.data.curves[0].splines[0]
curve.bezier_points[0].co
curve.bezier_points[0].handle_left
curve.bezier_points[0].handle_right
$\endgroup$
1
  • $\begingroup$ Note that there is/was a bug in the interpolation utility function that made it return 2D coordinates, even if the curve is 3D: developer.blender.org/T43473 $\endgroup$
    – CodeManX
    Commented Feb 4, 2015 at 17:08

1 Answer 1

8
$\begingroup$

The parameters can be found by taking the coordinate and right handle of a point, and the coordinate and left handle of the next point in the bezier points array.

For cyclic curves the last point and the first point in the array form an extra segment.

spline = bpy.data.curves[0].splines[0]

if len(spline.bezier_points) >= 2:
    r = spline.resolution_u + 1
    segments = len(spline.bezier_points)
    if not spline.use_cyclic_u:
        segments -= 1

    points = []
    for i in range(segments):
        inext = (i + 1) % len(spline.bezier_points)

        knot1 = spline.bezier_points[i].co
        handle1 = spline.bezier_points[i].handle_right
        handle2 = spline.bezier_points[inext].handle_left
        knot2 = spline.bezier_points[inext].co

        _points = mathutils.geometry.interpolate_bezier(knot1, handle1, handle2, knot2, r)
        points.extend(_points)
$\endgroup$
2
  • $\begingroup$ That's exactly what I'm looking for, thank you brecht! $\endgroup$
    – zeffii
    Commented Jun 6, 2013 at 19:54
  • $\begingroup$ I rolled this snippet into a function that takes a spline and a clean boolean (to remove consecutive doubles) : gist.github.com/zeffii/5724956 $\endgroup$
    – zeffii
    Commented Jun 6, 2013 at 22:31

You must log in to answer this question.

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