Get Curve Length?

Hi I am working on a project similar to my Ant Rig!

I am in desperate need of some code. I need to know how to find the length of a bezier curve. The API has getPathLen() But this returns the length in time the curve is not its physical length! is there anyway to do this?

It is really annoying becuse in the Curve edit mode it tells you its length, but that cant be used with anything.

I doesnt need to be mega acurate. I was looking into Cubic Curve’s on wiki but it got OTT very quickly, is there no way to simple get the curve length?

What I’m making needs an object to scale relative to the curve length!

Thankyou!

The two ways I’v found:

A: Use some coplicated math to work out the curve length based on its 4 points per segment. IE the controle point centers and the handels.

B: Use the resoulution of the curve to aproxamate the curve length using all the little straight lines that make up the curve!

the lenght should be using only the middle point for the triple bezier

the other 2 are for the handle and don’t change the lenght!

have you done that in 2.5 and can you share what you found

and i assume here that your not talking about an IPO curve but a real curve in blender!

thanks

Yea this is for a real curve not an IPO Curve!

Hmm. from what I have been reading bezier curves in blender are Cubic and the handels do effect the length!

Picture:

See here the P0 is the handle center and P1 is its handle, they have a direct effect on its length!

I havent implemented either of the two aproches outlined above, I was just stating that was what I think will be required, Im just thinking there might be a better way?

Hi there!

Dont know what you need this curve length for… If you really need the precise length of the curve, you’d need to find its equation, then calculate the length mathematically. This will be a hard task though!!! BUT for practical reasons, if you need to calculate what path covers you object during an animation, you may wish to sum up the lengths between points where the object is at each frame. In general, this is NOT the length of your curve (path) but as soon as your object will NEVER be at a place between such adjacent 2 points (corresponding to frames (N-1) and N). It deasnt matter how curvy is you curve between 2 such points (positions) cause your object practically NEVER goes there! At least in your animation :slight_smile: Soooo… in that case you will need to sum up lengths of sections of a polyline which is much easier :wink:

Abidos: that approch would work for animation but not in this case :frowning:

is there no way to get the length of a curve? its written right there in the edit mode panal and the array modifier can access it to fit count to curve length?

What I am doing is arraying an object allong a curve, but I need it to get smaller as it goes down the curve from 100% to 25%, I have used the array objects settings to offset by object so it does scale as it goes down the length, the problem is that as the curve gets longer the object offset keeps getting greater!

So if at rest pose the scale factor was 100% to 25%, when I increase the length of the curve to twice its normal distance there are now twice as many objects being arrayed so this intern increses the scale factor. what I need to do is make it so as the curve gets longer the scale offset down the curve is reduced by a factor of the curves length!

See this .Blend LINK

move the arm IK goal to change legth of the curve or use the curve point controls, you will notice the arm starts to get too thick, you then have to use the Empty between his feet to reduce the offset!

I would do it like this:
Make a copy of your bezier!
Make a mesh with enough vertices of the copy, more give more precision!
Count the length of the edges
It is so to say mathematically approximation of the integral of the curve-length.
In the limit (infinitely many points with shrinking to zero distance between points) it
would be the mathematically exact length of the space curve!

Example:


import bpy
def findlengthofcurve(obj):
    mesh = obj.data
    edges = mesh.edges.values()
    vertices = mesh.vertices.values()
    #print(edges)
    nrofedges = len(edges)
    summer = 0
    for el in edges:
        v1i,v2i = el.vertices
    #    print(v1i,v2i)   
        tmp = (vertices[v1i].co-vertices[v2i].co).length
#        print(tmp)
        summer += tmp
    return summer     
    
    
print("length is",findlengthofcurve(bpy.context.active_object) ) #a bezier converted to mesh!  

Try, which subdivision gives enough accuracy!

EDIT,
just checked, you may remove .values() (both), superfluous

there would be an indirect way
convert your bezier curve to a poly line then easy to calculate the lenght i think

but to calculate the lenght of a cubic curve is not easy and faster to get the equivalent lenght from the poly line
then you can convert back to bezier curve if need be

bu not certain if this can be use while using modifier !

salutations

Thanks PKHG and Ricky

Hmm. Could I have my nice bezier curve then using the Hook system attach a vert from a polyline to each handle piont then use the subdivided mesh of the polyline to work out the length? that way I could animate the curve and the polyline would follow!

would the above script by PKHG work on a mesh with modifiers applied?

Just did a quick test, if I create a Pollyline and carefully place the verts, I can then use the Vert Hook modifier to parent a vert to an empty, if I then use that same empty as the controle for the bezier curve using a Hook modifier on that the two match as close as dam it :).

Link to .Blend!

So if the above aproche worked after modifers I could get a curve length, is there a way to use the above script after modifiers without having to apply them so it can work for animation?

Thankyou

I did not understand totally the way of working
If I convert 'things ’ to a mesh my script works …

well then you need a script to calculate the lenght of poly line use for the modifier

you cannot convert curve to a mesh cause you loose your curve for the modifier
unless you make a copy of it then convert to a mesh

so you need another script to calculalte from the poly curve

is that what you need ?

not certain i i have to look if i have a script for 2.5 curve lenght!

by the way how much precision do you need here for the lenght?

happy 2.5

Recently, I needed to scale a texture along a mesh that was being controlled by a bezier curve. I used the following code as a driver for the scale and offset, passing the name of the curve to the function:

import Blender 

def get_length(name):
	total=0
	mesh = Blender.Mesh.New()
	mesh.getFromObject(name)
	for i in mesh.edges:
		total+=i.length
	return total

Could this be a solution to your problem?

Hi Guys thanks for your help managed to solve it with some script links magic woot woot!
Thankyou DarkFoundation you are a legend! that script worked a treat ontop of the poly chain thing I made!

Here is a video of the script working its magic LINK

I’m using the length of the curve to keep the arms thickness correct!
you can download it here: .Blend 2.4X Only

Glad the code snippet was of some use! It’s funny that you’re using it on a robot arm, because that’s exactly what I developed it for (see this thread and this one as well).

And I love GIR! Hope to see some crazy action outta him soon!

DarkFoundation:
Awsome stuff man thanks for sharing, so does 2.53 solve the twisting problem in the arm? and how would you run the script in 2.53 if it did with no script links? just intrested?

yea its weird we both had the same problem, cool toster though!

again thanks for sharing!

MC: Actually, the 2.5 series has proved too unstable for me to use in my projects. When I found it didn’t solve the mesh-twisting problem, I hopped back to 2.49; I didn’t fully investigate the script’s functionality in the latest versions.

Hi DarkFoundation

I was thinking, you dont need to use a script on your setup, you can do it using the array modifier here is a .blend

if you get a jagged curve just up it resolution!

Hope that helps!

cannot make the first blend works

so can you explain how to first script blend file works for letters ?

Thanks

RickyBlender:
What blender are you using? its 2.4X Only

The script is scaling a bone relative to the length of the poly chane that is atached to the controle points of the curve, in such a way that the poly chane is the same length of the curve. I am then using a driver to scale the offset Empty for the arms thickness Array! relative to the scaled bone. all the script is doing is keeping the arms thickness correct!