Function to return location of intersect with poly mesh and spherical object moving in the positive direction on the Y axis. Removing print()’s increases script speed enormously.
mel source
global proc int setSphereMeshIntersect(string $sphere, string $mesh, float $radius)
{
int $iter = 100;
float $limit[3] = {0.0, 2.5, 0.0};
float $startPos[3] = {0.0, 0.0, 0.0};
float $curPos[3] = {0.0, 0.0, 0.0};
$curPos[0] = getAttr ($sphere + ".translateX");
$curPos[2] = getAttr ($sphere + ".translateZ");
$cpomNode = "cpom";
for ($i = 0; $i <= $iter; $i++)
{
$shape = `listRelatives -shapes $mesh`;
createNode -n $cpomNode closestPointOnMesh;
connectAttr -f ($shape[0] + ".outMesh") ($cpomNode + ".inMesh");
setAttr ($cpomNode + ".inPosition") $curPos[0] $curPos[1] $curPos[2];
$cpom = `getAttr ($cpomNode + ".position")`;
delete $cpomNode;
if ( pointDist($curPos, $cpom) <= $radius) {
return 1;
}
$curPos[1] = $startPos[1] + ($limit[1]) / $iter * $i;
setAttr ($sphere + ".translateY") $curPos[1];
}
return 0;
}
global proc float pointDist(float $p1[], float $p2[])
{
return sqrt(
(($p1[0] - $p2[0]) * ($p1[0] - $p2[0])) +
(($p1[1] - $p2[1]) * ($p1[1] - $p2[1])) +
(($p1[2] - $p2[2]) * ($p1[2] - $p2[2])));
}
result



