简体   繁体   English

在特定轴上找到网格上的最近点(Maya)

[英]Find closest point on mesh in specific axis (maya)

Let's say I have one locator above a polyPlane. 假设我在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/ 我想做的是从定位器以负y或正y查找或跟踪,直到它碰到polyPlane并返回最近点/顶点/ 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 编辑:添加图像的第一个建议的解决方案和我想要实现之间的差异

在此处输入图片说明

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. 我们可以做的是使用OpenMaya(Maya的API)循环遍历数组中收集的faceVerts,检查与当前facevert相比,距定位器位置最短的距离,如果比最近的最短距离短,请保存它作为最近的Vertex变量。

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()
    selectionList.add(geo.name())
    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 :) 这可能是在没有API的情况下使用python来完成的,但是如果您拥有maya,就可以访问API了:)

I hope this helps 我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM