blindDoubleDataCmd.py
import sys
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
kPluginName = "spBlindDoubleData"
kPluginDataId = OpenMaya.MTypeId(0x87011)
fValueDictionary={}
def printMsg(msg):
print msg
stream=OpenMaya.MStreamUtils.stdOutStream()
OpenMaya.MStreamUtils.writeCharBuffer(stream,msg)
class blindDoubleData(OpenMayaMPx.MPxData):
def __init__(self):
OpenMayaMPx.MPxData.__init__(self)
self.__fValue = 0.0
fValueDictionary[OpenMayaMPx.asHashable(self)]=self.__fValue
def readASCII(self, args, lastParsedElement):
try:
if args.length() > 0:
parsedIndex = OpenMaya.MScriptUtil.getUint(lastParsedElement)
self.__fValue = args.asDouble( parsedIndex )
parsedIndex += 1
OpenMaya.MScriptUtil.setUint(lastParsedElement,parsedIndex)
fValueDictionary[OpenMayaMPx.asHashable(self)]=self.__fValue
except:
sys.stderr.write("Failed to read ASCII value.")
raise
def readBinary(self, inStream, length):
readParam = OpenMaya.MScriptUtil()
readParam.createFromDouble(0.0)
readPtr = readParam.asDoublePtr()
OpenMaya.MStreamUtils.readDouble(inStream, readPtr, True )
self.__fValue = OpenMaya.MScriptUtil.getDouble( readPtr )
def writeASCII(self, out):
try:
OpenMaya.MStreamUtils.writeDouble(out, self.__fValue, False)
except:
sys.stderr.write("Failed to write ASCII value.")
raise
def writeBinary(self, out):
try:
OpenMaya.MStreamUtils.writeDouble(out, self.__fValue, True)
except:
sys.stderr.write("Failed to write binary value.")
raise
def copy(self, other):
self.__fValue = fValueDictionary[OpenMayaMPx.asHashable(other)]
def typeId(self):
return kPluginDataId
def name(self):
return kPluginName
def value(self):
return self.__fValue
def setValue(self, newVal):
self.__fValue = newVal
class blindDoubleDataCmd(OpenMayaMPx.MPxCommand):
def __init__(self):
OpenMayaMPx.MPxCommand.__init__(self)
self.__iter = None
def doIt(self, args):
selList = OpenMaya.MSelectionList()
OpenMaya.MGlobal.getActiveSelectionList(selList)
self.__iter = OpenMaya.MItSelectionList(selList)
self.redoIt()
def redoIt(self):
dependNode = OpenMaya.MObject()
def error(msg):
sys.stderr.write(err)
self.__iter.next()
while not self.__iter.isDone():
try:
self.__iter.getDependNode(dependNode)
except:
error("Error getting the dependency node")
continue
try:
fnDN = OpenMaya.MFnDependencyNode(dependNode)
except:
error("Error creating MFnDependencyNode")
continue
fnAttr = OpenMaya.MFnTypedAttribute()
newAttr = fnAttr.create("blindDoubleData", "BDD", kPluginDataId)
fnDN.addAttribute(newAttr, OpenMaya.MFnDependencyNode.kLocalDynamicAttr)
plug = OpenMaya.MPlug(dependNode, newAttr)
newData = OpenMayaMPx.asMPxPtr(blindDoubleData())
newData.setValue(3.2)
plug.setMPxData(newData)
try:
sData = plug.asMObject()
except:
error("Error getting value off plug")
continue
pdFn = OpenMaya.MFnPluginData(sData)
data = pdFn.constData()
if not data:
error("Error: failed to retrieve data.")
continue
self.__iter.next()
def undoIt(self):
pass
def isUndoable(self):
return True
def cmdCreator():
return OpenMayaMPx.asMPxPtr(blindDoubleDataCmd())
def dataCreator():
return OpenMayaMPx.asMPxPtr(blindDoubleData())
def initializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject, "Autodesk", "1.0", "Any")
try:
mplugin.registerData(kPluginName, kPluginDataId, dataCreator)
except:
sys.stderr.write("Failed to register new data type: %s\n" % kPluginName)
raise
try:
mplugin.registerCommand(kPluginName, cmdCreator)
except:
sys.stderr.write("Failed to register command: %s\n" % kPluginName)
raise
def uninitializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.deregisterCommand(kPluginName)
except:
sys.stderr.write("Failed to unregister command: %s\n" % kPluginName)
raise
try:
mplugin.deregisterData(kPluginDataId)
except:
sys.stderr.write("Failed to unregister data type: %s\n" % kPluginName)
raise