Maya: Select vertex based on position

I’m wondering if it’s possible to select a vertex or UV just by knowing it’s position?

Lets say pSphere1.vtx[123] is positioned at wordspace position 21.31 4.22 4.66, is it then possible to select that vertex without knowing the name of the vertex?

The reason why I’m asking, is that I want to create a ‘Mirror Cluster’ script. So if the object is symetrical and positioned in the center of the scene, I should be able to get the position (vertexes) on the other side.

I could always select the other side manually, and then loop through and compare each vertex’s position, but that takes time :(:

1 Like

If you’re willing to delve into the API a bit, there’s a MFnMesh::getClosestPoint() method that should suit your purposes (if you’re scared, i could probably whip up a python example for you)

I have to admit that the API scares me a little, I guess I should take a stab at it some day :lookdown:

An example would really be appreciated! :nod:

I’m assuming you’re in python here…

You probably want to read up about some simple maya API stuff, you only need to know a little to get some great functionality in your python scripts. Learn about MObjects and MDagPaths etc…

Find the docs for MFnMesh, browse down to closestPoint. Have a read.

I had a quick test of this code and it ran ok, see if it works for you


import maya, maya.OpenMaya

# Name of object
name = '|pCube1|pCubeShape1'

# Pointer to object in maya's memory...sort of
nodeDagPath = maya.OpenMaya.MObject()

try:
	selectionList = maya.OpenMaya.MSelectionList()
	selectionList.add(name)
	nodeDagPath = maya.OpenMaya.MDagPath()
	selectionList.getDagPath(0, nodeDagPath)
except:
	raise RuntimeError('NameToNode() failed on %s' % name)

# A function set to wrap around the object, indicating you can perform mesh operations on it
mfnMesh = maya.OpenMaya.MFnMesh(nodeDagPath)

pointA = maya.OpenMaya.MPoint(0,0,0)
pointB = maya.OpenMaya.MPoint()
space = maya.OpenMaya.MSpace.kWorld

# Maya's way of dealing with pointers
util = maya.OpenMaya.MScriptUtil()
util.createFromInt(0)
idPointer = util.asIntPtr()

mfnMesh.getClosestPoint(pointA, pointB, space, idPointer)
print maya.OpenMaya.MScriptUtil(idPointer).asInt()


[QUOTE=dbsmith;8233]I’m assuming you’re in python here…

You probably want to read up about some simple maya API stuff, you only need to know a little to get some great functionality in your python scripts. Learn about MObjects and MDagPaths etc…

Find the docs for MFnMesh, browse down to closestPoint. Have a read.

I had a quick test of this code and it ran ok, see if it works for you

[/QUOTE]
Thanks for your reply :)

I'm actually in MEL, that's the reason why I haven't dived into the API yet :lookdown: 

I didn't get your example to work though, if I select a vertex of a pCube and run it, it just outputs 0. If I try with a pSphere it outputs 177. I tried to make sense of your code, but I wasn't able to figure out how it's supposed to work :|:

Looks like maya’s getClosestPoint() doesn’t behave the way I thought it would. It definitely isn’t getting the closest point…

Theres a similar method on MMeshIntersector, but that gets a point on the mesh, not necessarily a vertex.

I’m sure theres a good way to do it with API, hopefully someone in the forums will know.

Otherwise there are smart ways to compare large amounts of verts with a point, using bounding boxes, sorting algorithms, octrees etc…

Thanks for your reply :slight_smile:

I’m actually in MEL, that’s the reason why I haven’t dived into the API yet

I didn’t get your example to work though, if I select a vertex of a pCube and run it, it just outputs 0. If I try with a pSphere it outputs 177. I tried to make sense of your code, but I wasn’t able to figure out how it’s supposed to work

Thats the index of the closest face/polygon, and the point
that that command gives is just on the surface of the mesh, not on any vertex.
You can loop trough the face thats found and compare each vertex in it which is the closest to your point if you really want the closest vertex.

Does it give you barycentric coords of the point, or object/world space coords of the point?

space = maya.OpenMaya.MSpace.kWorld
mfnMesh.getClosestPoint(pointA, pointB, space, idPointer)

In this case, world space coords.

PyMEL might have some helper methods for ya:

from pymel.all import *

poly = PyNode(‘pSphere1’)
pos = [0,0,0]
count = 0
for point in poly.getPoints(‘world’):
if dt.Vector(point) == dt.Vector(pos):
select(poly.vtx[count])
count +=1

Can you use a poly selection constraint?

In the bonus tools there is the pointOnMeshInfo and pointOnMesh which should give you the access you need to do those conversions (i.e. get a point in world space coordinates based off a UV space input).

[QUOTE=shcmack;8214]I’m wondering if it’s possible to select a vertex or UV just by knowing it’s position?

Lets say pSphere1.vtx[123] is positioned at wordspace position 21.31 4.22 4.66, is it then possible to select that vertex without knowing the name of the vertex?

The reason why I’m asking, is that I want to create a ‘Mirror Cluster’ script. So if the object is symetrical and positioned in the center of the scene, I should be able to get the position (vertexes) on the other side.

I could always select the other side manually, and then loop through and compare each vertex’s position, but that takes time :(:[/QUOTE]

I use polySelectConstraint in my mirroring scripts to do this very thing.
If you want to dissect the script just do a control f (find) fer polySelectConstraint.

download