34 import maya.OpenMaya
as OpenMaya
35 import maya.OpenMayaMPx
as OpenMayaMPx
39 kRockingTransformPluginName =
"spRockingTransform"
40 kRockingTransformNodeName =
"spRockingTransformNode"
47 kTrackingDictionary = {}
50 class rockingTransformMatrix(OpenMayaMPx.MPxTransformationMatrix):
53 OpenMayaMPx.MPxTransformationMatrix.__init__(self)
54 kTrackingDictionary[OpenMayaMPx.asHashable(self)] = self
58 del kTrackingDictionary[OpenMayaMPx.asHashable(self)]
60 def setRockInX(self,rockingValue):
61 self.rockXValue = rockingValue
64 return self.rockXValue
66 def asMatrix(self,percent=None):
68 Find the new matrix and return it
71 matrix = OpenMayaMPx.MPxTransformationMatrix.asMatrix(self)
73 quat = self.rotation()
74 rockingValue = self.getRockInX()
75 newTheta = math.radians( rockingValue )
76 quat.setToXAxis( newTheta )
77 tm.addRotationQuaternion( quat.x, quat.y, quat.z, quat.w, OpenMaya.MSpace.kTransform )
80 m = OpenMayaMPx.MPxTransformationMatrix(self)
82 trans = m.translation()
83 rotatePivotTrans = m.rotatePivot()
84 scalePivotTrans = m.scalePivotTranslation()
85 trans = trans * percent
86 rotatePivotTrans = rotatePivotTrans * percent
87 scalePivotTrans = scalePivotTrans * percent
89 m.setRotatePivot( rotatePivotTrans )
90 m.setScalePivotTranslation( scalePivotTrans )
92 quat = self.rotation()
93 rockingValue = self.getRockInX()
94 newTheta = math.radians( rockingValue )
96 quat.setToXAxis( newTheta )
98 eulerRotate = m.eulerRotation()
99 m.rotateTo( eulerRotate * percent, OpenMaya.MSpace.kTransform )
101 s = self.scale( OpenMaya.MSpace.kTransform )
102 s.x = 1.0 + ( s.x - 1.0 ) * percent
103 s.y = 1.0 + ( s.y - 1.0 ) * percent
104 s.z = 1.0 + ( s.z - 1.0 ) * percent
105 m.scaleTo( s, OpenMaya.MSpace.kTransform )
110 class rockingTransformNode(OpenMayaMPx.MPxTransform):
114 def __init__(self, transform=None):
115 if transform
is None:
116 OpenMayaMPx.MPxTransform.__init__(self)
118 OpenMayaMPx.MPxTransform.__init__(self, transform)
119 self.rockXValue = 0.0
121 def createTransformationMatrix(self):
122 return OpenMayaMPx.asMPxPtr( rockingTransformMatrix() )
125 return kRockingTransformNodeName
128 def computeLocalTransformation(self, mtx, data):
130 rockInXData = data.inputValue( self.aRockInX )
131 rockInX = rockInXData.asDouble()
136 ltm = kTrackingDictionary[OpenMayaMPx.asHashable(mtx)]
139 ltm.setRockInX(rockInX)
141 except Exception
as ex:
142 print(
'rockingTransform computeLocalTransformation error: %s' % str(ex))
144 return OpenMayaMPx.MPxTransform.computeLocalTransformation(self, mtx, data)
147 def compute(self, plug, data):
150 return OpenMayaMPx.MPxTransform.compute(self, plug, data)
152 if ( (plug == self.matrix)
153 or (plug == self.inverseMatrix)
154 or (plug == self.worldMatrix)
155 or (plug == self.worldInverseMatrix)
156 or (plug == self.parentMatrix)
157 or (plug == self.parentInverseMatrix) ):
160 ltm = self.getRockingTransformationMatrix()
162 self.computeLocalTransformation(ltm, data)
164 except Exception
as ex:
165 print(
'rockingTransform Compute error: %s' % str(ex))
167 return OpenMayaMPx.MPxTransform.compute(self, plug, data)
170 def validateAndSetValue(self, plug, handle):
171 if not plug.isNull():
172 if plug == self.aRockInX:
173 block = self._forceCache()
174 rock_handle = block.outputValue(plug)
177 rockInX = handle.asDouble()
178 rock_handle.setDouble(rockInX)
182 ltm = self.getRockingTransformationMatrix()
184 ltm.setRockInX(rockInX)
186 rock_handle.setClean()
192 OpenMayaMPx.MPxTransform.validateAndSetValue(self, plug, handle)
195 def getRockingTransformationMatrix(self):
196 baseXform = self.transformationMatrixPtr()
197 return kTrackingDictionary[OpenMayaMPx.asHashable(baseXform)]
202 return OpenMayaMPx.asMPxPtr( rockingTransformMatrix() )
205 return OpenMayaMPx.asMPxPtr( rockingTransformNode() )
207 def nodeInitializer():
210 rockingTransformNode.aRockInX = numFn.create(
"RockInX",
"rockx", OpenMaya.MFnNumericData.kDouble, 0.0)
212 numFn.setKeyable(
True)
213 numFn.setAffectsWorldSpace(
True)
215 rockingTransformNode.addAttribute(rockingTransformNode.aRockInX)
216 rockingTransformNode.mustCallValidateAndSet(rockingTransformNode.aRockInX)
220 def initializePlugin(mobject):
221 mplugin = OpenMayaMPx.MFnPlugin(mobject)
224 mplugin.registerTransform( kRockingTransformPluginName, kRockingTransformNodeID, \
225 nodeCreator, nodeInitializer, matrixCreator, kRockingTransformMatrixID )
227 sys.stderr.write(
"Failed to register transform: %s\n" % kRockingTransformPluginName )
231 def uninitializePlugin(mobject):
232 mplugin = OpenMayaMPx.MFnPlugin(mobject)
235 mplugin.deregisterNode( kRockingTransformNodeID )
237 sys.stderr.write(
"Failed to unregister node: %s\n" % kRockingTransformPluginName )