gpt4 book ai didi

python - 如何在 Maya 中获得面部法线平均值?

转载 作者:行者123 更新时间:2023-12-01 04:29:37 26 4
gpt4 key购买 nike

我通常编写 Unity3D 相关的东西,我对 Maya 的 python API 还很陌生。

我想做的是获取当前选定的面部法线平均值:

What I need to achieve

到目前为止,我所实现的是根据移动操纵器定位的立方体实例。

Code result

import maya.cmds as cmds
import re #regular expression

# get current position of the move manipulator
pos = cmds.manipMoveContext('Move', q=True, p=True)

# get the current selection
selection = cmds.ls(sl=True)

# trying to get the face normal angles of the current selection
polyInfo = cmds.polyInfo(selection, fn=True)
polyInfoArray = re.findall(r"[\w.-]+", polyInfo[0]) # convert the string to array with regular expression
polyInfoX = float(polyInfoArray[2])
polyInfoY = float(polyInfoArray[3])
polyInfoZ = float(polyInfoArray[4])

print str(polyInfoX) + ', ' + str(polyInfoY) + ', ' + str(polyInfoZ)

target = cmds.polyCube()

cmds.move(pos[0], pos[1], pos[2], target)

现在我只需要将立方体旋转到选择的面平均法线

请问,对此有什么想法吗?

Maya 有什么方法可以给我这些角度吗?我尝试使用带有面部法线的polyInfo进行旋转,但我认为我遗漏了一些东西......

编辑:

最终解决方案(感谢@theodox)

import maya.cmds as cmds

# get current position of the move manipulator
pos = cmds.manipMoveContext('Move', query=True, position=True)

# get the current selection
selection = cmds.ls(selection=True)

target = cmds.polyCube()

cmds.move(pos[0], pos[1], pos[2], target)

constr = cmds.normalConstraint(selection, target, aimVector = (0,0,1), worldUpType= 0)
cmds.delete(constr)

最佳答案

你可以在不解决问题中的问题的情况下实现你想要的。 normalConstraint 节点会将对象定向到最接近的可用面法线。如果您只想面部对齐,则可以定位对象,然后创建并立即删除法线约束。最小版本是:

 constr = cmds.normalConstraint('reference_object', 
'object_to_align',
aimVector = (0,0,1),
worldUpType= 0)
cmds.delete(constr)

其中目标向量是将与表面对齐的局部轴。 (有很多关于如何进行对齐的选项,您应该查看 normalConstraint 命令中的文档以了解更多信息)。

要真正获得面法线,您可以使用polyNormalPerVertex命令来获取面的顶点面法线(它们将以数字形式返回,因此您无需对其进行正则表达式) .您需要对它们进行平均:

def face_normal(face):
vtxface = cmds.polyListComponentConversion(face, tvf = True)
xes = cmds.polyNormalPerVertex(vtxface, q=True, x =True)
yes = cmds.polyNormalPerVertex(vtxface, q=True, y =True)
zes = cmds.polyNormalPerVertex(vtxface, q=True, z =True)
divisor = 1.0 / len(xes)
return sum(xes)* divisor, sum(yes) * divisor, sum(zes) * divisor

但是这些都在本地空间。将它们转换为世界空间的唯一方法是将它们转换为向量(使用 Pymel 或 Maya api),然后将它们与对象矩阵相乘(也使用 API)。一旦有了世界空间向量,您就可以使用该向量作为“目标向量”、世界向上以及这些向量的交叉向量作为要对齐的对象的新矩阵来构造一个新矩阵。

...所有这些都不是 super 难,但如果您还不太了解 API,则需要做很多工作。这就是为什么我首先尝试使用 normalConstraint 技巧。

关于python - 如何在 Maya 中获得面部法线平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32555962/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com