[Maya] How to get face normals average value?

Hi everyone!

I generally program Unity3D related stuff, I’m still pretty new to Maya’s python API.

What I’m trying to do is to get the currently selected face normals average:

What I need to achieve

What I achieved until now is an instance of a cube positioned according to the Move Manipulator.


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)

Now what I only need is to rotate the cube to the face average normals of the selection

Please, any thoughts about this?

Maya does have any method to give me those angles? I tried to rotate using polyInfo with face normals, but I think I’m missing something…

polyInfo will give you the normal, but it will be formatted as a string, so you need to extract the values out and convert to something usable, like a tuple.


cmds.polyInfo(polygonFace, faceNormals=True)

Thanks for the response @rgkovach123

I already did this in that part of my the code:


# 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])

But isn’t the exact value that I need for rotation:


cmds.rotate(polyInfoX, polyInfoY, polyInfoZ, target)

Maybe I’m missing a conversion or something…

Any thoughts?

I found a workaround, if anyone is interested in the answer, here is what I did for the final solution:

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()
target = cmds.group(world=True, empty=True, name=('Point'))

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

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

yea, the normal of the face can be twisted around any amount, so some additional information is required in the form of the up vector.