Skip to content

Instantly share code, notes, and snippets.

@robmoggach robmoggach/idmx.py
Created Sep 28, 2017

Embed
What would you like to do?
Identity Matrix Locator Creator
import maya.cmds as cmds
import maya.mel as mel
import maya.OpenMaya as OpenMaya
class IdMx():
"""
Identity Matrix Locator Creator Class
written by Robert Moggach, 2010
This creates an animated locator that represents
the identity matrix for two selected
objects
"""
def __init__(self):
self.name = "idMxWin"
self.title = "Channel Export"
self.currentFrame = cmds.currentTime(query=True)
self.startFrame = cmds.playbackOptions(query=True, animationStartTime=True)
self.endFrame = cmds.playbackOptions(query=True, animationEndTime=True)
self.frames = range(int(self.startFrame), int(self.endFrame+1))
self.selection=self.getSelection()
self.refObject=self.selection[0]
self.tgtObjects=self.selection[1:]
self.refMxList=self.returnMatrixForNode(self.refObject)
self.inMxList=self.returnMatrixListForNodes(self.tgtObjects)
self.outMxList=self.returnRelativeMatrixList(self.refMxList, self.inMxList)
self.targets=self.createTargetList(self.tgtObjects)
self.applyMatrixListToTargetList(self.outMxList, self.targets)
def mtx2lst(self, mtx):
lst=[]
for i in range(0,4):
for j in range(0,4):
lst.append(mtx(i,j))
return lst
def lst2mtx(self, lst):
mtx=OpenMaya.MMatrix()
OpenMaya.MScriptUtil.createMatrixFromList(lst, mtx)
return mtx
def getSelection(self):
selection = cmds.ls(long=False, selection=True)
return selection
def returnMatrixForNode(self, node):
mtx=[]
for frame in self.frames:
mtx.append(cmds.getAttr("%s.wm" % node, t=frame))
return mtx
def returnMatrixListForNodes(self, nodes):
mtx=[]
for node in nodes:
objMx=self.returnMatrixForNode(node)
mtx.append(objMx)
return mtx
def createTargetList(self, tgtList):
targets=[]
for obj in tgtList:
targets.append(cmds.createNode('transform'))
return targets
def applyMMatrix(self, mtx, node):
mtx = mtx2lst(mtx)
cmds.xform(node, ws=True, m=mtx)
cmds.setKeyframe(node)
def applyMatrix(self, mtx, node):
cmds.xform(node, ws=True, m=mtx)
cmds.setKeyframe(node)
def returnRelativeMatrix(self, refMx, inMx):
refMMx=self.lst2mtx(refMx)
inMMx=self.lst2mtx(inMx)
#outMMx=refMMx.inverse() * inMMx
#outMMx=inMMx * refMMx.inverse()
#outMMx=inMMx.inverse() * refMMx
#outMMx=refMMx * inMMx.inverse()
outMMx=inMMx * refMMx.inverse()
outMx=self.mtx2lst(outMMx)
return outMx
def returnRelativeMatrixList(self, refMx, inMxList):
outMxList=[]
for nodeIdx, nodeMxList in enumerate(inMxList):
outMx=[]
for fr, frMx in enumerate(nodeMxList):
outFrMx = self.returnRelativeMatrix(refMx[fr], frMx)
outMx.append(outFrMx)
outMxList.append(outMx)
return outMxList
def applyMatrixListToTargetList(self, mxList, tgtList):
for frIdx, frame in enumerate(self.frames):
print frame
cmds.currentTime(frame)
for nodeIdx, node in enumerate(tgtList):
print "nodeIdx: %s, node: %s, mx: %s" % (nodeIdx, node, mxList[nodeIdx][frIdx])
self.applyMatrix(mxList[nodeIdx][frIdx], node)
# print "Time: %s, Matrix: %s" % (frame, mxList[nodeIdx][frIdx])
cmds.currentTime(self.currentFrame)
# mtxA=cmds.getAttr('hlxA.wm')
# mtxB=cmds.getAttr('hlxB.wm')
# mtxA=lst2mtx(mtxA)
# mtxB=lst2mtx(mtxB)
# mtxBA=mtx2lst(mtxA.inverse()*mtxB)
# mtxAB=mtx2lst(mtxB.inverse()*mtxA)
# cmds.xform('hlxAB', ws=True, m=mtxAB)
# cmds.xform('hlxBA', ws=True, m=mtxBA)
# cmds.currentTime(fr)
# cmds.setAttr(selectedChan, importValue)
# cmds.setKeyframe(selectedChan)
# cmds.setKeyframe(selected, at=chan, v=importValue, t=fr)
IdMx=IdMx()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.