Python API 2.0 Reference
python/api1/py1BlindDoubleDataCmd.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 from builtins import next
70 import sys
71 import maya.OpenMaya as OpenMaya
72 import maya.OpenMayaMPx as OpenMayaMPx
73 
74 kPluginName = "spBlindDoubleData"
75 kPluginDataId = OpenMaya.MTypeId(0x0008006D)
76 
77 #
78 fValueDictionary={}
79 
80 # testing function
81 def printMsg(msg):
82  print(msg)
83  stream=OpenMaya.MStreamUtils.stdOutStream()
84  OpenMaya.MStreamUtils.writeCharBuffer(stream,msg)
85 
86 # data
87 class blindDoubleData(OpenMayaMPx.MPxData):
88  def __init__(self):
89  OpenMayaMPx.MPxData.__init__(self)
90  self.__fValue = 0.0
91  fValueDictionary[OpenMayaMPx.asHashable(self)]=self.__fValue
92 
93  def readASCII(self, args, lastParsedElement):
94  try:
95  if args.length() > 0:
96  parsedIndex = OpenMaya.MScriptUtil.getUint(lastParsedElement)
97  self.__fValue = args.asDouble( parsedIndex )
98  parsedIndex += 1
99  OpenMaya.MScriptUtil.setUint(lastParsedElement,parsedIndex)
100  fValueDictionary[OpenMayaMPx.asHashable(self)]=self.__fValue
101  except:
102  sys.stderr.write("Failed to read ASCII value.")
103  raise
104 
105  def readBinary(self, inStream, length):
106  readParam = OpenMaya.MScriptUtil(0.0)
107  readPtr = readParam.asDoublePtr()
108  OpenMaya.MStreamUtils.readDouble(inStream, readPtr, True )
109  self.__fValue = readParam.getDouble(readPtr)
110 
111  def writeASCII(self, out):
112  try:
113  OpenMaya.MStreamUtils.writeDouble(out, self.__fValue, False)
114  except:
115  sys.stderr.write("Failed to write ASCII value.")
116  raise
117 
118  def writeBinary(self, out):
119  try:
120  OpenMaya.MStreamUtils.writeDouble(out, self.__fValue, True)
121  except:
122  sys.stderr.write("Failed to write binary value.")
123  raise
124 
125  def copy(self, other):
126  # Cannot convert other to self. Use a dictionary
127  # to hold the information we need.
128  self.__fValue = fValueDictionary[OpenMayaMPx.asHashable(other)]
129 
130  def typeId(self):
131  return kPluginDataId
132 
133  def name(self):
134  return kPluginName
135 
136  def value(self):
137  return self.__fValue
138 
139  def setValue(self, newVal):
140  self.__fValue = newVal
141 
142 # command
143 class blindDoubleDataCmd(OpenMayaMPx.MPxCommand):
144  def __init__(self):
145  OpenMayaMPx.MPxCommand.__init__(self)
146  self.__iter = None
147 
148  def doIt(self, args):
149  selList = OpenMaya.MSelectionList()
151  self.__iter = OpenMaya.MItSelectionList(selList)
152  self.redoIt()
153 
154  def redoIt(self):
155  dependNode = OpenMaya.MObject() # Selected dependency node
156  # show message and advance iterator
157  def error(msg):
158  sys.stderr.write(err)
159  next(self.__iter)
160 
161  # Iterate over all selected dependency nodes
162  #
163  while not self.__iter.isDone():
164  # Get the selected dependency node and create
165  # a function set for it
166  #
167  try:
168  self.__iter.getDependNode(dependNode)
169  except:
170  error("Error getting the dependency node")
171  continue
172 
173  try:
174  fnDN = OpenMaya.MFnDependencyNode(dependNode)
175  except:
176  error("Error creating MFnDependencyNode")
177  continue
178 
179  # Create a new attribute for our blind data
180  #
181  fnAttr = OpenMaya.MFnTypedAttribute()
182  newAttr = fnAttr.create("blindDoubleData", "BDD", kPluginDataId)
183 
184  # Now add the new attribute to the current dependency node
185  #
186  fnDN.addAttribute(newAttr, OpenMaya.MFnDependencyNode.kLocalDynamicAttr)
187 
188  # Create a plug to set and retrive value off the node.
189  #
190  plug = OpenMaya.MPlug(dependNode, newAttr)
191 
192  # Instantiate blindDoubleData and set its value.
193  #
194  newData = OpenMayaMPx.asMPxPtr(blindDoubleData())
195  newData.setValue(3.2)
196 
197  # Set the value for the plug.
198  #
199  plug.setMPxData(newData)
200 
201  # Now try to retrieve the value off the plug as an MObject.
202  #
203  try:
204  sData = plug.asMObject()
205  except:
206  error("Error getting value off plug")
207  continue
208 
209  # Convert the data back to MPxData.
210  #
211  pdFn = OpenMaya.MFnPluginData(sData)
212  data = pdFn.constData()
213 
214  # Get the value.
215  #
216  if not data:
217  error("Error: failed to retrieve data.")
218  continue
219 
220  next(self.__iter)
221 
222  def undoIt(self):
223  pass
224 
225  def isUndoable(self):
226  return True
227 
228 
229 ########################################################################
230 
231 
232 # Creators
233 def cmdCreator():
234  return OpenMayaMPx.asMPxPtr(blindDoubleDataCmd())
235 
236 def dataCreator():
237  return OpenMayaMPx.asMPxPtr(blindDoubleData())
238 
239 # Initialize the script plug-in
240 def initializePlugin(mobject):
241  mplugin = OpenMayaMPx.MFnPlugin(mobject, "Autodesk", "1.0", "Any")
242  try:
243  mplugin.registerData(kPluginName, kPluginDataId, dataCreator)
244  except:
245  sys.stderr.write("Failed to register new data type: %s\n" % kPluginName)
246  raise
247 
248  try:
249  mplugin.registerCommand(kPluginName, cmdCreator)
250  except:
251  sys.stderr.write("Failed to register command: %s\n" % kPluginName)
252  raise
253 
254 
255 # Uninitialize the script plug-in
256 def uninitializePlugin(mobject):
257  mplugin = OpenMayaMPx.MFnPlugin(mobject)
258  try:
259  mplugin.deregisterCommand(kPluginName)
260  except:
261  sys.stderr.write("Failed to unregister command: %s\n" % kPluginName)
262  raise
263 
264  try:
265  mplugin.deregisterData(kPluginDataId)
266  except:
267  sys.stderr.write("Failed to unregister data type: %s\n" % kPluginName)
268  raise
269