How to rotate a node from a custom pivot - Maya API

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?

Thank you for your help!

I think you’re trying to change the pivot of the object and rotate it from that new pivot? But the position you supplied in the cmds.spaceLocator() command is just a vector position. You need to tell it to rotate separately. I don’t know what the API equivalent would be as I’m not quite well versed enough

I answered this one on StackOverflow. TLDR: just like in the channel box, a rotation value may change how a transform looks but it can’t change the actual content of the translation. You need to grab the matrix of your pivot, get the relative position of your desired point, then rotate the matrix and multiply the relative point against that to get a transformed position.