Python API 2.0 Reference
python/api1/py1MoveManip.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 from builtins import next
61 import sys
62 import maya.OpenMaya as OpenMaya
63 import maya.OpenMayaUI as OpenMayaUI
64 import maya.OpenMayaMPx as OpenMayaMPx
65 
66 moveManipId = OpenMaya.MTypeId(0x00080050)
67 contextCmdName = "spMoveManipCtxCmd"
68 nodeName = "spMoveManip"
69 
70 class moveManip(OpenMayaMPx.MPxManipContainer):
71  fDistanceManip = OpenMaya.MDagPath()
72  fFreePointManip = OpenMaya.MDagPath()
73 
74  def __init__(self):
75  OpenMayaMPx.MPxManipContainer.__init__(self)
76 
77  def createChildren(self):
78  self.fDistanceManip = self.addDistanceManip("distanceManip", "distance")
79  distanceManipFn = OpenMayaUI.MFnDistanceManip(self.fDistanceManip)
80  startPoint = OpenMaya.MPoint(0.0, 0.0, 0.0)
81  direction = OpenMaya.MVector(0.0, 1.0, 0.0)
82  distanceManipFn.setStartPoint(startPoint)
83  distanceManipFn.setDirection(direction)
84  self.fFreePointManip = self.addFreePointTriadManip("pointManip", "freePoint")
85 
86  def connectToDependNode(self, node):
87  nodeFn = OpenMaya.MFnDependencyNode(node)
88 
89  try:
90  syPlug = nodeFn.findPlug("scaleY")
91  tPlug = nodeFn.findPlug("translate")
92  distanceManipFn = OpenMayaUI.MFnDistanceManip(self.fDistanceManip)
93  distanceManipFn.connectToDistancePlug(syPlug)
94  freePointManipFn = OpenMayaUI.MFnFreePointTriadManip(self.fFreePointManip)
95  freePointManipFn.connectToPointPlug(tPlug)
96  OpenMayaMPx.MPxManipContainer.finishAddingManips(self)
97  OpenMayaMPx.MPxManipContainer.connectToDependNode(self,node)
98  except:
99  sys.stderr.write( "Error finding and connecting plugs\n" )
100  raise
101 
102 def moveManipCreator():
103  return OpenMayaMPx.asMPxPtr( moveManip() )
104 
105 def moveManipInitialize():
106  OpenMayaMPx.MPxManipContainer.initialize()
107 
108 class moveManipContext(OpenMayaMPx.MPxSelectionContext):
109  def __init__(self):
110  OpenMayaMPx.MPxSelectionContext.__init__(self)
111  self.updateManipulatorsCallbackID = None
112 
113  def toolOnSetup(self,event):
114  updateManipulators(self)
115  self.updateManipulatorsCallbackID = OpenMaya.MModelMessage.addCallback(OpenMaya.MModelMessage.kActiveListModified, updateManipulators, self)
116 
117  def toolOffCleanup(self):
118  self.deleteManipulators()
119  try:
120  if self.updateManipulatorsCallbackID != None:
121  OpenMaya.MModelMessage.removeCallback(self.updateManipulatorsCallbackID)
122  except:
123  sys.stderr.write( "Cleanup called before setup.\n" )
124  super(moveManipContext, self).toolOffCleanup()
125 
126 def updateManipulators(clientData):
127  clientData.deleteManipulators()
128  selectionList = OpenMaya.MSelectionList()
129 
131  selectionIter = OpenMaya.MItSelectionList(selectionList, OpenMaya.MFn.kInvalid)
132  while not selectionIter.isDone():
133  dependNode = OpenMaya.MObject()
134  selectionIter.getDependNode(dependNode)
135  if dependNode.isNull() or not dependNode.hasFn(OpenMaya.MFn.kDependencyNode):
136  print("depend node is null")
137  continue
138 
139  dependNodeFn = OpenMaya.MFnDependencyNode(dependNode)
140  rPlug = dependNodeFn.findPlug("translate", False)
141  sPlug = dependNodeFn.findPlug("scaleY", False)
142  if rPlug.isNull() or sPlug.isNull():
143  print("translate and/or scale plugs are null")
144  next(selectionIter)
145  continue
146 
147  manipObject = OpenMaya.MObject()
148  manipulator = OpenMayaMPx.MPxManipContainer.newManipulator(nodeName, manipObject)
149  if manipulator is not None:
150  clientData.addManipulator(manipObject)
151  manipulator.connectToDependNode(dependNode)
152  next(selectionIter)
153 
154 class moveManipCtxCmd(OpenMayaMPx.MPxContextCommand):
155  def __init__(self):
156  OpenMayaMPx.MPxContextCommand.__init__(self)
157 
158  def makeObj(self):
159  return OpenMayaMPx.asMPxPtr( moveManipContext() )
160 
161 
162 def contextCmdCreator():
163  return OpenMayaMPx.asMPxPtr( moveManipCtxCmd() )
164 
165 
166 # initialize the script plug-in
167 def initializePlugin(mobject):
168  mplugin = OpenMayaMPx.MFnPlugin(mobject)
169 
170  try:
171  mplugin.registerContextCommand( contextCmdName, contextCmdCreator )
172  except:
173  print("Failed to register context command: %s" % contextCmdName)
174  raise
175 
176  try:
177  mplugin.registerNode(nodeName, moveManipId, moveManipCreator, moveManipInitialize, OpenMayaMPx.MPxNode.kManipContainer)
178  except:
179  print("Failed to register node: %s" % nodeName)
180  raise
181 
182 # uninitialize the script plug-in
183 def uninitializePlugin(mobject):
184  mplugin = OpenMayaMPx.MFnPlugin(mobject)
185  try:
186  mplugin.deregisterContextCommand(contextCmdName)
187  except:
188  print("Failed to deregister context command: %s" % contextCmdName)
189  raise
190 
191  try:
192  mplugin.deregisterNode(moveManipId)
193  except:
194  print("Failed to deregister node: %s" % nodeName)
195  raise