View Full Version : How to get ID number of the nearest vertice?

11 November 2006, 01:28 PM
Hey guys,

Is there a quick way to get the ID number of the nearest vertice with worldspace point as input like with nearestPointOnMesh?

I'm using the longest way at the moment.
- get the amount of vertices of selected mesh for looping
- get the distance between given worldspace point and vertice with ID number from the current loop value
- if the distance is closer than the distance stored, a new ID number is stored
- when the loop is completed, the vertice is selected using the final ID number

It worked, but I had to wait longer if I want to run the script on a heavy mesh.

Thanks in advance,

11 November 2006, 05:31 PM
You can't get the closest vertex index, with nearestPointOnMesh. But, you can get the closest face index, `gettAttr someNearestPointOnMeshNode.nearestFaceIndex`. With this you could get the verticies associated with that face and then you would only have to run your distance test on those verticies.

12 December 2006, 02:23 AM
polySelectConstraint -t 0x001 -m 3 -d 1 -dp $pos[0] $pos[1] $pos[2] -db 0 $tolerance ;
print`ls -sl -fl`;
polySelectConstraint -m 0;//this resets the selection so you can still select other vertices.

Sorry I don't write the full names of the flags out, but I don't remember them offhand.

You need to be in component mode, it will select the vertex or vertices that lie in the area within the tolerance, and then print out the list of vertex/CV ids.

12 December 2006, 09:27 AM
What I like about MEL scripting is that there are lots of ways to solve the problem. Thanks for the wonderful tip. That's exactly what I'm looking for.


12 December 2006, 05:10 PM
Yeah, not sure if it's faster or slower than, say, using nearestPointOnMesh method described above, or using mag and a dual lookup table/comparison loop, but I definitely got it streamlined ;)

Definitely not the fastest method since it actually selects the point/points within your $tolerance "bounding box".

12 December 2006, 10:48 AM
I tested different methods to select vertices randomly. first method was string based. I had an object and in mel I converted them to vertices and also created a rand function and by that selected some vertices. if ($rand > 0.1)...
that method was ssllllooowww. it used huges of amounts of memory and finally crashed maya in large objects.

Second method was using selection sets:

string $vtx = `sets -empty`;
sets -add $vtx `ls -sl`;

I always made selections and if I had to save the selection I made it a set.
And in the end of the mel I just deleted the sets.

third and fastest way where the polySelectConstrains and there where built-in random function!.

That nearesPointOnMesh command is still untested.

12 December 2006, 06:58 PM
well if you want to test out the closest point on mesh method here you go:

global proc float gtCalcDistance(float $p1[], float $p2[]){
return(sqrt(pow($p1[0] - $p2[0],2) + pow($p1[1] - $p2[1],2) + pow($p1[2] - $p2[2],2)));

global proc string gtGetClosestVertex(string $sampleObj,float $samplePoint[]) {
string $CPOM =`closestPointOnMesh $sampleObj`;
string $faceVerts[];
string $result;
float $distance;
float $testDistance;
float $vertPos[];

if(`pluginInfo -q -l closestPointOnMesh` == 0)
loadPlugin "closestPointOnMesh";
setAttr ($CPOM + ".inPosition") $samplePoint[0] $samplePoint[1] $samplePoint[2];
$faceVerts = eval("polyListComponentConversion -tv " + $sampleObj + ".f[" + getAttr($CPOM + ".closestFaceIndex") + "]");
$faceVerts = `ls -fl $faceVerts`;
$vertPos = `xform -q -ws -t $faceVerts[0]`;
$distance = gtCalcDistance($samplePoint,$vertPos);
$result = $faceVerts[0];
$vertPos = `xform -q -ws -t $faceVerts[$i]`;
$testDistance = gtCalcDistance($samplePoint,$vertPos);
$distance = $testDistance;
$result = $faceVerts[$i];
delete $CPOM;

01 January 2007, 11:07 PM
Sorry for draging this old thing up again, but I am working on something similar.

For some time the "closestPointOnMesh" plugin was working, but now I cant find it anymore. Any ideas on how I can get it back or where to download it? In the plugin editor it only says nearestPointOnMesh, which does any return the closest face and does not really work in my case.


CGTalk Moderation
01 January 2007, 11:08 PM
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.