PDA

View Full Version : nearestpointonmesh


twilightened
12-26-2011, 12:22 PM
on a simple (unmodified) mesh, this command works proper ( it shows the nearest face )
when modified ( add divisions ) , it does not work as designed. How is it possible to get the nearest face/point on a mesh after it has been further subdivided


string $token[], $token2[], $shapes[];

int $vert;

spaceLocator -n "reference";

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

// select all objects
string $selectedObjects[] = `ls -sl`;

// select source object
string $sourceObject = $selectedObjects[0];
string $shapes[] = `listRelatives -s -path $sourceObject`;

connectAttr -f ($sourceObject+".worldMesh") ($closestPoint + ".inMesh");
// destination object is rest of $selectedObjects

select -clear;

for ( $i = 1; $i < size($selectedObjects);$i++)
{
//connect locator1 pos' to closestPointOnMesh pos'
connectAttr -f ($selectedObjects[$i]+".translateX") cpom1.inPositionX;
connectAttr -f ($selectedObjects[$i]+".translateY") cpom1.inPositionY;
connectAttr -f ($selectedObjects[$i]+".translateZ") cpom1.inPositionZ;

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

string $ftv[] = `polyInfo -faceToVertex ($sourceObject + ".f[" + $selectedFaces + "]")`;

tokenize $ftv[0] " :\n\r" $token2;

$vert = int($token2[2]);

float $coords[] = `xform -ws -q -t ($shapes[0] + ".vtx[" + $vert + "]")`;

select -r reference;

move -a -ws $coords[0] $coords[1] $coords[2];

select -tgl $selectedObjects[$i];

parentConstraint -weight 1 ;

parentConstraint -remove;


select -d $selectedObjects[$i];
}


select -r reference;
doDelete;

djx
12-26-2011, 01:13 PM
After you make changes to the mesh, this
string $sourceObject = $selectedObjects[0];
will no longer give you what you want.
You need to be connecting the output from the polySmoothFace (or whatever you used).

David

twilightened
12-27-2011, 02:03 AM
After you make changes to the mesh, this
string $sourceObject = $selectedObjects[0];
will no longer give you what you want.
You need to be connecting the output from the polySmoothFace (or whatever you used).

David


got it.

string $sourceObject = $selectedObjects[0]; this line gets the selected object. in my case the mesh. Your second point i will try it connecting in the nearestpointonmesh node and get back. thank you for replying.

djx
12-27-2011, 02:13 PM
What I meant was, that mel can be lazy (or smart) about you specify connections. For example:
connectAttr -f "pSphere1.worldMesh" "cpom1.inMesh";
// Result: Connected pSphereShape1.worldMesh to cpom1.inMesh. //



Notice how I specified pSphere1, but maya actually connected pSphereShape1. So your code was a bit lazy and you depend on maya being a bit smart. Better if you be smart and not rely on maya. You should be specific about the shape node name. And do not always assume it will be in the [0] index (even if it often will be). If you have a deformer then there could be more than one shapeNode. Have a look at the connections in the hypergraph and you will see what I mean.

David

twilightened
01-02-2012, 11:50 AM
after adding divisions to a mesh and then connecting to nearestpointonmesh node ( the above code), it mal functions. I need to divide the mesh. How is it possible to get a proper result in such a condition.

CGTalk Moderation
01-02-2012, 11:50 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.