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

时间:2015-09-14 00:46:56

标签: python maya mel

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

我要做的是获得当前选择的面部法线平均值:

What I need to achieve

到目前为止,我所获得的是根据Move Manipulator定位的立方体实例。

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)

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

请问,对此有何想法?

玛雅有没有办法给我这些角度?我尝试使用面向法线的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)

1 个答案:

答案 0 :(得分:2)

您可以在不解决问题的情况下实现您想要的目标。 max-width:90%节点将使对象朝向最接近的可用面法线。如果您只想要面对齐,可以定位对象,然后创建并立即删除normalConstraint。最小版本将是:

normalConstraint

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

要实际让脸部正常,您可以使用 constr = cmds.normalConstraint('reference_object', 'object_to_align', aimVector = (0,0,1), worldUpType= 0) cmds.delete(constr) 命令获取脸部的顶点面法线(它们会以数字的形式返回,因此您不需要正则表达式它。你需要平均它们:

polyNormalPerVertex

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

......所有这些都不是超级很难但如果你还不熟悉API,那么它的工作量很大。这就是为什么我首先尝试normalConstraint技巧。