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...
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!
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:
- Make your
MTransformationMatrix, set it's translation to the pivot point you want. - Get the relative position of the point you want to "rotate" by multiplying that point against the
.asMatrixInverse()of theMTransformationMatrix - Now apply your rotation.
- Then multiply the relative position from step 2 against the
.asMatrix()of yourMTransformationMatrix. 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))