scripted/moveManip.py

scripted/moveManip.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 "spMoveManipCtxCmd" to create the example context.
43 #
44 # To use this plug-in, execute the following:
45 #
46 # import maya
47 # maya.cmds.loadPlugin("moveManip.py")
48 # maya.cmds.spMoveManipCtxCmd( 'spMoveManipContext1' )
49 # maya.cmds.setParent( 'Shelf1' )
50 # maya.cmds.toolButton( 'spMoveManip1', cl='toolCluster', t='spMoveManipContext1', i1="moveManip.xpm" )
51 #
52 # This creates a new entry in the "Shelf1" tab of the tool shelf called "moveManip".
53 # Create a sphere and click on the moveManip icon on the shelf. A free point triad
54 # manipulator will appear when the object is selected.
55 #
56 # Note that you must have a Shelf1 tab before executing the commands.
57 #
58 ########################################################################
59 
60 import sys
61 import maya.OpenMaya as OpenMaya
62 import maya.OpenMayaUI as OpenMayaUI
63 import maya.OpenMayaMPx as OpenMayaMPx
64 
65 moveManipId = OpenMaya.MTypeId(0x87009)
66 contextCmdName = "spMoveManipCtxCmd"
67 nodeName = "spMoveManip"
68 
69 class moveManip(OpenMayaMPx.MPxManipContainer):
70  fDistanceManip = OpenMaya.MDagPath()
71  fFreePointManip = OpenMaya.MDagPath()
72 
73  def __init__(self):
74  OpenMayaMPx.MPxManipContainer.__init__(self)
75 
76  def createChildren(self):
77  self.fDistanceManip = self.addDistanceManip("distanceManip", "distance")
78  distanceManipFn = OpenMayaUI.MFnDistanceManip(self.fDistanceManip)
79  startPoint = OpenMaya.MPoint(0.0, 0.0, 0.0)
80  direction = OpenMaya.MVector(0.0, 1.0, 0.0)
81  distanceManipFn.setStartPoint(startPoint)
82  distanceManipFn.setDirection(direction)
83  self.fFreePointManip = self.addFreePointTriadManip("pointManip", "freePoint")
84 
85  def connectToDependNode(self, node):
86  nodeFn = OpenMaya.MFnDependencyNode(node)
87 
88  try:
89  syPlug = nodeFn.findPlug("scaleY")
90  tPlug = nodeFn.findPlug("translate")
91  distanceManipFn = OpenMayaUI.MFnDistanceManip(self.fDistanceManip)
92  distanceManipFn.connectToDistancePlug(syPlug)
93  freePointManipFn = OpenMayaUI.MFnFreePointTriadManip(self.fFreePointManip)
94  freePointManipFn.connectToPointPlug(tPlug)
95  OpenMayaMPx.MPxManipContainer.finishAddingManips(self)
96  OpenMayaMPx.MPxManipContainer.connectToDependNode(self,node)
97  except:
98  sys.stderr.write( "Error finding and connecting plugs\n" )
99  raise
100 
101 def moveManipCreator():
102  return OpenMayaMPx.asMPxPtr( moveManip() )
103 
104 def moveManipInitialize():
105  OpenMayaMPx.MPxManipContainer.initialize()
106 
107 class moveManipContext(OpenMayaMPx.MPxSelectionContext):
108  def __init__(self):
109  OpenMayaMPx.MPxSelectionContext.__init__(self)
110 
111  def toolOnSetup(self,event):
112  updateManipulators(self)
113  OpenMaya.MModelMessage.addCallback(OpenMaya.MModelMessage.kActiveListModified, updateManipulators, self)
114 
115 def updateManipulators(clientData):
116  clientData.deleteManipulators()
117  selectionList = OpenMaya.MSelectionList()
118 
120  selectionIter = OpenMaya.MItSelectionList(selectionList, OpenMaya.MFn.kInvalid)
121  while not selectionIter.isDone():
122  dependNode = OpenMaya.MObject()
123  selectionIter.getDependNode(dependNode)
124  if dependNode.isNull() or not dependNode.hasFn(OpenMaya.MFn.kDependencyNode):
125  print "depend node is null"
126  continue
127 
128  dependNodeFn = OpenMaya.MFnDependencyNode(dependNode)
129  rPlug = dependNodeFn.findPlug("translate", False)
130  sPlug = dependNodeFn.findPlug("scaleY", False)
131  if rPlug.isNull() or sPlug.isNull():
132  print "translate and/or scale plugs are null"
133  selectionIter.next()
134  continue
135 
136  manipObject = OpenMaya.MObject()
137  manipulator = OpenMayaMPx.MPxManipContainer.newManipulator(nodeName, manipObject)
138  if manipulator is not None:
139  clientData.addManipulator(manipObject)
140  manipulator.connectToDependNode(dependNode)
141  selectionIter.next()
142 
143 class moveManipCtxCmd(OpenMayaMPx.MPxContextCommand):
144  def __init__(self):
145  OpenMayaMPx.MPxContextCommand.__init__(self)
146 
147  def makeObj(self):
148  return OpenMayaMPx.asMPxPtr( moveManipContext() )
149 
150 
151 def contextCmdCreator():
152  return OpenMayaMPx.asMPxPtr( moveManipCtxCmd() )
153 
154 
155 # initialize the script plug-in
156 def initializePlugin(mobject):
157  mplugin = OpenMayaMPx.MFnPlugin(mobject)
158 
159  try:
160  mplugin.registerContextCommand( contextCmdName, contextCmdCreator )
161  except:
162  print "Failed to register context command: %s" % contextCmdName
163  raise
164 
165  try:
166  mplugin.registerNode(nodeName, moveManipId, moveManipCreator, moveManipInitialize, OpenMayaMPx.MPxNode.kManipContainer)
167  except:
168  print "Failed to register node: %s" % nodeName
169  raise
170 
171 # uninitialize the script plug-in
172 def uninitializePlugin(mobject):
173  mplugin = OpenMayaMPx.MFnPlugin(mobject)
174  try:
175  mplugin.deregisterContextCommand(contextCmdName)
176  except:
177  print "Failed to deregister context command: %s" % contextCmdName
178  raise
179 
180  try:
181  mplugin.deregisterNode(moveManipId)
182  except:
183  print "Failed to deregister node: %s" % nodeName
184  raise