// setDistance.mel v1.0 // (c) 07 June 2002 by Carsten Kolve // carsten@kolve.com // www.kolve.com // // Description: // sets the distance between 2 objects, by moving the second object along the // line between the two objects // // Usage: // * source script // * type: // setDistance object1 object1 distance; // // Examples: // setDistance pCube1 nurbsSphere1 8; // // setDistance pSphere1.vtx[6] pSphere1.vtx[7] 2.5; // // sets the distance between the vertices to 2.5 // // if the vertices share an edge, this will have the specified length // // Credits: // Christian B. on maya@highend3d for super-duper-great inspiration on this one global proc int setDistance(string $object1, string $object2, float $distance) { // initialize needed variables vector $pos1; // position of the first object, this one will be fixed vector $pos2; // position of the second object, this one will be moved vector $direction; // direction vector between object1 and object2 float $temp[]; // temp array to store position vector components // get world space position of objects $temp = `xform -ws -q -t $object1`; $pos1 = <<$temp[0],$temp[1],$temp[2]>>; $temp = `xform -ws -q -t $object2`; $pos2 = <<$temp[0],$temp[1],$temp[2]>>; // check whether objects have the same position if ($pos1 == $pos2) { error "positions are equal!"; return false; } // calculate new position for object2 $direction = $pos2 - $pos1; $direction = `unit $direction`; $direction *= $distance; $pos2 = $pos1 + $direction; // set new position for object2 eval("xform -ws -t "+$pos2.x+" "+$pos2.y+" "+$pos2.z+" "+$object2); return true; }