python - 查找特定轴上网格上最近的点(maya)

标签 python maya mel pymel

假设我在多平面上方有一个定位器。我想要做的是从定位器以负或正 y 进行查找或跟踪,直到它到达 polyPlane 并返回最近点/顶点/uv/的位置

我想这已经完成了一百万次,但我发现的唯一例子是通过基于所有轴定位最近的点来工作的,在我的例子中,这几乎是无用的。

如果我能得到任何帮助,我将不胜感激!

编辑: 添加了第一个建议的解决方案与我想要实现的目标之间的差异的图像

enter image description here

最佳答案

我们可以做的是使用OpenMaya(Maya的API)循环收集在数组中的faceVerts,检查与当前facevert相比,距定位器位置的最短距离,如果它比最后一个最短距离短,将其保存为closestVertex变量。

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)

这可能可以在没有 API 的情况下使用 python 来完成,但如果您有 Maya,则可以访问 API :)

希望这有帮助

关于python - 查找特定轴上网格上最近的点(maya),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21720904/

相关文章:

python - 如何在 python 中打印颜色/颜色?

python - 将 settings.py 配置文件加载到字典中

python - 如何为视频播放器选择文件

c# - 解释 PATH 的意义

python - 将随机颜色分配给灰度范围内的顶点

python - 如何从 Maya 中启动 Nuke?

python - 使用Python中指定的程序打开特定文件。

python - 从文件名路径列表中仅提取 Maya 文件中使用的纹理文件名

python - 在带有动画子对象的父对象上卡住缩放变换(MAYA MEL/Python 脚本)

nodes - 如何在 Maya MEL 语言中创建一个空节点