scripted/blindDoubleDataCmd.py

scripted/blindDoubleDataCmd.py
1 #-
2 # ==========================================================================
3 # Copyright (C) 1995 - 2006 Autodesk, Inc. and/or its licensors. All
4 # rights reserved.
5 #
6 # The coded instructions, statements, computer programs, and/or related
7 # material (collectively the "Data") in these files contain unpublished
8 # information proprietary to Autodesk, Inc. ("Autodesk") and/or its
9 # licensors, which is protected by U.S. and Canadian federal copyright
10 # law and by international treaties.
11 #
12 # The Data is provided for use exclusively by You. You have the right
13 # to use, modify, and incorporate this Data into other products for
14 # purposes authorized by the Autodesk software license agreement,
15 # without fee.
16 #
17 # The copyright notices in the Software and this entire statement,
18 # including the above license grant, this restriction and the
19 # following disclaimer, must be included in all copies of the
20 # Software, in whole or in part, and all derivative works of
21 # the Software, unless such copies or derivative works are solely
22 # in the form of machine-executable object code generated by a
23 # source language processor.
24 #
25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
26 # AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
27 # WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
28 # NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
29 # PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR
30 # TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS
31 # BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
32 # DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK
33 # AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY
34 # OR PROBABILITY OF SUCH DAMAGES.
35 #
36 # ==========================================================================
37 #+
38 
39 # import maya.cmds
40 # maya.cmds.loadPlugin("blindDoubleDataCmd.py")
41 # maya.cmds.sphere()
42 # maya.cmds.spBlindDoubleData()
43 
44 import sys
45 import maya.OpenMaya as OpenMaya
46 import maya.OpenMayaMPx as OpenMayaMPx
47 
48 kPluginName = "spBlindDoubleData"
49 kPluginDataId = OpenMaya.MTypeId(0x87011)
50 
51 #
52 fValueDictionary={}
53 
54 # testing function
55 def printMsg(msg):
56  print msg
57  stream=OpenMaya.MStreamUtils.stdOutStream()
58  OpenMaya.MStreamUtils.writeCharBuffer(stream,msg)
59 
60 # data
61 class blindDoubleData(OpenMayaMPx.MPxData):
62  def __init__(self):
63  OpenMayaMPx.MPxData.__init__(self)
64  self.__fValue = 0.0
65  fValueDictionary[OpenMayaMPx.asHashable(self)]=self.__fValue
66 
67  def readASCII(self, args, lastParsedElement):
68  try:
69  if args.length() > 0:
70  parsedIndex = OpenMaya.MScriptUtil.getUint(lastParsedElement)
71  self.__fValue = args.asDouble( parsedIndex )
72  parsedIndex += 1
73  OpenMaya.MScriptUtil.setUint(lastParsedElement,parsedIndex)
74  fValueDictionary[OpenMayaMPx.asHashable(self)]=self.__fValue
75  except:
76  sys.stderr.write("Failed to read ASCII value.")
77  raise
78 
79  def readBinary(self, inStream, length):
80  readParam = OpenMaya.MScriptUtil(0.0)
81  readPtr = readParam.asDoublePtr()
82  OpenMaya.MStreamUtils.readDouble(inStream, readPtr, True )
83  self.__fValue = readParam.getDouble(readPtr)
84 
85  def writeASCII(self, out):
86  try:
87  OpenMaya.MStreamUtils.writeDouble(out, self.__fValue, False)
88  except:
89  sys.stderr.write("Failed to write ASCII value.")
90  raise
91 
92  def writeBinary(self, out):
93  try:
94  OpenMaya.MStreamUtils.writeDouble(out, self.__fValue, True)
95  except:
96  sys.stderr.write("Failed to write binary value.")
97  raise
98 
99  def copy(self, other):
100  # Cannot convert other to self. Use a dictionary
101  # to hold the information we need.
102  self.__fValue = fValueDictionary[OpenMayaMPx.asHashable(other)]
103 
104  def typeId(self):
105  return kPluginDataId
106 
107  def name(self):
108  return kPluginName
109 
110  def value(self):
111  return self.__fValue
112 
113  def setValue(self, newVal):
114  self.__fValue = newVal
115 
116 # command
117 class blindDoubleDataCmd(OpenMayaMPx.MPxCommand):
118  def __init__(self):
119  OpenMayaMPx.MPxCommand.__init__(self)
120  self.__iter = None
121 
122  def doIt(self, args):
123  selList = OpenMaya.MSelectionList()
124  OpenMaya.MGlobal.getActiveSelectionList(selList)
125  self.__iter = OpenMaya.MItSelectionList(selList)
126  self.redoIt()
127 
128  def redoIt(self):
129  dependNode = OpenMaya.MObject() # Selected dependency node
130  # show message and advance iterator
131  def error(msg):
132  sys.stderr.write(err)
133  self.__iter.next()
134 
135  # Iterate over all selected dependency nodes
136  #
137  while not self.__iter.isDone():
138  # Get the selected dependency node and create
139  # a function set for it
140  #
141  try:
142  self.__iter.getDependNode(dependNode)
143  except:
144  error("Error getting the dependency node")
145  continue
146 
147  try:
148  fnDN = OpenMaya.MFnDependencyNode(dependNode)
149  except:
150  error("Error creating MFnDependencyNode")
151  continue
152 
153  # Create a new attribute for our blind data
154  #
155  fnAttr = OpenMaya.MFnTypedAttribute()
156  newAttr = fnAttr.create("blindDoubleData", "BDD", kPluginDataId)
157 
158  # Now add the new attribute to the current dependency node
159  #
160  fnDN.addAttribute(newAttr, OpenMaya.MFnDependencyNode.kLocalDynamicAttr)
161 
162  # Create a plug to set and retrive value off the node.
163  #
164  plug = OpenMaya.MPlug(dependNode, newAttr)
165 
166  # Instantiate blindDoubleData and set its value.
167  #
168  newData = OpenMayaMPx.asMPxPtr(blindDoubleData())
169  newData.setValue(3.2)
170 
171  # Set the value for the plug.
172  #
173  plug.setMPxData(newData)
174 
175  # Now try to retrieve the value off the plug as an MObject.
176  #
177  try:
178  sData = plug.asMObject()
179  except:
180  error("Error getting value off plug")
181  continue
182 
183  # Convert the data back to MPxData.
184  #
185  pdFn = OpenMaya.MFnPluginData(sData)
186  data = pdFn.constData()
187 
188  # Get the value.
189  #
190  if not data:
191  error("Error: failed to retrieve data.")
192  continue
193 
194  self.__iter.next()
195 
196  def undoIt(self):
197  pass
198 
199  def isUndoable(self):
200  return True
201 
202 
203 ########################################################################
204 
205 
206 # Creators
207 def cmdCreator():
208  return OpenMayaMPx.asMPxPtr(blindDoubleDataCmd())
209 
210 def dataCreator():
211  return OpenMayaMPx.asMPxPtr(blindDoubleData())
212 
213 # Initialize the script plug-in
214 def initializePlugin(mobject):
215  mplugin = OpenMayaMPx.MFnPlugin(mobject, "Autodesk", "1.0", "Any")
216  try:
217  mplugin.registerData(kPluginName, kPluginDataId, dataCreator)
218  except:
219  sys.stderr.write("Failed to register new data type: %s\n" % kPluginName)
220  raise
221 
222  try:
223  mplugin.registerCommand(kPluginName, cmdCreator)
224  except:
225  sys.stderr.write("Failed to register command: %s\n" % kPluginName)
226  raise
227 
228 
229 # Uninitialize the script plug-in
230 def uninitializePlugin(mobject):
231  mplugin = OpenMayaMPx.MFnPlugin(mobject)
232  try:
233  mplugin.deregisterCommand(kPluginName)
234  except:
235  sys.stderr.write("Failed to unregister command: %s\n" % kPluginName)
236  raise
237 
238  try:
239  mplugin.deregisterData(kPluginDataId)
240  except:
241  sys.stderr.write("Failed to unregister data type: %s\n" % kPluginName)
242  raise
243