PDA

View Full Version : closestPointOnMesh node


bendingiscool
11-01-2008, 09:54 PM
Hi, I'm having problems with the closest/nearestPointOnMesh (depending on Maya version). I want to collect the nearest face on the mesh to the locator. This is because I want to eventually be able to draw a curve on the surface and select the faces the curve goes over. The node returns a seemingly random 'closestFaceIndex' number, that is nowhere near the locator's posiiton. Below is what I have at mo...


// create closestPointOnMesh node
string $closestPoint = eval("createNode closestPointOnMesh -n cpom1");

//connect it to the polymesh
connectAttr -f pPlane1.outMesh ($closestPoint + ".inMesh");

//connect locator1 pos' to closestPointOnMesh pos'
connectAttr -f locator1.translateX cpom1.inPositionX;
connectAttr -f locator1.translateY cpom1.inPositionY;
connectAttr -f locator1.translateZ cpom1.inPositionZ;

//store face index
int $selectedFaces = `getAttr cpom1.closestFaceIndex`;


Has anyone had anyluck with this node, am I going about this the wrong way?

Any help/advice/suggestions would be very welcome

Many thanks,

Chris

thematt
11-01-2008, 10:05 PM
yep I had problem with it too..try nearestPointOnMesh (have to load it from the pluggin list in 2008 at least).

cheers

MrWoo
11-02-2008, 09:24 AM
I think you need to use "worldMesh" attribute instead of "outMesh"

NaughtyNathan
11-02-2008, 11:30 AM
your problem could simply be because you've changed your default scene units from cm (you naughty boy!!). All of Maya works in cm internally, including all plug-ins, irrespective of what your units are set to.

If this is the case, you'll need to multiply your values by whatever the scale difference is before you feed them to the plug-in. Then you'll have to feed the answer back through the reverse multiplication. This is bad enough in a script, but if you're connecting the nodes inputs/outputs it's ridiculous.

no-one should ever change from cm for this one reason alone, let alone all the other bugs/problems it causes...

:nathaN

bendingiscool
11-02-2008, 12:12 PM
Thanks MrWoo, that was the problem, that has been bugging me for sooo long, I shall post up some progress in the near future.

Thanks,

Chris

bendingiscool
11-02-2008, 07:44 PM
OK I have this so far, it loops through creating a locator in a random pos (for test purposes), then it makes a new 'nearestPointOnMesh' node each time and an int to grab the value of the 'faceIndex'.

Unfortunatly it seems to be averaging out the position and selecting the face in the middle of the locators, rather than grabbing each face as it goes through. Does anyone know what I'm doing wroung here?


for ($i=1;$i<5;$i++){
spaceLocator -p (rand(-10,10)) 0 (rand(-10,10)) -n ("locator" + $i);

string $nearestPoint = `createNode nearestPointOnMesh -n ("npom" + $i)`;

//connect it to the polymesh
connectAttr -f pPlane1.worldMesh ($nearestPoint + ".inMesh");

//connect locator1 pos' to nearestPointOnMesh pos'
connectAttr -f (("locator" + $i) + ".translateX") (("npom" + $i) + ".inPositionX");
connectAttr -f (("locator" + $i) + ".translateY") (("npom" + $i) + ".inPositionY");
connectAttr -f (("locator" + $i) + ".translateZ") (("npom" + $i) + ".inPositionZ");

//store face index
int $selFaces = `getAttr (("npom" + $i) + ".nearestFaceIndex")`;

select -add pPlane1.f[$selFaces];
}


Any input would be greatly apreciated,

Chris

kojala
11-02-2008, 10:22 PM
try this:

string $loc[] = `spaceLocator`;
string $nearestPoint = `createNode nearestPointOnMesh`;
connectAttr -f "pPlane1.worldMesh" ($nearestPoint + ".inMesh");
connectAttr -f ($loc[0] + ".translateX") ($nearestPoint + ".inPositionX");
connectAttr -f ($loc[0] + ".translateY") ($nearestPoint + ".inPositionY");
connectAttr -f ($loc[0] + ".translateZ") ($nearestPoint + ".inPositionZ");

for ($i=1;$i<5;$i++)
{
float $pos[] = `rand << -10, 0, -10>> << 10, 0, 10>>`;
move -a -ws $pos[0] 0 $pos[2] $loc[0];

int $selFaces = `getAttr ($nearestPoint + ".nearestFaceIndex")`;
select -add pPlane1.f[$selFaces];
}

you dont have to create each time a new nearestPointOnMesh node or locator node.
if you do the selection is changed to them and you loose selected face from previous
loop cycle. sometimes Ive noticed that maya wont update nodes correctly in scripts
so you might also try command refresh -f; after moving the locator.

bendingiscool
11-02-2008, 11:56 PM
Hey, thanks guys, getting closer!

@kojala - I tried your code, but it didn't like it, it crashed Maya lol.

I have revised the latest piece of code on the locator's movement.

When the loop is done it now selects the end one so it's getting there, now
instead of just selecting the end one how would I go about keeping each faceIndex selected, or at least put in an array, so I can select the array once the loop has finished.

I promise to stop with the annoying questions, for a little while at least ; ) , if I can get this to work lol.

for ($i=1;$i<5;$i++){
spaceLocator -n ("locator" + $i);
move (rand(-10,10)) 0 (rand(-10,10));

// create nearestPointOnMesh node
//string $nearestPoint = eval("createNode nearestPointOnMesh -n npom1");

string $nearestPoint = `createNode nearestPointOnMesh -n ("npom" + $i)`;

//connect it to the polymesh
connectAttr -f pPlane1.worldMesh ($nearestPoint + ".inMesh");

//connect locator1 pos' to nearestPointOnMesh pos'
connectAttr -f (("locator" + $i) + ".translateX") (("npom" + $i) + ".inPositionX");
connectAttr -f (("locator" + $i) + ".translateY") (("npom" + $i) + ".inPositionY");
connectAttr -f (("locator" + $i) + ".translateZ") (("npom" + $i) + ".inPositionZ");

//store face index
int $selFaces = `getAttr (("npom" + $i) + ".nearestFaceIndex")`;

select -add pPlane1.f[$selFaces];
}

Many thanks,

Chris

kojala
11-03-2008, 12:17 AM
well this one I tested. did not crash my maya2008.
and now it selects in the end all found faces.

{
string $loc[] = `spaceLocator`;
string $nearestPoint = `createNode nearestPointOnMesh`;
connectAttr -f "pPlane1.worldMesh" ($nearestPoint + ".inMesh");
connectAttr -f ($loc[0] + ".translateX") ($nearestPoint + ".inPositionX");
connectAttr -f ($loc[0] + ".translateY") ($nearestPoint + ".inPositionY");
connectAttr -f ($loc[0] + ".translateZ") ($nearestPoint + ".inPositionZ");
string $found_faces[];

int $i = 0;
while($i < 5)
{
vector $pos = `rand << -10, 0, -10>> << 10, 0, 10>>`;
move -a -ws ($pos.x) 0 ($pos.z) $loc[0];

int $selFaces = `getAttr ($nearestPoint + ".nearestFaceIndex")`;
$found_faces[$i] = "pPlane1.f[" + $selFaces + "]";
$i++;
}
select -r $found_faces;
}

bendingiscool
11-03-2008, 10:53 AM
Thanks kojala, that one worked, now I am gonna start connecting it up to either locators along a curve or the actual CV's, any ideas etc from people, I have a loop that goes through each CV and attaches a locator, now going to apply it with this script.

Many thanks again

Chris

CGTalk Moderation
11-03-2008, 10:53 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.