How to Rotate a Node from a Custom Pivot

On Maya python api 2.0 I am trying to rotate a node from a custom pivot.

This is how it looks like inside Maya

And this is my code:

import maya.OpenMaya as om
import maya.cmds as cmds

object_position = om.MVector(0.0, 1.0, 0.0)
pivot_pos = om.MVector(0.0, 0.0, 0.0)
twist_axis = om.MVector(0.0, 0.0, 1.0)
twist_value = 1

transform = om.MTransformationMatrix()
transform.setTranslation(object_position, om.MSpace.kWorld)
transform.setRotatePivot(
    om.MPoint(pivot_pos),
    om.MSpace.kWorld,
    True)

rotation_quat = om.MQuaternion(twist_value, twist_axis) 
transform.rotateBy(rotation_quat, om.MSpace.kTransform)
final_pos = transform.translation(om.MSpace.kTransform)

cmds.spaceLocator(p=(final_pos.x, final_pos.y, final_pos.z))

Doesn't seem to rotate from the provided pivot, What am I missing?

I may add that I've tried with rotatePivotTranslate with no luck either.

Thank you for your help!

3

1 Answer

A MTransformationMatrix is analogous to a transform node in Maya -- the rotation and translation values are independent, applying a rotation will not change the value of the translation component.

What's easier is to fall back on plain matrix math:

  1. Make your MTransformationMatrix, set it's translation to the pivot point you want.
  2. Get the relative position of the point you want to "rotate" by multiplying that point against the .asMatrixInverse() of the MTransformationMatrix
  3. Now apply your rotation.
  4. Then multiply the relative position from step 2 against the .asMatrix() of your MTransformationMatrix. The result is the new world space position you want.

import maya.api.OpenMaya as om
import maya.cmds as cmds

# from the question code...
object_position = om.MVector(0,1,0)
pivot_pos = om.MVector(0,0,0)
twist_axis = om.MVector(0.0, 0.0, 1.0)
twist_value = 1

# note that we set the translation to the _pivot pos_
transform = om.MTransformationMatrix()
transform.setTranslation(pivot_pos, om.MSpace.kWorld)

# get the relative position to the desired point
relative_pos = object_position * transform.asMatrixInverse()

# original rotation code remains the same
rotation_quat = om.MQuaternion(twist_value, twist_axis)
transform.rotateBy(rotation_quat, om.MSpace.kTransform)

# instead of checking the translation, 
# multiply the test point by the transform matrix
final_pos =  relative_pos * transform.asMatrix(1)

# Your locator pos will be rotated around the pivot you supplied
cmds.spaceLocator(p=(final_pos.x, final_pos.y, final_pos.z))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest