Bone Lenght scripted Expression

Hello.
Recently I’ve come across this video which shows a cool trick to perform a foot roll using a single bone:

The problem is that the scripted expression used in the video apparently doesnt work on Blender 2.7x, and since I’m not a python literate, I have no idea why.
It seems as if the “len” property has changed, but I can’t find what has replaced it and how can I make it work again.
The original expressions were:

len*(1-cos(max(RotX,0)))
-len*sin(max(RotX,0))

Any help will be appreciated.

OK - you have been hood-winked a little here sir!

The variable “len” in that scripted expression is the length of the bone, so far so good. The question is how did he get it and here is the bit he left out totally from the Video - shame on him. So how do you get the length of a bone - well he did it by measuring or calculating the length of the bone (cheeky) and adding it as a Custom Property for the bone. Then you add a variable to the scripted expression of the Single Property type - then RMB click on the Custom Property and select “Copy Data Path” - then LMB click in the single property “Path” box and key CTRL+V - that will paste in the data path for the variable. Then name this variable “len” and it will work.

An example of a data path for bone would be: pose.bones[“Bone.001”][“len”] with the Armature Object as the “Prop” Are you still with me?

So how do you calculate the length of a bone might I ask?

Well the simplest way I know is to place an Empty at each end of the bone then set up a scripted expression on the first empty - it matters not on which value, but I use X Location, then set a Distance Variable for the driver and use the location of the two empties as the parts of the variable type - The value you see will be the distance they are apart - hence the length of the bone. Simples!

Here’s a picture to explain things:


Just type this value into your Custom Property and Voila!

Cheers Clock.

Hi Clockmender!

Thanks for the response… I’ve understood your explanation and will work on it as soon as I have time. It made perfect sense.

I’ve actually managed to make it work right after I posted my question, by just ignoring the “len” variable. So for example, my Y expression was just “1-cos(max(RotX,0))” But it only worked because I used it on a newly created bone, which had a lenght of 1… scaling it messed everything up.

What I find intriguing is that on the video he shows the entire process, and he simply doesn’t create a custom property or the “len” variable itself. Could he perhaps have purposefuly cut out this part of the video/process? Maybe, but checking a little further, I’ve come across the Driver manual, which shows something called Driver_namespace, which is a list of builtin functions that you can use on the driver’s scripted expressions. You can check the list on the python console:
https://www.blender.org/manual/animation/basics/drivers.html

Among the functions listed are well known ones like sin, cos, tan… as well as the others shown on the video expression: mat and guess what… len! So according to the blender manual and the list inside blender, “len” is some property or function that you can use to work with drivers.
So this makes me wonder if somehow on blender 2.5 (the video’s version), a “len” attribute was used, and worked on that version… while on recent versions it doesn’t and we must resort to creating a variable ourselves.

Edit: check it out:


Anyway, thanks!
Regards

Yes - I think he left it out of the video also. I have not played with the “built-ins functions” yet, I did make my own python script to calculate a complex maths function once and then added that to the by.app - but I think you would need a script to work out the length based upon calculations using the end point of the bone. I think this is far too much work for me when I can simply use the method I described above and get the answer quickly. I regularly use the two empties approach to measure things in Blender, even keeping these as a group in a separate blend file, along with other such goodies, I then simply append this group to my project file and move the two empties to various locations and simply read off the value as a quick easy measuring tool.

I have tried to find out how to access “len”, but without writing a scrip I think it is not possible. I even wrote a script to return the value of “pi”, having found the “math.pi” function in Python - then found out I could simply type “pi” in the driver scripted expression - D’Oh. I guess that as I use K.I.S.S as a maxim, I tend to look for the easiest functional way out of a problem.

Cheers, Clock

PS. just a quick thought - have you tried something like len[‘pose.bones[“Bone.001”]’] as a way of passing the bone your are interested in to the “len” function? I cannot see how “len” knows what you are interested in unless you tell it - perhaps some kind Python Expert might comment on this.

len[‘pose.bones[“Bone.001”]’]

No - this doesn’t work! Also you cannot copy the data path for the head and tail positions of the bone, although you can see them in Edit Mode - this means that writing a script to calculate the values from this information would likely not be possible - bad news.

Cheers, Clock.

EDIT:

I think the “len” function return the length of a string or number in characters, not the length of a bone! So len(25.65) would return 5 - not what we want…

I checked his video again… apparently I made a mistake and because of that I also led you to do the same. For that I’m sorry.

That’s because even though in the cheat sheet image he uses “len” on the expression, when he actually writes the expression on the driver’s fields, he uses the int value 1, because he knows that his bone has 1 BU of lenght. That’s why his expression doesn’t return as invalid… because he didn’t write the literal “len” on the expression, but the numerical value.
So I’m pretty sure he assumed that the viewers would understand that they had to manually find the bones lenght before writing the expression, but he didn’t say that out loud. I also failed to notice him writing “1” instead of “len”, and on top of that there’s also the unfortunate coincidence that “len” turns out to be a builtin function that ‘could’ be used in a scripted expression.

So yeah… I can’t blame the guy, now I honestly don’t think he tried to deceive us… :slight_smile:

PS: I used your technique of measuring using empties and it worked like a charm! It even works dynamically by using copy location constraints on the empties (one for the head and other for the tail).
Cheers

Pleased to help - I have started a thread to find a way to get the bone’s data-path length - I will report any submissions to you.

Cheers for now, Clock.

So I had a response to my post on bone length:

If you type bpy.data.armatures[‘Armature’].bones[‘Bone’].length into the Python Console - you will get the bone’s length, just change the names in the [] brackets to reflect the name of your armature and specific bone.

Then add the given value as a Custom Property and then set the variable to read this property - you can then use the bone’s length in your scripted expression - a picture to show how it’s done:


To get the path, RMB click the value in the Custom property and select “Copy Data Path” then paste this into the path field. I have not yet found a way to get Blender to read this length value as a path yet…

Cheers, Clock.

In that tutorial, the ‘len’ is just hard coded in as a number value. Find the value, if you don’t know it, via clockmender’s method and replace the word ‘len’ with that number. So if the length of the bone is 2.5, your expression would look something like this:


2.5*(1-cos(max(RotX,0)))
-2.5*sin(max(RotX,0))

This is commonly used for heel/foot roll, and moves the pivot point of a bone from the root to the tip. Very useful in this area, but I’ve never needed it anywhere else.

Hope this helps,
Randy

Yeah Randy, after some time we finally figured that out… but thanks!!

And Thanks Clock for the script to retrieve the bone lenght dynamically!

I’m not sure if I will use it somewhere else besides rigging the foot as well, and I don’t think the foot bone would commonly change it’s lenght (maybe on a metamorphosed creature), but it is nice to know this, and to have this info here in case anyone else wants to do that.

:slight_smile:
Regards