rockingTransform.py
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
import math
import sys
kRockingTransformPluginName = "spRockingTransform"
kRockingTransformNodeName = "spRockingTransformNode"
kRockingTransformNodeID = OpenMaya.MTypeId(0x87014)
kRockingTransformMatrixID = OpenMaya.MTypeId(0x87015)
kTrackingDictionary = {}
class rockingTransformMatrix(OpenMayaMPx.MPxTransformationMatrix):
def __init__(self):
OpenMayaMPx.MPxTransformationMatrix.__init__(self)
kTrackingDictionary[OpenMayaMPx.asHashable(self)] = self
self.rockXValue = 0.0
def __del__(self):
del kTrackingDictionary[OpenMayaMPx.asHashable(self)]
def setRockInX(self,rockingValue):
self.rockXValue = rockingValue
def getRockInX(self):
return self.rockXValue
def asMatrix(self,percent=None):
"""
Find the new matrix and return it
"""
if percent == None:
matrix = OpenMayaMPx.MPxTransformationMatrix.asMatrix(self)
tm = OpenMaya.MTransformationMatrix(matrix)
quat = self.rotation()
rockingValue = self.getRockInX()
newTheta = math.radians( rockingValue )
quat.setToXAxis( newTheta )
tm.addRotationQuaternion( quat.x, quat.y, quat.z, quat.w, OpenMaya.MSpace.kTransform )
return tm.asMatrix()
else:
m = OpenMayaMPx.MPxTransformationMatrix(self)
trans = m.translation()
rotatePivotTrans = m.rotatePivot()
scalePivotTrans = m.scalePivotTranslation()
trans = trans * percent
rotatePivotTrans = rotatePivotTrans * percent
scalePivotTrans = scalePivotTrans * percent
m.translateTo(trans)
m.setRotatePivot( rotatePivotTrans )
m.setScalePivotTranslation( scalePivotTrans )
quat = self.rotation()
rockingValue = self.getRockInX()
newTheta = math.radians( rockingValue )
quat.setToXAxis( newTheta )
m.rotateBy( quat )
eulerRotate = m.eulerRotation()
m.rotateTo( eulerRotate * percent, OpenMaya.MSpace.kTransform )
s = self.scale( OpenMaya.MSpace.kTransform )
s.x = 1.0 + ( s.x - 1.0 ) * percent
s.y = 1.0 + ( s.y - 1.0 ) * percent
s.z = 1.0 + ( s.z - 1.0 ) * percent
m.scaleTo( s, OpenMaya.MSpace.kTransform )
return m.asMatrix()
class rockingTransformNode(OpenMayaMPx.MPxTransform):
aRockInX = OpenMaya.MObject()
def __init__(self, transform=None):
if transform is None:
OpenMayaMPx.MPxTransform.__init__(self)
else:
OpenMayaMPx.MPxTransform.__init__(self, transform)
self.rockXValue = 0.0
def createTransformationMatrix(self):
return OpenMayaMPx.asMPxPtr( rockingTransformMatrix() )
def className(self):
return kRockingTransformNodeName
def validateAndSetValue(self, plug, handle, context):
if not plug.isNull():
block = self._forceCache(context)
blockHandle = block.outputValue(plug)
if plug == self.aRockInX:
rockInX = handle.asDouble()
blockHandle.setDouble(rockInX)
ltm = self.getRockingTransformationMatrix()
if ltm is not None:
ltm.setRockInX(rockInX)
blockHandle.setClean()
self._dirtyMatrix()
OpenMayaMPx.MPxTransform.validateAndSetValue(self, plug, handle, context)
def getRockingTransformationMatrix(self):
baseXform = self.transformationMatrixPtr()
return kTrackingDictionary[OpenMayaMPx.asHashable(baseXform)]
def matrixCreator():
return OpenMayaMPx.asMPxPtr( rockingTransformMatrix() )
def nodeCreator():
return OpenMayaMPx.asMPxPtr( rockingTransformNode() )
def nodeInitializer():
numFn = OpenMaya.MFnNumericAttribute()
rockingTransformNode.aRockInX = numFn.create("RockInX", "rockx", OpenMaya.MFnNumericData.kDouble, 0.0)
numFn.setKeyable(True)
numFn.setAffectsWorldSpace(True)
rockingTransformNode.addAttribute(rockingTransformNode.aRockInX)
rockingTransformNode.mustCallValidateAndSet(rockingTransformNode.aRockInX)
return
def initializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.registerTransform( kRockingTransformPluginName, kRockingTransformNodeID, \
nodeCreator, nodeInitializer, matrixCreator, kRockingTransformMatrixID )
except:
sys.stderr.write( "Failed to register transform: %s\n" % kRockingTransformPluginName )
raise
def uninitializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.deregisterNode( kRockingTransformNodeID )
except:
sys.stderr.write( "Failed to unregister node: %s\n" % kRockingTransformPluginName )
raise