The only tricky part here is that in a DAG hierarchy you can't really use the translate attributes as the measure inputs because those don't reflect world space distances. So you'll need to hook up one of the pivots for each object and then use the "world space" attributes for each object into the respective input matrices of the distance node. Huh? Just watch the video and I'll walk through it.
The basic code for the node itself is:
shadingNode -asUtility distanceBetween -n "name;
BTW, here's the code I used in the video for creating this distance node . . . You can just copy it into your script editor and make a quick mel button for it. I've added just a quick dummy check to make sure there are 2 tranforms selected.
{
string $sel[] = `ls -sl -tr`;
if (`size($sel)` != 2){
error "please select 2 transformable objects to measure";
}
string $name = "distance";
string $dis = `shadingNode -asUtility distanceBetween -n $name`;
connectAttr ($sel[0] + ".worldMatrix") ($dis + ".inMatrix1");
connectAttr ($sel[1] + ".worldMatrix") ($dis + ".inMatrix2");
connectAttr ($sel[0] + ".rotatePivotTranslate") ($dis + ".point1");
connectAttr ($sel[1] + ".rotatePivotTranslate") ($dis + ".point2");
}
Thanks, this really useful for measuring the distance between joints and controls to create elbow and knee locks. It saved me a ton of additional locators, constraints and headaches.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteJust an fyi, you can just connect the world matrices of the object you want to measure the distance between without having to also connect the pivots. I believe it's an either or situation.
ReplyDeleteThanks for sharing all the info!
Hello,
ReplyDeleteI personnally use the 'parentMatrix' and the 'translate' of the objects to measure distance between them.
A python version of what I do :
import maya.cmds as mc
sel= mc.ls( sl= True )
if not len(sel) == 2:
print "please select 2 transformable objects to measure"
else:
disNode= mc.createNode( 'distanceBetween' )
mc.connectAttr( sel[0] + '.parentMatrix', disNode + ".inMatrix1")
mc.connectAttr( sel[1] + '.parentMatrix', disNode + ".inMatrix2")
for axis in ['X', 'Y', 'Z']:
mc.connectAttr( sel[0] + '.translate'+ axis, disNode + '.point1'+ axis )
mc.connectAttr( sel[1] + '.translate'+ axis, disNode + '.point2'+ axis )
Thanks for sharing,
felixlechA