@Nowayfra
Hi, I've set this up, per your instructions. Obviously I did something wrong. I'm still looking into it, but if it is clearly obvious to you what I have missed, would you mind pointing it out to me?
Thanks!!!
//create nearestPointOnMesh nodes
createNode nearestPointOnMesh -name npom1;
createNode nearestPointOnMesh -name npom2;
string $theCubes[] = `ls -type transform "cube\_*"`;
for($obj in $theCubes)
{
//make connections as described in the instructions by nowayfra: http://tinyurl.com/2av2323
//part 1, First
connectAttr -f sourceGeometryShape.outMesh npom1.inMesh;
connectAttr -f ($obj + ".translate") npom1.inPosition;
//store the results of part 1, First
float $vector1f[] = getAttr("npom1.position");
vector $vector1 = getAttr("npom1.position");
//part 2, Second
connectAttr -f ($obj + ".outMesh") npom2.inMesh;
setAttr ($obj + ".resultPoint1X") $vector1f[0];
setAttr ($obj + ".resultPoint1Y") $vector1f[1];
setAttr ($obj + ".resultPoint1Z") $vector1f[2];
connectAttr ($obj + ".resultPoint1X") npom2.inPositionX;
connectAttr ($obj + ".resultPoint1Y") npom2.inPositionY;
connectAttr ($obj + ".resultPoint1Z") npom2.inPositionZ;
//store the results of part 2, Second
vector $vector2 = getAttr("npom2.normal");
float $angle = `angle $vector1 $vector2`;
//disconnect everything
disconnectAttr sourceGeometryShape.outMesh npom1.inMesh;
disconnectAttr ($obj + ".translate") npom1.inPosition;
disconnectAttr ($obj + ".outMesh") npom2.inMesh;
disconnectAttr ($obj + ".resultPoint1X") npom2.inPositionX;
disconnectAttr ($obj + ".resultPoint1Y") npom2.inPositionY;
disconnectAttr ($obj + ".resultPoint1Z") npom2.inPositionZ;
if ( $angle > 0.5) {
delete $obj;
}
}
fyi, I am storing the value of resultPoint1 in a vector attribute that I added to each cube.
Before
After
The python to create the cube setup.
import maya.cmds as mc
\# create a block grid of cubes
mult = 3;
count = 0
cubes = []
y = mult - mult
while y <= mult *2:
x = mult * -1
while x <= mult:
z = mult * -1
while z <= mult:
cube = mc.polyCube( name = ('cube\_' + str(count) ) )
## add a vector attribute to each cube to store closestPointOnMesh result.
mc.addAttr( cube[0], ln = 'resultPoint1', at = 'double3' )
mc.addAttr( cube[0], ln = 'resultPoint1X', at = 'double', p = 'resultPoint1' )
mc.addAttr( cube[0], ln = 'resultPoint1Y', at = 'double', p = 'resultPoint1' )
mc.addAttr( cube[0], ln = 'resultPoint1Z', at = 'double', p = 'resultPoint1' )
mc.setAttr( cube[0] + '.resultPoint1', 0,0,0, type = 'double3')
mc.setAttr( cube[0] + '.resultPoint1', keyable = True)
mc.setAttr( cube[0] + '.resultPoint1X', keyable = True)
mc.setAttr( cube[0] + '.resultPoint1Y', keyable = True)
mc.setAttr( cube[0] + '.resultPoint1Z', keyable = True)
count +=1
cubeName=cube[0]
cubes.append(cubeName)
mc.move(x, y, z)
z += 1
x +=1
y +=1
\##create source geometry
mc.polySphere(name = 'sourceGeometry')
mc.move(0,3,0)
mc.setAttr('sourceGeometry.scaleX', 3)
mc.setAttr('sourceGeometry.scaleY', 3)
mc.setAttr('sourceGeometry.scaleZ', 3)