scripted/mathTableControl.py

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