84 from builtins
import next
85 import maya.OpenMaya
as OpenMaya
86 import maya.OpenMayaMPx
as OpenMayaMPx
87 import maya.OpenMayaUI
as OpenMayaUI
90 kPluginCmdName=
"spMoveToolCmd"
91 kPluginCtxName=
"spMoveToolContext"
92 kVectorEpsilon = 1.0e-3
97 kTrackingDictionary = {}
100 class MoveToolCmd(OpenMayaMPx.MPxToolCommand):
101 kDoIt, kUndoIt, kRedoIt = 0, 1, 2
104 OpenMayaMPx.MPxToolCommand.__init__(self)
105 self.setCommandString(kPluginCmdName)
107 kTrackingDictionary[OpenMayaMPx.asHashable(self)] = self
110 del kTrackingDictionary[OpenMayaMPx.asHashable(self)]
112 def doIt(self, args):
115 if args.length() == 1:
116 vector.x = args.asDouble(0)
117 elif args.length == 2:
118 vector.x = args.asDouble(0)
119 vector.y = args.asDouble(1)
120 elif args.length == 3:
121 vector.x = args.asDouble(0)
122 vector.y = args.asDouble(1)
123 vector.z = args.asDouble(2)
124 self.__delta = vector
125 self.__action(MoveToolCmd.kDoIt)
128 self.__action(MoveToolCmd.kRedoIt)
131 self.__action(MoveToolCmd.kUndoIt)
133 def isUndoable(self):
138 Command is finished, construct a string for the command
142 command.addArg(self.commandString())
143 command.addArg(self.__delta.x)
144 command.addArg(self.__delta.y)
145 command.addArg(self.__delta.z)
151 OpenMayaMPx.MPxToolCommand._doFinalize(self, command)
155 def setVector(self, x, y, z):
160 def __action(self, flag):
162 Do the actual work here to move the objects by vector
164 if flag == MoveToolCmd.kUndoIt:
165 vector = -self.__delta
167 vector = self.__delta
177 spc = OpenMaya.MSpace.kWorld
181 while not sIter.isDone():
184 sIter.getDagPath(mdagPath, mComponent)
191 transFn.translateBy(vector, spc)
193 sys.stderr.write(
"Error doing translate on transform\n")
202 while not cvFn.isDone():
203 cvFn.translateBy(vector, spc)
213 while not sCvFn.isDone():
214 while not CvFn.isRowDone():
215 sCvFn.translateBy(vector, spc)
218 sCvFn.updateSurface()
225 while not vtxFn.isDone():
226 vtxFn.translateBy(vector, spc)
228 vtxFn.updateSurface()
233 class MoveContext(OpenMayaMPx.MPxSelectionContext):
234 kTop, kFront, kSide, kPersp = 0, 1, 2, 3
237 OpenMayaMPx.MPxSelectionContext.__init__(self)
238 self._setTitleString(
"moveTool")
239 self.setImage(
"moveTool.xpm", OpenMayaMPx.MPxContext.kImage1)
242 self.__startPos_x = 0
244 self.__startPos_y = 0
248 def toolOnSetup(self, event):
249 self._setHelpString(
"drag to move selected object")
251 def doPress(self, event):
252 OpenMayaMPx.MPxSelectionContext.doPress(self, event)
253 spc = OpenMaya.MSpace.kWorld
258 if not self._isSelecting():
259 argX = OpenMaya.MScriptUtil(0)
260 argXPtr = argX.asShortPtr()
261 argY = OpenMaya.MScriptUtil(0)
262 argYPtr = argY.asShortPtr()
263 event.getPosition(argXPtr, argYPtr)
264 self.__startPos_x = argX.getShort(argXPtr)
265 self.__startPos_y = argY.getShort(argYPtr)
269 self.__view.getCamera(camera)
271 upDir = fnCamera.upDirection(spc)
272 rightDir = fnCamera.rightDirection(spc)
276 if fnCamera.isOrtho():
277 if upDir.isEquivalent(OpenMaya.MVector.zNegAxis, kVectorEpsilon):
278 self.__currWin = MoveContext.kTop
279 elif rightDir.isEquivalent(OpenMaya.MVector.xAxis, kVectorEpsilon):
280 self.__currWin = MoveContext.kFront
282 self.__currWin = MoveContext.kSide
285 self.__currWin = MoveContext.kPersp
289 newCmd = self._newToolCommand()
290 self.__cmd = kTrackingDictionary.get(OpenMayaMPx.asHashable(newCmd),
None)
291 self.__cmd.setVector(0.0, 0.0, 0.0)
293 def doDrag(self, event):
294 OpenMayaMPx.MPxSelectionContext.doDrag(self, event)
300 if not self._isSelecting():
301 argX = OpenMaya.MScriptUtil(0)
302 argXPtr = argX.asShortPtr()
303 argY = OpenMaya.MScriptUtil(0)
304 argYPtr = argY.asShortPtr()
305 event.getPosition(argXPtr, argYPtr)
306 self.__endPos_x = argX.getShort(argXPtr)
307 self.__endPos_y = argY.getShort(argYPtr)
312 self.__view.viewToWorld(self.__startPos_x, self.__startPos_y, startW, vec)
313 self.__view.viewToWorld(self.__endPos_x, self.__endPos_y, endW, vec)
314 downButton = event.mouseButton()
320 if self.__currWin == MoveContext.kTop:
321 if downButton == OpenMayaUI.MEvent.kMiddleMouse:
322 self.__cmd.setVector(endW.x - startW.x, 0.0, 0.0)
324 self.__cmd.setVector(endW.x - startW.x, 0.0, endW.z - startW.z)
326 elif self.__currWin == MoveContext.kFront:
327 if downButton == OpenMayaUI.MEvent.kMiddleMouse:
329 self.__cmd.setVector(endW.x - startW.x, 0.0, 0.0)
333 self.__cmd.setVector(endW.x - startW.x, endW.y - startW.y, 0.0)
335 elif self.__currWin == MoveContext.kSide:
336 if downButton == OpenMayaUI.MEvent.kMiddleMouse:
337 self.__cmd.setVector(0.0, 0.0, endW.z - startW.z)
339 self.__cmd.setVector(0.0, endW.y - startW.y, endW.z - startW.z)
342 self.__view.refresh(
True)
344 def doRelease(self, event):
345 OpenMayaMPx.MPxSelectionContext.doRelease(self, event)
346 if not self._isSelecting():
347 argX = OpenMaya.MScriptUtil(0)
348 argXPtr = argX.asShortPtr()
349 argY = OpenMaya.MScriptUtil(0)
350 argYPtr = argY.asShortPtr()
351 event.getPosition(argXPtr, argYPtr)
352 self.__endPos_x = argX.getShort(argXPtr)
353 self.__endPos_y = argY.getShort(argYPtr)
360 if (math.fabs(self.__startPos_x - self.__endPos_x) < 2
and
361 math.fabs(self.__startPos_y - self.__endPos_y) < 2):
363 self.__view.refresh(
True)
365 self.__cmd.finalize()
366 self.__view.refresh(
True)
368 def doEnterRegion(self, event):
370 Print the tool description in the help line.
372 self._setHelpString(
"click on object and drag to move it")
378 class MoveContextCommand(OpenMayaMPx.MPxContextCommand):
380 OpenMayaMPx.MPxContextCommand.__init__(self)
383 return OpenMayaMPx.asMPxPtr(MoveContext())
386 return OpenMayaMPx.asMPxPtr(MoveToolCmd())
389 return OpenMayaMPx.asMPxPtr(MoveContextCommand())
393 syntax.addArg(OpenMaya.MSyntax.kDouble)
394 syntax.addArg(OpenMaya.MSyntax.kDouble)
395 syntax.addArg(OpenMaya.MSyntax.kDouble)
400 def initializePlugin(mobject):
401 mplugin = OpenMayaMPx.MFnPlugin(mobject,
"Autodesk",
"1.0",
"Any")
403 mplugin.registerContextCommand(kPluginCtxName, ctxCmdCreator, kPluginCmdName, cmdCreator, syntaxCreator)
405 sys.stderr.write(
"Failed to register context command: %s\n" % kPluginCtxName)
409 def uninitializePlugin(mobject):
410 mplugin = OpenMayaMPx.MFnPlugin(mobject)
412 mplugin.deregisterContextCommand(kPluginCtxName, kPluginCmdName)
414 sys.stderr.write(
"Failed to deregister context command: %s\n" % kPluginCtxName)