Bone Length

Blender 2.65a. How do I access the length of a given bone?

I need to use the bone length in a python expression.

I am don’t use python much, but bone distance is fixed by definition. The head and tail location of each bone is set when rigged. And, with parent child relationship, moving parent bone well moves all its children. Right?

I don’t know much about the details of rigs. Just learning. I can scale a bone in the local Y axis during posing. Shouldn’t this change the length of the bone?

Run this snippet in the console or save it as a script, it should give you your bone details. I think this is what you want.


import bpy

bones = bpy.context.object.data.bones
pose_bones = bpy.context.object.pose.bones
for bone in pose_bones:
    print(bone.name)
    print(bone.length)
    print("-----")

Save it as a script and indent the 3 print statements. Not sure how to post code on this forum.
Edit: figured out the code tags, it can be pasted into the console now. Just hit enter, twice.

Okay that snippet gives me the apparent length of each bone in the currently selected armature. That is cool. I will keep that for future use. But I need to figure out how to grab the length of a given bone. Here is my current example. I have no .blend at the moment because it is just three bones I am learning with.

I have an armature named “Armature.001” which has only one bone called “Master”. I have this bone set up to scale along the local Y axis. I have a driver keeping an eye on this scale value that is driving the rotation of another bone in another armature. I got the driver working great for my test setup. I used this python expression for the driver: -(90 * (pi/180) * Master_scale) + (90 * (pi/180)) where “Master_scale” is the name of the variable in the driver which is set to “Transform Channel” and Z Rotation. This works flawlessly, but I need to reposition the “Master” bone in a different location that requires some simple trigonometry to calculate the resulting Z rotation angle. I can do the math easy, but I need the value of the length of “Master” inside my python expression in order for the driver to correctly rotate my driven bone.

That is the best description without giving too much specificity I can give. So what I need to know is how to express the length of any bone as a valid python expression like Master.length or len(Master). I do not know a lot of Blender Python yet, but I am trying to learn.

Well I come from a C background, and there is probably like one line of code that will work, but this little function does the trick without too much hassle. Is this more or less what you’re looking for?


import bpy

def getBoneLength(armname,bonename):
    bonelength = 9.99
    for bone in bpy.data.objects[armname].pose.bones:
        if (bone.name == bonename):
            bonelength = bone.length
    return bonelength


masterlength = getBoneLength('Armature.001','Master')
print(masterlength) 

blenderallday, I appreciate your generous help, my friend. You have helped me get a start on what I need. I like your scripts, but I needed something I can plug into a driver’s “Python Expression” box.

I did, however and finally, find an answer. It took me two days to find this information, even after looking through the API. I think someone needs to write a more concise and detailed, maybe tutorial based, API document.

What I found was this:

-(90 * (pi/180) * bpy.data.objects["Armature.001"].pose.bones["Master"].length) + (90 * (pi/180))

The above can be plugged into any Blender numeric field and evaluate to an actual number. I need to tweak this function to get the proper rotation though, which shouldn’t be too hard. I don’t know why it took me so long to find the answer.

Now all I need to do is make sure the driver can update this function for each frame of an animation.

Learning is fun!