52 from builtins
import next
53 import maya.api.OpenMaya
as om
54 import maya.api.OpenMayaUI
as omui
60 kPluginCmdName=
"spMoveToolCmd"
61 kPluginCtxName=
"spMoveToolContext"
62 kVectorEpsilon = 1.0e-3
63 kZNegAxis = ( 0, 0,-1)
66 class MoveToolCmd(omui.MPxToolCommand):
67 kDoIt, kUndoIt, kRedoIt = 0, 1, 2
70 omui.MPxToolCommand.__init__(self)
71 self.commandString = kPluginCmdName
72 self.__delta = om.MVector()
75 argData = om.MArgDatabase(self.syntax, args)
76 vector = om.MVector(1.0, 0.0, 0.0)
77 if args.length() == 1:
78 vector.x = args.asDouble(0)
79 elif args.length == 2:
80 vector.x = args.asDouble(0)
81 vector.y = args.asDouble(1)
82 elif args.length == 3:
83 vector.x = args.asDouble(0)
84 vector.y = args.asDouble(1)
85 vector.z = args.asDouble(2)
87 self.__action(MoveToolCmd.kDoIt)
90 self.__action(MoveToolCmd.kRedoIt)
93 self.__action(MoveToolCmd.kUndoIt)
100 Command is finished, construct a string for the command
103 command = om.MArgList()
104 command.addArg(self.commandString)
105 command.addArg(self.__delta.x)
106 command.addArg(self.__delta.y)
107 command.addArg(self.__delta.z)
113 omui.MPxToolCommand.doFinalize(self, command)
117 def setVector(self, x, y, z):
122 def __action(self, flag):
124 Do the actual work here to move the objects by vector
126 if flag == MoveToolCmd.kUndoIt:
127 vector = -self.__delta
129 vector = self.__delta
133 slist = om.MGlobal.getActiveSelectionList()
134 sIter = om.MItSelectionList(slist)
136 mdagPath = om.MDagPath()
137 mComponent = om.MObject()
138 spc = om.MSpace.kWorld
142 while not sIter.isDone():
145 mdagPath = sIter.getDagPath()
146 mComponent = sIter.getComponent()
148 transFn = om.MFnTransform(mdagPath)
153 transFn.translateBy(vector, spc)
155 sys.stderr.write(
"Error doing translate on transform\n")
160 cvFn = om.MItCurveCV(mdagPath, mComponent)
164 while not cvFn.isDone():
165 cvFn.translateBy(vector, spc)
170 sCvFn = om.MItSurfaceCV(mdagPath, mComponent,
True)
175 while not sCvFn.isDone():
176 while not CvFn.isRowDone():
177 sCvFn.translateBy(vector, spc)
180 sCvFn.updateSurface()
183 vtxFn = om.MItMeshVertex(mdagPath, mComponent)
187 while not vtxFn.isDone():
188 vtxFn.translateBy(vector, spc)
190 vtxFn.updateSurface()
195 class MoveContext(omui.MPxSelectionContext):
196 kTop, kFront, kSide, kPersp = 0, 1, 2, 3
199 omui.MPxSelectionContext.__init__(self)
200 self.setTitleString(
"moveTool")
201 self.setImage(
"moveTool.xpm", omui.MPxContext.kImage1)
203 self.__view = omui.M3dView()
204 self.__startPos_x = 0
206 self.__startPos_y = 0
210 def toolOnSetup(self, event):
211 self.setHelpString(
"drag to move selected object")
213 def doPress(self, event, drawMgr, context):
214 omui.MPxSelectionContext.doPress(self, event, drawMgr, context)
215 spc = om.MSpace.kWorld
220 if not self.isSelecting():
221 self.__startPos_x, self.__startPos_y = event.position
222 self.__view = omui.M3dView.active3dView()
224 camera = self.__view.getCamera()
225 fnCamera = om.MFnCamera(camera)
226 upDir = fnCamera.upDirection(spc)
227 rightDir = fnCamera.rightDirection(spc)
231 if fnCamera.isOrtho():
232 if upDir.isEquivalent(om.MVector.kZnegAxisVector, kVectorEpsilon):
233 self.__currWin = MoveContext.kTop
234 elif rightDir.isEquivalent(om.MVector.kXaxisVector, kVectorEpsilon):
235 self.__currWin = MoveContext.kFront
237 self.__currWin = MoveContext.kSide
239 om.MGlobal.displayWarning(
'moveTool only works in top, front and side views')
240 self.__currWin = MoveContext.kPersp
244 self.__cmd = MoveToolCmd()
245 self.__cmd.setVector(0.0, 0.0, 0.0)
247 def doDrag(self, event, drawMgr, context):
248 omui.MPxSelectionContext.doDrag(self, event, drawMgr, context)
254 if not self.isSelecting():
255 self.__endPos_x, self.__endPos_y = event.position
260 self.__view.viewToWorld(self.__startPos_x, self.__startPos_y, startW, vec)
261 self.__view.viewToWorld(self.__endPos_x, self.__endPos_y, endW, vec)
262 downButton = event.mouseButton()
268 if self.__currWin == MoveContext.kTop:
269 if downButton == omui.MEvent.kMiddleMouse:
270 self.__cmd.setVector(endW.x - startW.x, 0.0, 0.0)
272 self.__cmd.setVector(endW.x - startW.x, 0.0, endW.z - startW.z)
274 elif self.__currWin == MoveContext.kFront:
275 if downButton == omui.MEvent.kMiddleMouse:
277 self.__cmd.setVector(endW.x - startW.x, 0.0, 0.0)
281 self.__cmd.setVector(endW.x - startW.x, endW.y - startW.y, 0.0)
283 elif self.__currWin == MoveContext.kSide:
284 if downButton == omui.MEvent.kMiddleMouse:
285 self.__cmd.setVector(0.0, 0.0, endW.z - startW.z)
287 self.__cmd.setVector(0.0, endW.y - startW.y, endW.z - startW.z)
290 self.__view.refresh(
True)
292 def doRelease(self, event, drawMgr, context):
293 omui.MPxSelectionContext.doRelease(self, event, drawMgr, context)
294 if not self.isSelecting():
295 self.__endPos_x, self.__endPos_y = event.position
302 if (math.fabs(self.__startPos_x - self.__endPos_x) < 2
and
303 math.fabs(self.__startPos_y - self.__endPos_y) < 2):
305 self.__view.refresh(
True)
307 self.__cmd.finalize()
308 self.__view.refresh(
True)
310 def doEnterRegion(self, event):
312 Print the tool description in the help line.
314 self.setHelpString(
"click on object and drag to move it")
320 class MoveContextCommand(omui.MPxContextCommand):
322 omui.MPxContextCommand.__init__(self)
331 return MoveContextCommand()
334 syntax = om.MSyntax()
335 syntax.addArg(om.MSyntax.kDouble)
336 syntax.addArg(om.MSyntax.kDouble)
337 syntax.addArg(om.MSyntax.kDouble)
342 def initializePlugin(mobject):
343 mplugin = om.MFnPlugin(mobject,
"Autodesk",
"1.0",
"Any")
345 mplugin.registerCommand(kPluginCmdName, cmdCreator, syntaxCreator)
346 mplugin.registerContextCommand(kPluginCtxName, ctxCmdCreator)
348 sys.stderr.write(
"Failed to register context command: %s\n" % kPluginCtxName)
352 def uninitializePlugin(mobject):
353 mplugin = om.MFnPlugin(mobject)
355 mplugin.deregisterCommand(kPluginCmdName)
356 mplugin.deregisterContextCommand(kPluginCtxName)
358 sys.stderr.write(
"Failed to deregister context command: %s\n" % kPluginCtxName)