Skip to content

Instantly share code, notes, and snippets.

@RedForty
Last active September 10, 2021 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RedForty/6a6e8868290fb1b1bf07688c601f2314 to your computer and use it in GitHub Desktop.
Save RedForty/6a6e8868290fb1b1bf07688c601f2314 to your computer and use it in GitHub Desktop.
from maya import cmds
import maya.OpenMaya as om
import math
LOCATOR_NAME = '_loc'
def get_rotation_from_matrix(matrix_list, rot_order):
# Create an empty MMatrix:
mMatrix = om.MMatrix() # MMatrix
# And populate the MMatrix object with the matrix list data:
om.MScriptUtil.createMatrixFromList(matrix_list, mMatrix)
#-------------------------------------------
# Part 2, get the euler values
# Convert to MTransformationMatrix to extract rotations:
mTransformMtx = om.MTransformationMatrix(mMatrix)
# Get an MEulerRotation object
eulerRot = mTransformMtx.eulerRotation() # MEulerRotation
# Update rotate order to match original object, since the orig MMatrix has
# no knoweldge of it:
eulerRot.reorderIt(rot_order)
# Convert from radians to degrees:
angles = [math.degrees(angle) for angle in (eulerRot.x, eulerRot.y, eulerRot.z)]
return angles
if __name___ == "__main__":
nodes = cmds.ls(sl=1)
start = cmds.timerX()
# code that is being timed
for node in nodes:
# Uncomment this if you know what you're doing
# if not cmds.nodeType(node) == 'transform': continue
loc = cmds.spaceLocator(name=node + LOCATOR_NAME)
rot_order = cmds.getAttr(loc[0] + '.rotateOrder')
keyframes = cmds.keyframe(node, q=True)
keyframes = list(set(keyframes))
for key in keyframes:
matrix_list = cmds.getAttr(node + '.worldMatrix', time=key)
# Translation
cmds.setKeyframe(loc[0], attribute='translateX', value=matrix_list[12], time=key)
cmds.setKeyframe(loc[0], attribute='translateY', value=matrix_list[13], time=key)
cmds.setKeyframe(loc[0], attribute='translateZ', value=matrix_list[14], time=key)
# Rotation
angles = get_rotation_from_matrix(matrix_list, rot_order)
cmds.setKeyframe(loc[0], attribute='rotateX', value=angles[0], time=key)
cmds.setKeyframe(loc[0], attribute='rotateY', value=angles[1], time=key)
cmds.setKeyframe(loc[0], attribute='rotateZ', value=angles[2], time=key)
# Done
cmds.currentTime(cmds.currentTime(q=True))
totalTime = cmds.timerX(startTime=start)
print "Total time: ", totalTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment