# Encuentra el punto más cercano en la malla en un eje específico (maya)

Let's say I have one locator above a polyPlane. What I want to do is a lookup or trace from the locator in negative or positive y until it hits the polyPlane and return the position of the closest point/vertex/uv/

I imagine this have been done one million times but the only examples I have found works by locating the closest point based on all axis which in my case is close to useless.

I would appreciate any help I could get!

Edit: Added image of the difference between the first suggested solution and what I want to achieve

preguntado el 12 de febrero de 14 a las 07:02

Do you need the UV hit (the actual point on the surface) or just the nearest vertex? -

nearest vertex will do fine (I imagine that's easier too get) @theodox -

## 1 Respuestas

What we can do is use OpenMaya (Maya's API) to loop over the faceVerts gathered in an array, check to see which is shortest distance from the locator position compared to the current facevert, if it is shorter than the last shortest distance, save it as the closestVertex variable.

``````import maya.OpenMaya as OpenMaya
from pymel.core import *

geo = PyNode('pSphere1')
pos = PyNode('locator1').getRotatePivot(space='world')

nodeDagPath = OpenMaya.MObject()
try:
selectionList = OpenMaya.MSelectionList()
nodeDagPath = OpenMaya.MDagPath()
selectionList.getDagPath(0, nodeDagPath)
except:
warning('OpenMaya.MDagPath() failed on %s' % geo.name())

mfnMesh = OpenMaya.MFnMesh(nodeDagPath)

pointA = OpenMaya.MPoint(pos.x, pos.y, pos.z)
pointB = OpenMaya.MPoint()
space = OpenMaya.MSpace.kWorld

util = OpenMaya.MScriptUtil()
util.createFromInt(0)
idPointer = util.asIntPtr()

mfnMesh.getClosestPoint(pointA, pointB, space, idPointer)
idx = OpenMaya.MScriptUtil(idPointer).asInt()

faceVerts = [geo.vtx[i] for i in geo.f[idx].getVertices()]
closestVertex = None
minLength = None
for v in faceVerts:
thisLength = (pos - v.getPosition(space='world')).length()
if minLength is None or thisLength < minLength:
minLength = thisLength
closestVertex = v
select(closestVertex)
``````

This could probably be done with python without the API, but if you've got maya, you've got access to the API :)

espero que esto ayude

Respondido 12 Feb 14, 11:02

Thanks for your answer! The problem with that solution is that it looks for faces in all directions, I only want it to look in one axis.I've added an image in the original post to clarify the issue - user2832718

I have provided you with a starting point, what you could do from here is in the loop at the bottom, where we were checking that thisLength is < minLength, instead of this, we could simply check for the vert that has the smallest distance from X and Z, and store that in an array, then once you're done that loop, you would loop over that array and check for shortest distance from each point. If you get stuck let me know but it's better to try then someone else write the code for you :) - Shannon Hochkins

I did not mean to be ungrateful. I misunderstood the approach you suggested and didn't think it could be tweaked to be applied for my situation. I will do my best to modify it but I will probably come back in a few days with an exploded head :) - user2832718

No that's fine, at least you had a go, maybe when you come back edit your question with what you've tried and I'll see if I can help you :) - Shannon Hochkins

Remember to click the arrow to accept my answer if it helped you find your answer :) - Shannon Hochkins

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.