Python API 2.0 Reference: 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 from builtins import next
13 import sys
14 import io
15 import pickle
16 import maya.api.OpenMaya as om
17 
18 def maya_useNewAPI():
19  """
20  The presence of this function tells Maya that the plugin produces, and
21  expects to be passed, objects created using the Maya Python API 2.0.
22  """
23  pass
24 
25 
26 ##############################################################################
27 ##
28 ## Proxy data class implementation
29 ##
30 ##############################################################################
31 class blindDoubleData(om.MPxData):
32  s_id = om.MTypeId( 0x80058 )
33  s_name = "blindDoubleData"
34  fValue = 0
35 
36  def __init__(self):
37  om.MPxData.__init__(self)
38 
39  @staticmethod
40  def creator():
41  return blindDoubleData()
42 
43  def readASCII(self, args, lastParsedElement):
44  if len(args) > 0:
45  self.fValue = args.asDouble(lastParsedElement)
46  lastParsedElement = lastParsedElement+1
47  return lastParsedElement
48 
49  def readBinary(self, istream, length):
50  rawData = io.BytesIO(istream)
51  reader = pickle.Unpickler(rawData)
52 
53  self.fValue = reader.load()
54 
55  return rawData.tell()
56 
57  def writeASCII(self):
58  data = str(self.fValue)
59  data += " "
60 
61  return data
62 
63  def writeBinary(self):
64  rawData = io.BytesIO()
65  writer = pickle.Pickler(rawData)
66 
67  writer.dump( self.fValue )
68  return bytearray(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  next(self.iter)
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