Wednesday, October 05, 2011

creating a nice clean distance node using "distanceBetween"

Here's another neat little node, called the "distanceBetween". As you might guess, it measures the distance between of two selected objects.  What's nice about this is that you don't get the extra gunk that comes the measure tool, like locators and UI stuff, which one typically doesn't need in a rig.
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");
}

4 comments:

  1. 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.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Just 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.

    Thanks for sharing all the info!

    ReplyDelete
  4. Hello,
    I 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

    ReplyDelete