<p>在上/下/左/右与网格的变换(局部空间)对齐的规则下,这是一个相当一致的解决方案,尽管也可以是世界空间。在</p>
<p>我要做的第一件事是使用网格变换的平均面顶点位置、面法线和世界空间Y轴为每个网格面建立一个面相对坐标系。这涉及到一些向量数学,所以我将使用API来简化这一过程。第一部分将为每个面创建一个坐标系,我们将把它存储到列表中,以便将来查询。见下文。在</p>
<pre><code>from maya import OpenMaya, cmds
meshTransform = 'polySphere'
meshShape = cmds.listRelatives(meshTransform, c=True)[0]
meshMatrix = cmds.xform(meshTransform, q=True, ws=True, matrix=True)
primaryUp = OpenMaya.MVector(*meshMatrix[4:7])
# have a secondary up vector for faces that are facing the same way as the original up
secondaryUp = OpenMaya.MVector(*meshMatrix[8:11])
sel = OpenMaya.MSelectionList()
sel.add(meshShape)
meshObj = OpenMaya.MObject()
sel.getDependNode(0, meshObj)
meshPolyIt = OpenMaya.MItMeshPolygon(meshObj)
faceNeighbors = []
faceCoordinates = []
while not meshPolyIt.isDone():
normal = OpenMaya.MVector()
meshPolyIt.getNormal(normal)
# use the seconary up if the normal is facing the same direction as the object Y
up = primaryUp if (1 - abs(primaryUp * normal)) > 0.001 else secondaryUp
center = meshPolyIt.center()
faceArray = OpenMaya.MIntArray()
meshPolyIt.getConnectedFaces(faceArray)
meshPolyIt.next()
faceNeighbors.append([faceArray[i] for i in range(faceArray.length())])
xAxis = up ^ normal
yAxis = normal ^ xAxis
matrixList = [xAxis.x, xAxis.y, xAxis.z, 0,
yAxis.x, yAxis.y, yAxis.z, 0,
normal.x, normal.y, normal.z, 0,
center.x, center.y, center.z, 1]
faceMatrix = OpenMaya.MMatrix()
OpenMaya.MScriptUtil.createMatrixFromList(matrixList, faceMatrix)
faceCoordinates.append(faceMatrix)
</code></pre>
<p>这些函数将查找并返回哪个面位于相对于该面特定方向(X和Y)的给定面旁边。它使用点积来查看哪个面更倾向于那个特定的方向。这应该适用于任何数量的面,但它只返回一个面,该面位于该方向的大部分。在</p>
^{pr2}$
<p>所以你可以这样称呼它:</p>
<pre><code>getUpFace(123)
</code></pre>
<p>数字是脸部索引,你想从中得到“向上”的脸。在</p>
<p>试试这个,看看它是否满足你的需要。在</p>