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