Length of bezier curve?

Hello everybody,

is it possible to get access to a bezier curve with python in blender to get the length of the curve?

I think for some modifiers Blender knows (or calculates) the lenght of the curve, so the length of the curve should be somewhere.
The question is now how to get this information?

Greetings
Dennis


import mathutils


>>> mathutils.geometry.interpolate_bezier(
interpolate_bezier(knot1, handle1, handle2, knot2, resolution)
.. function:: interpolate_bezier(knot1, handle1, handle2, knot2, resolution)
Interpolate a bezier spline segment.
:arg knot1: First bezier spline point.
:type knot1: :class:`mathutils.Vector`
:arg handle1: First bezier spline handle.
:type handle1: :class:`mathutils.Vector`
:arg handle2: Second bezier spline handle.
:type handle2: :class:`mathutils.Vector`
:arg knot2: Second bezier spline point.
:type knot2: :class:`mathutils.Vector`
:arg resolution: Number of points to return.
:type resolution: int
:return: The interpolated points
:rtype: list of :class:`mathutils.Vector`'s

depending on the resolution you decide, it will return that number of points on the curve. More points is higher accuracy. Then stick something like this in there.


# takes interpolated points as an argument
def get_combined_length(p_list):
    edge_length = 0
    for idx in range(len(p_list)-1):
        edge_length += (p_list[idx] - p_list[idx+1]).length  
           
    return edge_length

maybe you can figure the rest out :slight_smile:


>>> bpy.data.curves['BezierCurve'].splines[0].bezier_points[0].     (ctrl+space to autocomplete)

will give you enough information, but it might end up looking something like this


import bpy
import mathutils

def get_combined_length(p_list):
    edge_length = 0
    for idx in range(len(p_list)-1):
        edge_length += (p_list[idx] - p_list[idx+1]).length  
           
    return edge_length

cv = bpy.data.curves['BezierCurve'].splines[0]

handle1 = cv.bezier_points[0].handle_left
knot1 = cv.bezier_points[0].co
knot2 = cv.bezier_points[1].co
handle2 = cv.bezier_points[1].handle_left

p_list = mathutils.geometry.interpolate_bezier(knot1, handle1, handle2, knot2, 20)
print(get_combined_length(p_list))

read up on bezier curve math if you want to do it yourself : http://processingjs.nihongoresources.com/bezierinfo/

Hi zeffii,

Thanks for this Information, ist Looks a Little bit more complicated than i thought.
I was more hoping, that the length is already given some where… Something like:

Bpy.Data.curves.[‘bezierCurve’].splines(0).length

Well, i Gruesse this would be too Easy :slight_smile:

Greetings

This might be a little excessive, but here is a small script that you run from text editor, then in 3dview a button appears when you select a curve.

When pressed:

  • it duplicates the curve as a mesh,
  • applies all transforms to the duplicate (for global coordinate values)
  • sums the collected edges of this mesh duplicate
  • prints the length to the terminal + copies value to clipboard
  • then deletes duplicate mesh
  • selects original curve again

Hi thanks for your tips and codes
I will have to try them… Guess this will work…

Greetings