Skip to content

Instantly share code, notes, and snippets.

@Dogydogsun
Last active May 2, 2022 12:54
Show Gist options
  • Save Dogydogsun/1d15ebd2d0abb73347592b1255231821 to your computer and use it in GitHub Desktop.
Save Dogydogsun/1d15ebd2d0abb73347592b1255231821 to your computer and use it in GitHub Desktop.
A short Maya script for transferring selected object transforms to their offset matrixes. Deletes old transforms after finishing. Useful for creating controllers when rigging.
"""
To use, paste this code into the script window in Maya. Make sure to paste it into a Python panel.
You can then mark the entire script with ctrl+a, and middle-mouse drag it onto a shelf to create a shortcut button.
Script works with multiple selections. Simply select all the objects that you want to transfer the objects transform to their
offset matrices on, and then run the script.
@author Frederik N. S. Larsen
"""
from maya.api.OpenMaya import MEulerRotation, MMatrix
import math
s = cmds.ls(sl=True)
for f in s:
tX = cmds.getAttr(f"{f}.translateX")
tY = cmds.getAttr(f"{f}.translateY")
tZ = cmds.getAttr(f"{f}.translateZ")
rX = cmds.getAttr(f"{f}.rotateX")
rY = cmds.getAttr(f"{f}.rotateY")
rZ = cmds.getAttr(f"{f}.rotateZ")
cmds.setAttr(f"{f}.translateX", 0)
cmds.setAttr(f"{f}.translateY", 0)
cmds.setAttr(f"{f}.translateZ", 0)
cmds.setAttr(f"{f}.rotateX", 0)
cmds.setAttr(f"{f}.rotateY", 0)
cmds.setAttr(f"{f}.rotateZ", 0)
tMatrix = MMatrix(((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (tX, tY, tZ, 1)))
eRotation = MEulerRotation (math.radians(rX), math.radians(rY), math.radians(rZ))
rMatrix = eRotation.asMatrix()
newOffsetMatrix = rMatrix * tMatrix
cmds.setAttr(f"{f}.offsetParentMatrix", newOffsetMatrix, type="matrix")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment