Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

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

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

143110913
Enthusiast Enthusiast
607 Views
3 Replies
Message 1 of 4

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

143110913
Enthusiast
Enthusiast

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!

0 Likes
Accepted solutions (1)
608 Views
3 Replies
Replies (3)
Message 2 of 4

brentmc
Autodesk
Autodesk
Accepted solution

Hi,

You are computing a transformation matrix but only applying the position to your newly created locator.

Instead you need to apply the whole transformation you created like this:

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)

# create space locator at origin
locator = cmds.spaceLocator()

# convert command result to an MObject
selection = om.MSelectionList()
selection.add(locator[0])
object = om.MObject()
selection.getDependNode(0, object)

# Apply transform computed above to the locator
locatorXform = om.MFnTransform(object)
locatorXform.set(transform)
Brent McPherson
Principle Engineer
0 Likes
Message 3 of 4

brentmc
Autodesk
Autodesk

P.S. Your example is Maya Python API 1.0. To use API 2.0 you need:

 

import maya.api.OpenMaya as om

 

Brent McPherson
Principle Engineer
0 Likes
Message 4 of 4

143110913
Enthusiast
Enthusiast

That was it! Thank you for your help, Brent!

0 Likes