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 ########################################################################
40 # DESCRIPTION:
41 #
42 # Produces the Python command "spBlindDoubleDataCmd" and user defined data
43 # type "spBlindDoubleData".
44 #
45 # This plug-in demonstrates how to create blind data (dynamic attributes) based
46 # on user defined data types. The plug-in uses a simple double value as the user
47 # data type. The use of the MPlug class to set and retrieve the value of the
48 # attribute is demonstrated, as are read and write routines that implement
49 # the storage and retrieval of the data in both Maya ASCII and Maya Binary file
50 # formats.
51 #
52 # To use this plug-in, select a dependency node, and then issue the command:
53 #
54 # maya.cmds.spBlindDoubleData()
55 #
56 # A dynamic attribute containing the double value 3.2 will be attached to each
57 # selected dependency node. If the scene is saved in Maya ASCII format, you will
58 # be able to see the Python commands that save the value of the dynamic attribute.
59 # If the scene is reloaded, the dynamic attribute will be reattached to the
60 # applicable nodes.
61 #
62 ########################################################################
63 
64 # import maya.cmds
65 # maya.cmds.loadPlugin("blindDoubleDataCmd.py")
66 # maya.cmds.sphere()
67 # maya.cmds.spBlindDoubleData()
68 
69 import sys
70 import maya.OpenMaya as OpenMaya
71 import maya.OpenMayaMPx as OpenMayaMPx
72 
73 kPluginName = "spBlindDoubleData"
74 kPluginDataId = OpenMaya.MTypeId(0x87011)
75 
76 #
77 fValueDictionary={}
78 
79 # testing function
80 def printMsg(msg):
81  print msg
82  stream=OpenMaya.MStreamUtils.stdOutStream()
83  OpenMaya.MStreamUtils.writeCharBuffer(stream,msg)
84 
85 # data
86 class blindDoubleData(OpenMayaMPx.MPxData):
87  def __init__(self):
88  OpenMayaMPx.MPxData.__init__(self)
89  self.__fValue = 0.0
90  fValueDictionary[OpenMayaMPx.asHashable(self)]=self.__fValue
91 
92  def readASCII(self, args, lastParsedElement):
93  try:
94  if args.length() > 0:
95  parsedIndex = OpenMaya.MScriptUtil.getUint(lastParsedElement)
96  self.__fValue = args.asDouble( parsedIndex )
97  parsedIndex += 1
98  OpenMaya.MScriptUtil.setUint(lastParsedElement,parsedIndex)
99  fValueDictionary[OpenMayaMPx.asHashable(self)]=self.__fValue
100  except:
101  sys.stderr.write("Failed to read ASCII value.")
102  raise
103 
104  def readBinary(self, inStream, length):
105  readParam = OpenMaya.MScriptUtil(0.0)
106  readPtr = readParam.asDoublePtr()
107  OpenMaya.MStreamUtils.readDouble(inStream, readPtr, True )
108  self.__fValue = readParam.getDouble(readPtr)
109 
110  def writeASCII(self, out):
111  try:
112  OpenMaya.MStreamUtils.writeDouble(out, self.__fValue, False)
113  except:
114  sys.stderr.write("Failed to write ASCII value.")
115  raise
116 
117  def writeBinary(self, out):
118  try:
119  OpenMaya.MStreamUtils.writeDouble(out, self.__fValue, True)
120  except:
121  sys.stderr.write("Failed to write binary value.")
122  raise
123 
124  def copy(self, other):
125  # Cannot convert other to self. Use a dictionary
126  # to hold the information we need.
127  self.__fValue = fValueDictionary[OpenMayaMPx.asHashable(other)]
128 
129  def typeId(self):
130  return kPluginDataId
131 
132  def name(self):
133  return kPluginName
134 
135  def value(self):
136  return self.__fValue
137 
138  def setValue(self, newVal):
139  self.__fValue = newVal
140 
141 # command
142 class blindDoubleDataCmd(OpenMayaMPx.MPxCommand):
143  def __init__(self):
144  OpenMayaMPx.MPxCommand.__init__(self)
145  self.__iter = None
146 
147  def doIt(self, args):
148  selList = OpenMaya.MSelectionList()
150  self.__iter = OpenMaya.MItSelectionList(selList)
151  self.redoIt()
152 
153  def redoIt(self):
154  dependNode = OpenMaya.MObject() # Selected dependency node
155  # show message and advance iterator
156  def error(msg):
157  sys.stderr.write(err)
158  self.__iter.next()
159 
160  # Iterate over all selected dependency nodes
161  #
162  while not self.__iter.isDone():
163  # Get the selected dependency node and create
164  # a function set for it
165  #
166  try:
167  self.__iter.getDependNode(dependNode)
168  except:
169  error("Error getting the dependency node")
170  continue
171 
172  try:
173  fnDN = OpenMaya.MFnDependencyNode(dependNode)
174  except:
175  error("Error creating MFnDependencyNode")
176  continue
177 
178  # Create a new attribute for our blind data
179  #
180  fnAttr = OpenMaya.MFnTypedAttribute()
181  newAttr = fnAttr.create("blindDoubleData", "BDD", kPluginDataId)
182 
183  # Now add the new attribute to the current dependency node
184  #
185  fnDN.addAttribute(newAttr, OpenMaya.MFnDependencyNode.kLocalDynamicAttr)
186 
187  # Create a plug to set and retrive value off the node.
188  #
189  plug = OpenMaya.MPlug(dependNode, newAttr)
190 
191  # Instantiate blindDoubleData and set its value.
192  #
193  newData = OpenMayaMPx.asMPxPtr(blindDoubleData())
194  newData.setValue(3.2)
195 
196  # Set the value for the plug.
197  #
198  plug.setMPxData(newData)
199 
200  # Now try to retrieve the value off the plug as an MObject.
201  #
202  try:
203  sData = plug.asMObject()
204  except:
205  error("Error getting value off plug")
206  continue
207 
208  # Convert the data back to MPxData.
209  #
210  pdFn = OpenMaya.MFnPluginData(sData)
211  data = pdFn.constData()
212 
213  # Get the value.
214  #
215  if not data:
216  error("Error: failed to retrieve data.")
217  continue
218 
219  self.__iter.next()
220 
221  def undoIt(self):
222  pass
223 
224  def isUndoable(self):
225  return True
226 
227 
228 ########################################################################
229 
230 
231 # Creators
232 def cmdCreator():
233  return OpenMayaMPx.asMPxPtr(blindDoubleDataCmd())
234 
235 def dataCreator():
236  return OpenMayaMPx.asMPxPtr(blindDoubleData())
237 
238 # Initialize the script plug-in
239 def initializePlugin(mobject):
240  mplugin = OpenMayaMPx.MFnPlugin(mobject, "Autodesk", "1.0", "Any")
241  try:
242  mplugin.registerData(kPluginName, kPluginDataId, dataCreator)
243  except:
244  sys.stderr.write("Failed to register new data type: %s\n" % kPluginName)
245  raise
246 
247  try:
248  mplugin.registerCommand(kPluginName, cmdCreator)
249  except:
250  sys.stderr.write("Failed to register command: %s\n" % kPluginName)
251  raise
252 
253 
254 # Uninitialize the script plug-in
255 def uninitializePlugin(mobject):
256  mplugin = OpenMayaMPx.MFnPlugin(mobject)
257  try:
258  mplugin.deregisterCommand(kPluginName)
259  except:
260  sys.stderr.write("Failed to unregister command: %s\n" % kPluginName)
261  raise
262 
263  try:
264  mplugin.deregisterData(kPluginDataId)
265  except:
266  sys.stderr.write("Failed to unregister data type: %s\n" % kPluginName)
267  raise
268