def setRotate_keepJointOrient(mtx, jnt): jntP = cmds.listRelatives(jnt, p=1)[0] joValue = cmds.getAttr(jnt + '.jo')[0] joMEuler = om.MEulerRotation(math.radians(joValue[0]), math.radians(joValue[1]), math.radians(joValue[2])) joTransform = mpx.MPxTransformationMatrix(om.MMatrix()) joTransform.rotateBy(joMEuler) jo_im = joTransform.asMatrixInverse() jntP_wim_list = cmds.getAttr(jntP + '.wim') jntP_wim = om.MMatrix() om.MScriptUtil.createMatrixFromList(jntP_wim_list, jntP_wim) cuMtx = mtx * jntP_wim * jo_im transform = mpx.MPxTransformationMatrix(cuMtx) rot = transform.eulerRotation().asVector() degrees = [math.degrees(rot.x), math.degrees(rot.y), math.degrees(rot.z)] for i in range(len(degrees)): if degrees[i] > 180: degrees[i] = degrees[i] - 360 elif degrees[i] < -180: degrees[i] = degrees[i] + 360 cmds.setAttr(jnt + '.r', *degrees)
def setFlyControlPoint(self, target, *args): ns = self.getNamespace(target) rootCtl = ns + 'Root_CTL' flyCtl = ns + 'Fly_CTL' flyCtlMtxList = cmds.getAttr(flyCtl + '.wm') rootCtlMtxList = cmds.getAttr(rootCtl + '.wm') flyCtlMtx = om.MMatrix() rootCtlMtx = om.MMatrix() om.MScriptUtil.createMatrixFromList(flyCtlMtxList, flyCtlMtx) om.MScriptUtil.createMatrixFromList(rootCtlMtxList, rootCtlMtx) localMtx = rootCtlMtx * flyCtlMtx.inverse() mpxTrans = mpx.MPxTransformationMatrix(localMtx) trans = mpxTrans.translation() rotate = mpxTrans.eulerRotation().asVector() cmds.setAttr(flyCtl + '.pivTx', trans.x) cmds.setAttr(flyCtl + '.pivTy', trans.y) cmds.setAttr(flyCtl + '.pivTz', trans.z) cmds.setAttr(flyCtl + '.pivRx', math.degrees(rotate.x)) cmds.setAttr(flyCtl + '.pivRy', math.degrees(rotate.y)) cmds.setAttr(flyCtl + '.pivRz', math.degrees(rotate.z)) cmds.setAttr(flyCtl + '.blend', 1)
def initializePlugin(obj): plugin = OpenMayaMPx.MFnPlugin(obj, 'David Zuber', '1.0', 'Any') matrixcreator = OpenMayaMPx.MPxTransformationMatrix.creator matrixid = OpenMayaMPx.MPxTransformationMatrix().baseTransformationMatrixId try: plugin.registerTransform(JB_AssetNode.kNodeName, JB_AssetNode.kPluginNodeId, JB_AssetNode.creator, JB_AssetNode.initialize, matrixcreator, matrixid) except: raise PluginInitError('Failed to register %s node' % JB_AssetNode.kNodeName)
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()
def setRotMMatrix(target, MMatrix, **options): os = True # objectSpace ws = False # worldSpace items = options.items() for item in items: if item[0] in ['os', 'objectSpace']: os = item[1] ws = not item[1] elif item[0] in ['ws', 'worldSpace']: ws = item[1] os = not item[1] mpxMatrix = mpx.MPxTransformationMatrix(MMatrix) rot = mpxMatrix.eulerRotation().asVector() cmds.xform( target, os=os, ws=ws, ro=[math.degrees(rot.x), math.degrees(rot.y), math.degrees(rot.z)])
def getRotateFromMatrix(mtx): transform = mpx.MPxTransformationMatrix(mtx) r = transform.eulerRotation().asVector() return math.degrees(r.x), math.degrees(r.y), math.degrees(r.z)