Python API 2.0 Reference
python/api1/mathTableControl.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 "spMathTableControl".
43 #
44 # This plug-in creates a simple spreadsheet table in a separate output window.
45 # You can perform calculations within this table such as addition and multiplication.
46 #
47 # To create a table with rows and columns, execute the following commands:
48 #
49 # import maya.cmds as cmds
50 # window = cmds.window(title="Math Table",widthHeight=(400,150))
51 # cmds.paneLayout()
52 # cmds.showWindow()
53 # cmds.spMathTableControl()
54 #
55 # A new window named "Math Table" appears with three columns and five rows.
56 #
57 ########################################################################
58 
59 #
60 # Creation Date: 13 October 2006
61 #
62 # Author: mlausch
63 #
64 
65 import math, sys
66 
67 import maya.OpenMaya as OpenMaya
68 import maya.OpenMayaMPx as OpenMayaMPx
69 import maya.OpenMayaUI as OpenMayaUI
70 
71 kPluginCmdName = "spMathTableControl"
72 
73 kNopFlag = "-nop"
74 kNopLongFlag = "-noOperation"
75 kMultFlag = "-mul"
76 kMultLongFlag = "-multiplyVals"
77 kAddFlag = "-add"
78 kAddLongFlag = "-addVals"
79 kRedrawFlag = "-rd"
80 kRedrawFlagLong = "-redraw"
81 
82 
83 # operations on cell coordinates, must return strings
84 kNop = lambda x,y: "cell(%d,%d)" % (x,y)
85 kMult = lambda x,y: str(x*y)
86 kAdd = lambda x,y: str(x+y)
87 
88 kPythonPtrTable = {}
89 
90 class MathTableControlCmd(OpenMayaMPx.MPxControlCommand):
91  def __init__(self):
92  OpenMayaMPx.MPxControlCommand.__init__(self)
93 
94 
95  def makeControl(self):
96  control = MathTableControl(self)
97  control.setOperation(kNop)
98  control.setNumberOfRows(5)
99  control.setNumberOfColumns(3)
100  return OpenMayaMPx.asMPxPtr(control)
101 
102 
103  def doEditFlags(self):
104  theParser = self._parser()
105  theControl = kPythonPtrTable.get(OpenMayaMPx.asHashable(self._control()), None)
106 
107  if theParser.isFlagSet(kNopFlag):
108  theControl.setOperation(kNop)
109  elif theParser.isFlagSet(kMultFlag):
110  theControl.setOperation(kMult)
111  elif theParser.isFlagSet(kAddFlag):
112  theControl.setOperation(kAdd)
113  elif theParser.isFlagSet(kRedrawFlag):
114  theControl.redrawCells()
115  theControl.redrawLabels()
116  else:
117  OpenMayaMPx.MPxControlCommand.doEditFlags(self)
118 
119 
120  def doQueryFlags(self):
121  return OpenMayaMPx.MPxControlCommand.doQueryFlags(self)
122 
123 
124  def appendSyntax(self):
125  theSyntax = self._syntax()
126  theSyntax.addFlag(kNopFlag, kNopLongFlag)
127  theSyntax.addFlag(kMultFlag, kMultLongFlag)
128  theSyntax.addFlag(kAddFlag, kAddLongFlag)
129  theSyntax.addFlag(kRedrawFlag, kRedrawFlagLong)
130 
131 
132 class MathTableControl(OpenMayaMPx.MPxUITableControl):
133  def __init__(self, command):
134  OpenMayaMPx.MPxUITableControl.__init__(self, command)
135  self.__myOperation = None
136  kPythonPtrTable[OpenMayaMPx.asHashable(self)] = self
137 
138 
139  def __del__(self):
140  del kPythonPtrTable[OpenMayaMPx.asHashable(self)]
141 
142 
143  def cellString(self, row, column, isValidCell):
144  result = ""
145  if callable(self.__myOperation):
146  result = self.__myOperation(row, column)
147  OpenMaya.MScriptUtil.setBool(isValidCell, bool(result))
148  return result
149 
150 
151  def labelString(self, labelType, index):
152  value = ""
153  if labelType == OpenMayaMPx.MPxUITableControl.kRowLabel:
154  value = "[Row %d]" % index
155  elif labelType == OpenMayaMPx.MPxUITableControl.kColumnLabel:
156  value = "[Col %d]" % index
157  return value
158 
159 
160  def setOperation(self, op):
161  self.__myOperation = op
162  self.redrawCells()
163 
164 
165 ################################################################
166 
167 
168 def cmdCreator():
169  return OpenMayaMPx.asMPxPtr(MathTableControlCmd())
170 
171 
172 def initializePlugin(mobject):
173  mplugin = OpenMayaMPx.MFnPlugin(mobject, "Autodesk", "1.0", "Any")
174  try:
175  mplugin.registerControlCommand(kPluginCmdName, cmdCreator)
176  except:
177  sys.stderr.write( "Failed to register command: %s\n" % kPluginCmdName)
178  raise
179 
180 
181 def uninitializePlugin(mobject):
182  mplugin = OpenMayaMPx.MFnPlugin(mobject)
183  try:
184  mplugin.deregisterControlCommand(kPluginCmdName)
185  except:
186  sys.stderr.write("Failed to unregister command: %s\n" % kPluginCmdName)
187  raise