Orienting a transform to a face with OpenMaya

Orienting a transform to a face with OpenMaya

AronDurkin
Enthusiast Enthusiast
283 Views
1 Reply
Message 1 of 2

Orienting a transform to a face with OpenMaya

AronDurkin
Enthusiast
Enthusiast

Hello!

I feel like this should be simple and I’m struggling a little. I’m trying to write something that will orient a transform to a face. Ie. It’s normal. I’m currently using a closestPointOnPoly node to get the a position on a face and it’s normal. Obviously moving a transform to a position is trivial but I dunno how to use that normal vector to turn that into a euler angle / matrix to set on the transform. I think you’d need another vector to derive a rotation?

Honestly I thought it’d be easy to say “point object Z+ towards this vector and we can ignore the twist around the axis for now” (although if there is a clever way to get the twist too that’d be great).

I’m familiar with cmds but I’ve only used OpenMaya a little.

Cheers

284 Views
1 Reply
Reply (1)
Message 2 of 2

brentmc
Autodesk
Autodesk

MQuaternion has a constructor that will construct a rotation to transform one vector into another one.

https://help.autodesk.com/view/MAYAUL/2024/ENU/?guid=MAYA_API_REF_cpp_ref_class_m_quaternion_html

 

If you want more control than a single axis then you will need to construct a 4x4 matrix from axis vectors that are unit length and at right angles to each other. In general you only need two axes and you can use a cross product to 1) find the 3rd axis and 2) make sure they are orthogonal (at right angles) to each other. You can then use MTransformationMatrix to extract the rotation (if needed) from the matrix.

Here is a post I found that pretty much describes how to contruct a matrix like this.

https://discourse.techart.online/t/vector-math-and-maya/4106/2

Brent McPherson
Principle Engineer