Not sure if this will help but I've been working on getting the orientation of 
a face in world space based on it's normal vector info and have come up with 
the help of this group have come up with this so far .... 

import maya.cmds as cmds
testObject = 'pCube1'
sl=cmds.ls(selection=True, flatten=True)
toVerts = cmds.polyListComponentConversion(sl, tv=True)
verts = cmds.ls(toVerts, flatten = True)
flatnormals = cmds.polyNormalPerVertex(verts, q=True, xyz=True)
normals = []
normals = zip(*[iter(flatnormals)]*3)
xNorm = [x[0] for x in normals]; xVector = sum(xNorm)/len(xNorm)
yNorm = [x[1] for x in normals]; yVector = sum(yNorm)/len(yNorm)
zNorm = [x[2] for x in normals]; zVector = sum(zNorm)/len(zNorm)

angles = cmds.angleBetween( euler=True, v1=(xVector, yVector, zVector), v2=(1, 
0, 0) )

# use lists to calculate center pos
xVal = []; yVal = []; zVal = []

for i in verts:
    p = cmds.pointPosition( i )
    xVal.append( p[0] )
    yVal.append( p[1] )
    zVal.append( p[2] )
    
# find average world position
x = sum(xVal) / len(xVal); y = sum(yVal) / len(yVal); z = sum(zVal) / len(zVal)

center = [x, y, z]

# move test obj into position
cmds.move( center[0], center[1], center[2], testObject)
cmds.rotate(angles[0], (angles[1]*-1), (angles[2]*-1), testObject, ws=True )
cmds.refresh(currentView=True)
cmds.rotate(-90, testObject ,z=1, rfl=1, r=1, os=1)

I've tested it with a joint instead of a mesh and it rotates the joint but 
doesn't orientate it, you probably just have to plug the angle info to the 
joint orientation directly. BUT, I hope it helps. If you do figure out how to 
calculate the y rotation to the direction of the line I would love to know as 
that's the last angle I'm having trouble with, the X and Z should angle 
perfectly to the selected component.

Cheers,

J

On Sunday, February 17, 2013 9:04:27 AM UTC+1, Jiet Shern Neo wrote:
> For your previous query, there is functions in the maya api to find connected 
> edges, or query which polygon is adjacent to each other, under the 
> MItMeshEdge and MItMeshPolygon.
>  
> As for the joint rotation, it is very hard to determine which direction you 
> want to rotate but one possible way is to use the face normal as a hint. For 
> example, you can assume that the result will always be that the face will 
> flip in the same direction as its face normal, so you can then use the normal 
> to determine which direction is correct. do a cross-product of the vector of 
> the edge and the vector from the center of the edge to the center of the face 
> and compare the resultant vector with the normal. if it is not parallel, then 
> flip the edge vertex order.
> 
> 
> 
> 
> On 17 February 2013 11:03, Bradon Webb <[email protected]> wrote:
> 
> I think the flipping is due to the point numbers.
> 
> 
> 
> you can see that the x axis will point in the direction of lowest number to 
> highest number but since maya orders the points there are cases where I would 
> want those axis to flip but I'm not sure what would be a good test to 
> determine if the axis needs to be flipped.
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> --
> 
> You received this message because you are subscribed to the Google Groups 
> "Python Programming for Autodesk Maya" group.
> 
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> 
> To post to this group, send email to [email protected].
> 
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to