how to obtain the distance between two bones ? (even if I move the parents ?)

Hi all,

I’d like to retrieve the distance between 2 bones, to (py) drive a third one.
the scripts I found to grab some code (still learning python… I’m not really at ease yet… I mean… I’m too bad in fact with blender python objects :-/), as far as I try, won’t work for my purpose : they don’t take into account the parent bones position.

``````
A       B
|       |
a.......b
c

``````

A&B are some parent bones,
a&b are the childs
… is the distance I’d like to retrieve.
c is the pydrived bone, the a-b distance must be evaluated for its transformation.

the script I’ve found work if I move a or b,
but they won’t if I move A or B : the local pos. of a&b won’t change and the returned value is incorrect.

here are the scripts I tried :
JMS mesure.py
http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_3dcursorposition_en.htm#distancedunpointaucurs%20eur3d

Blender Caliper 1.4

http://www.alienhelpdesk.com/python_scripts/caliper

Blender Measurement Tool v 08 03 27
http://www.sammler-mediengestaltung.com/

in fact not matter if it’s not bone, it could be two empties parented to two bones, that I never move directly.

btw I don’t need to interact with the script, I just need to give it the name of the two objects as args in the pydriver of the c bone…

thanks for your help and directions !

littleneo

my goal is that thing :

works better.

Well, I’ve been measuring the location of empties parented to bones, and this is pretty much what Ive used adjusted to find the distance:

``````
import Blender
from Blender.Mathutils import Vector

#empty1 is the first empty object
#empty2 is the second empty object
pos1= Vector(empty1.getLocation("worldspace"))
pos2= Vector(empty2.getLocation("worldspace"))
dist=(pos1-pos2).length
print dist

``````

thanks a lot !
I’ve tested it and it works but I still have a problem…

my c bone is :
Z-rescaled depending on the distance between a & b, in the ‘a bone space’
X-rescaled depending on the distance between a & b, in the ‘a bone space’
the a bone is in fact the origin, that also moves in the worldspace.

if I apply a rotation on the whole armature or A, or B during the animation, as I don’t retrieve the rotation infos from the matrix, the vectors change, although the two empties still have the same vectors in the ‘a’ bone space

I guess I need to take into account the rotation ‘a’ as a constantly moving origin, and mix it into the b space…

how can I add the rotation/location of ‘a’ to compensate the b vector ?

Sorry, I’m a bit confused as to what you’re actually trying to achieve here.

You are trying to get something other than the distance between the two points?

Yes and No…
I need the x, y, and z translations of ‘b’ in the ‘a’ worldspace.
‘a’ and ‘b’ can’t be parented, ‘b’ is not animated directly (locaspace coords wont’ change, 0,0,0.)

when my armature is in rest state, the x distance between a & b is 1, y 0.5 and z something else.
during an animation, I need to retrieve the new x, y z distances and compare them to the rest state distances to get some 3 new variables from x, y, and z, separately.

if I rotate some part of the armature and that a & b are translated due to that rotation, the x distance changes, as I work in the worldspace, whereas the x dist between a & b is still 1 in the a localspace.

I want to get rid of any parent rotation consequence on the x, y and z distance because I need to measure separately the x, y and z variation to drive separately different pose channels of another bone… if I rotateY 90°, x dist becomes y, and the pose of my driven bone reacts doesn’t work as I want.

(sorry if it’s not clear, I’ve also some issues to explain it to me)

you can look the video on the second post :
my goal is to have the breast being Xreduced & Yexplanded when the shoulder rise, Xexpanded & Zreduced when the shoulder goes back and so on…
I’ve something working like that, as you will see, but it doesn’t work anymore if I move the parents bone, as it works in a localspace.

when my armature is in rest state, the x distance between a & b is 1, y 0.5 and z something else.
during an animation, I need to retrieve the new x, y z distances and compare them to the rest state distances to get some 3 new variables from x, y, and z, separately.

I think I see. You want these three values in the bonespace of A.

Well, you would be able to combine the rotation values (get a rotation matrix for A and the armature, multiply together) then use that to multiply the inverse with any position to take it back to a rest state.

Or, stick in some more empties, and use those to calculate the position in a new space.

To do that, put one empty at the new 0,0,0 position, one at 1,0,0 ; 0,1,0 ; 0,0,1
Then find the distance from the plane from each pair of these (dist from X-Y plane = new Z value).

Both should give the same answer. Edit - will, not should, they both transform the position in world-space to a local-space (though only the second can automatically take care of scale, but may be screwed by skew, neither should be an issue)

yes it should be the way, but python is not my friend yet. I understand nothing with the blender object for the moment. I think I need more time, more patience and discipline…

BUT… since this morning I did some tests with the new transformation constraint, and I think I’m about to reach my goal through it…
I’m too weak with python I think, + big lack of theory about geometry…

by adjusting relative influences of several transformation constraints on the same bone, I obtain the same results than with my first python script, and it’s refreshed directly, and it resolves my worldspace-moving parents problems :).

I used python and pydrivers before because a bone channel (scaley for example)
need to be influenced by a locx of another bone, scalex by locx and
locz for example, etc…
transformation constraints now do the job ! even if my constaint stack is big…

thank you for your help anyway !