scripted/pyBlindDoubleDataCmd.py

scripted/pyBlindDoubleDataCmd.py
1 #-
2 # ==========================================================================
3 # Copyright 2015 Autodesk, Inc. All rights reserved.
4 #
5 # Use of this software is subject to the terms of the Autodesk
6 # license agreement provided at the time of installation or download,
7 # or which otherwise accompanies this software in either electronic
8 # or hard copy form.
9 # ==========================================================================
10 #+
11 
12 import sys
13 import io
14 import pickle
15 import maya.api.OpenMaya as om
16 
17 def maya_useNewAPI():
18  """
19  The presence of this function tells Maya that the plugin produces, and
20  expects to be passed, objects created using the Maya Python API 2.0.
21  """
22  pass
23 
24 
25 ##############################################################################
26 ##
27 ## Proxy data class implementation
28 ##
29 ##############################################################################
30 class blindDoubleData(om.MPxData):
31  s_id = om.MTypeId( 0x80003 )
32  s_name = "blindDoubleData"
33  fValue = 0
34 
35  def __init__(self):
36  om.MPxData.__init__(self)
37 
38  @staticmethod
39  def creator():
40  return blindDoubleData()
41 
42  def readASCII(self, args, lastParsedElement):
43  if len(args) > 0:
44  self.fValue = args.asDouble(lastParsedElement)
45  lastParsedElement = lastParsedElement+1
46  return lastParsedElement
47 
48  def readBinary(self, istream, length):
49  rawData = io.BytesIO(istream)
50  reader = pickle.Unpickler(rawData)
51 
52  self.fValue = reader.load()
53 
54  return rawData.tell()
55 
56  def writeASCII(self, ostream):
57  data = str(self.fValue)
58  data += " "
59 
60  ostream[:] = bytearray(data, "ascii")
61 
62  def writeBinary(self, ostream):
63  rawData = io.BytesIO()
64  writer = pickle.Pickler(rawData)
65 
66  writer.dump( self.fValue )
67 
68  ostream[:] = rawData.getvalue()
69 
70  def copy(self, other):
71  self.fValue = other.fValue
72 
73  def typeId(self):
74  return blindDoubleData.s_id
75 
76  def name(self):
77  return blindDoubleData.s_name
78 
79  def setValue(self, newValue):
80  self.fValue = newValue
81 
82 ##############################################################################
83 ##
84 ## Command class implementation
85 ##
86 ##############################################################################
87 class blindDoubleDataCmd(om.MPxCommand):
88  s_name = "blindDoubleData"
89  iter = None
90 
91  def __init__(self):
92  om.MPxCommand.__init__(self)
93 
94  @staticmethod
95  def creator():
96  return blindDoubleDataCmd()
97 
98  def doIt(self, args):
99  sList = om.MGlobal.getActiveSelectionList()
100  self.iter = om.MItSelectionList(sList, om.MFn.kInvalid)
101  self.redoIt()
102 
103  def redoIt(self):
104  # Iterate over all selected dependency nodes
105  #
106  while not self.iter.isDone():
107  # Get the selected dependency node and create
108  # a function set for it
109  #
110  dependNode = self.iter.getDependNode()
111  self.iter.next()
112 
113  fnDN = om.MFnDependencyNode(dependNode)
114 
115  fullName = "blindDoubleData"
116  try:
117  fnDN.findPlug(fullName, True)
118  # already have the attribute
119  continue
120  except:
121  pass
122 
123  # Create a new attribute for our blind data
124  #
125  fnAttr = om.MFnTypedAttribute()
126  briefName = "BDD"
127  newAttr = fnAttr.create( fullName, briefName, blindDoubleData.s_id )
128 
129  # Now add the new attribute to the current dependency node
130  #
131  fnDN.addAttribute( newAttr )
132 
133  # Create a plug to set and retrive value off the node.
134  #
135  plug = om.MPlug( dependNode, newAttr )
136 
137  # Instantiate blindDoubleData and set its value.
138  #
139  newData = blindDoubleData()
140  newData.setValue( 3.2 )
141 
142  # Set the value for the plug.
143  #
144  plug.setMPxData( newData )
145 
146  # Now try to retrieve the value off the plug as an MObject.
147  #
148  sData = plug.asMObject()
149 
150  # Convert the data back to MPxData.
151  #
152  pdFn = om.MFnPluginData( sData )
153  data = pdFn.data()
154  assert(isinstance(data, blindDoubleData))
155 
156  def undoIt(self):
157  return
158 
159  def isUndoable(self):
160  return True
161 
162 ##############################################################################
163 ##
164 ## The following routines are used to register/unregister
165 ## the command we are creating within Maya
166 ##
167 ##############################################################################
168 def initializePlugin(obj):
169  plugin = om.MFnPlugin(obj, "Autodesk", "3.0", "Any")
170  try:
171  plugin.registerData(blindDoubleData.s_name, blindDoubleData.s_id, blindDoubleData.creator)
172  except:
173  sys.stderr.write("Failed to register data\n")
174  raise
175 
176  try:
177  plugin.registerCommand(blindDoubleDataCmd.s_name, blindDoubleDataCmd.creator)
178  except:
179  sys.stderr.write("Failed to register command\n")
180  raise
181 
182 def uninitializePlugin(obj):
183  plugin = om.MFnPlugin(obj)
184  try:
185  plugin.deregisterCommand(blindDoubleDataCmd.s_name)
186  except:
187  sys.stderr.write("Failed to deregister command\n")
188  raise
189 
190  try:
191  plugin.deregisterData(blindDoubleData.s_id)
192  except:
193  sys.stderr.write("Failed to deregister data\n")
194  raise
195