MFnMesh ALINTERSECTIONS返回错误结果

2024-09-28 21:01:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Maya的api来测试一个点是否位于某个网格内,方法是拍摄一条光线并查看它击中多少面。我是用MFnMesh.allIntersections来做这件事的。我想到的问题是,有时该方法返回的结果是我不期望的,也没有意义的!例如,我正在测试球体底部的一个点,其法线如下:

Example

就像图中一样,它应该达到2点,但出于某种原因,它说它达到了3点! 为什么会这样?我将包含复制此示例的代码:

import maya.cmds as cmds
import maya.OpenMaya as OpenMaya


cmds.file(new=True, force=True)

cmds.polyCube(subdivisionsWidth=4, subdivisionsHeight=4, subdivisionsDepth=4)
cmds.setAttr("pCube1.translate", 0, 0.236, 0)

cmds.polySphere(subdivisionsAxis=25, subdivisionsHeight=25)
cmds.setAttr("pSphere1.translate", 0, 2, 0)

dag_path = OpenMaya.MDagPath()
sel = OpenMaya.MSelectionList()
sel.add("pCube1")
sel.getDagPath(0, dag_path)

mfn_mesh = OpenMaya.MFnMesh(dag_path)

hit_points = OpenMaya.MFloatPointArray()
hit_ray_params = OpenMaya.MFloatArray()
hit_faces = OpenMaya.MIntArray()

has_int = mfn_mesh.allIntersections(
    OpenMaya.MFloatPoint(0, 1, 0), # Should match pSphere1.vtx[600]'s world position
    OpenMaya.MFloatVector(1.1905393115796414e-08, -1.0, 1.8535209278525144e-07), # Should match pSphere1.vtx[600]'s normal
    None,
    None,
    False,
    OpenMaya.MSpace.kWorld,
    999999,
    False,
    mfn_mesh.autoUniformGridParams(),
    False,
    hit_points,
    hit_ray_params,
    hit_faces,
    None,
    None,
    None,
    0.0001
)

print hit_points.length() # Should return 2 but returns 3!!!

Tags: path方法nonefalsepointsdagmeshcmds
1条回答
网友
1楼 · 发布于 2024-09-28 21:01:06

因此,当MFnMesh.allIntersections与一条边或一个顶点正好相交时,它将返回多个命中点,而不是返回一个命中点,因此有可能在同一位置获得多个命中。我对该方法的容差值进行了调整,但没有效果。因此,当命中发生时,我可以使用MFloatVector.isEquivalent并修剪几乎相同的任何位置。现在我得到了预期的输出

相关问题 更多 >