Python API 2.0 Reference
python/api2/py2BlindDoubleDataCmd.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 = "py2BlindDoubleData"
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 = "py2BlindDoubleData"
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  try:
116  fnDN.findPlug(blindDoubleDataCmd.s_name, True)
117  # already have the attribute
118  continue
119  except:
120  pass
121 
122  # Create a new attribute for our blind data
123  #
124  fnAttr = om.MFnTypedAttribute()
125  briefName = "BDD"
126  newAttr = fnAttr.create( blindDoubleDataCmd.s_name, briefName, blindDoubleData.s_id )
127 
128  # Now add the new attribute to the current dependency node
129  #
130  fnDN.addAttribute( newAttr )
131 
132  # Create a plug to set and retrive value off the node.
133  #
134  plug = om.MPlug( dependNode, newAttr )
135 
136  # Instantiate blindDoubleData and set its value.
137  #
138  newData = blindDoubleData()
139  newData.setValue( 3.2 )
140 
141  # Set the value for the plug.
142  #
143  plug.setMPxData( newData )
144 
145  # Now try to retrieve the value off the plug as an MObject.
146  #
147  sData = plug.asMObject()
148 
149  # Convert the data back to MPxData.
150  #
151  pdFn = om.MFnPluginData( sData )
152  data = pdFn.data()
153  assert(isinstance(data, blindDoubleData))
154 
155  def undoIt(self):
156  return
157 
158  def isUndoable(self):
159  return True
160 
161 ##############################################################################
162 ##
163 ## The following routines are used to register/unregister
164 ## the command we are creating within Maya
165 ##
166 ##############################################################################
167 def initializePlugin(obj):
168  plugin = om.MFnPlugin(obj, "Autodesk", "3.0", "Any")
169  try:
170  plugin.registerData(blindDoubleData.s_name, blindDoubleData.s_id, blindDoubleData.creator)
171  except:
172  sys.stderr.write("Failed to register data\n")
173  raise
174 
175  try:
176  plugin.registerCommand(blindDoubleDataCmd.s_name, blindDoubleDataCmd.creator)
177  except:
178  sys.stderr.write("Failed to register command\n")
179  raise
180 
181 def uninitializePlugin(obj):
182  plugin = om.MFnPlugin(obj)
183  try:
184  plugin.deregisterCommand(blindDoubleDataCmd.s_name)
185  except:
186  sys.stderr.write("Failed to deregister command\n")
187  raise
188 
189  try:
190  plugin.deregisterData(blindDoubleData.s_id)
191  except:
192  sys.stderr.write("Failed to deregister data\n")
193  raise
194