30 from maya.api
import OpenMaya, OpenMayaRender, OpenMayaUI
31 from maya
import OpenMayaRender
as OpenMayaRenderV1
33 logger = logging.getLogger(
'marqueeTool')
41 This context command class creates instances of the MarqueeContext.
43 kPluginCmdName =
"py2MarqueeToolContext"
51 This factory method creates an instance of the MarqueeContextCmd class.
57 This factory method creates an instance of the MarqueeContext class.
59 return MarqueeContext()
63 This context class extends a bounding box as the user drags the cursor during a selection
67 help_string =
"Click with left button or drag with middle button to select"
72 Create and return an instance of the MarqueeContext class.
74 return MarqueeContext(cls)
78 Initialize the context member variables.
82 self.list_adjustment = 0
84 self.setTitleString(
'Marquee Tool')
85 self.setImage(
'marqueeTool.xpm', OpenMayaUI.MPxContext.kImage1)
87 def stringClassName(self):
89 Return the class name string.
93 def toolOnSetup( self, event ):
95 Perform any setup operations when the tool is created. In this case,
98 self.setHelpString( MarqueeContext.helpString )
100 def check_event( self, event ):
102 Print out some information for the given event, such as its position which button
103 was pressed, and if any modifiers were also pressed.
105 logger.info(
"Check event:")
106 logger.info(
" position: (%d, %d)" % event.position)
107 logger.info(
" windowPosition: (%d, %d)" % event.getWindowPosition())
108 mouse_btn = event.mouseButton()
109 if (mouse_btn == OpenMayaUI.MEvent.kLeftMouse):
110 logger.info(
" mouseButton: kLeftMouse")
111 elif (mouse_btn == OpenMayaUI.MEvent.kMiddleMouse):
112 logger.info(
" mouseButton: kMiddleMouse")
114 logger.info(
" mouseButton: unknown!")
115 if event.isModifierShift():
116 logger.info(
" Shift key was pressed")
117 if event.isModifierControl():
118 logger.info(
" Control key was pressed")
119 if event.isModifierNone():
120 logger.info(
" No modifiers were pressed")
122 def do_press_common( self, event ):
124 Perfom the press operations common to both VP2.0 and the Legacy Viewport.
127 if event.isModifierShift()
or event.isModifierControl():
128 if event.isModifierShift():
129 if event.isModifierControl():
131 self.list_adjustment = OpenMaya.MGlobal.kAddToList
134 self.list_adjustment = OpenMaya.MGlobal.kXORWithList
136 elif event.isModifierControl():
138 self.list_adjustment = OpenMaya.MGlobal.kRemoveFromList
140 self.list_adjustment = OpenMaya.MGlobal.kReplaceList
144 self.start = event.position
146 logger.info(
"EXCEPTION IN do_press_common!")
148 def do_release_common( self, event ):
150 Perfom the release operations common to both VP2.0 and the Legacy Viewport.
153 self.last = event.position
162 if ( math.fabs(self.start[0] - self.last[0]) < 2
and math.fabs(self.start[1] - self.last[1]) < 2 ):
166 listAdjustment = OpenMaya.MGlobal.kReplaceList,
167 selectMethod = selection_method )
173 self.last[0], self.last[1],
174 listAdjustment = OpenMaya.MGlobal.kReplaceList,
175 selectMethod = OpenMaya.MGlobal.kWireframeSelectMethod )
187 logger.info(
"Mouse button release state:")
188 self.check_event(event)
190 def draw_marquee_gl( self ):
192 Draw the Marquee bounding box using OpenGL. This method is used by the Legacy Viewport.
197 gl_renderer = OpenMayaRenderV1.MHardwareRenderer.theRenderer()
198 gl_ft = gl_renderer.glFunctionTable()
199 gl_ft.glBegin( OpenMayaRenderV1.MGL_LINE_LOOP )
200 gl_ft.glVertex2i( self.start[0], self.start[1] )
201 gl_ft.glVertex2i( self.last[0], self.start[1] )
202 gl_ft.glVertex2i( self.last[0], self.last[1] )
203 gl_ft.glVertex2i( self.start[0], self.last[1] )
208 def doPressLegacy( self, event ):
210 Handle the mouse press event in the Legacy Viewport.
212 self.do_press_common(event)
214 self.fs_drawn =
False
216 def doDragLegacy( self, event ):
218 Handle the mouse drag event in the Legacy Viewport.
220 self.view.beginXorDrawing()
223 self.draw_marquee_gl()
228 self.last = event.position
231 self.draw_marquee_gl()
232 self.view.endXorDrawing()
234 def doHoldLegacy( self, event ):
236 Handle the mouse hold event in the Legacy Viewport.
238 self.do_hold_common(event)
240 def doReleaseLegacy( self, event ):
242 Handle the mouse release event in the Legacy Viewport.
246 self.view.beginXorDrawing();
249 self.draw_marquee_gl()
250 self.view.endXorDrawing()
252 self.do_release_common( event );
254 def doPress( self, event, drawMgr, context ):
256 Handle the mouse press event in VP2.0.
258 self.do_press_common(event)
260 def doRelease( self, event, drawMgr, context ):
262 Handle the release press event in VP2.0.
264 self.do_release_common(event)
266 def doDrag( self, event, draw_mgr, context ):
268 Handle the mouse drag event in VP2.0.
271 self.last = event.position
274 draw_mgr.beginDrawable()
280 draw_mgr.endDrawable()
282 def doHold( self, event, drawMgr, context ):
284 Handle the mouse hold event in VP2.0.
286 self.do_hold_common(event)
288 def doEnterRegion( self, event ):
290 Handle the enter region event. This method is called from both VP2.0 and the Legacy Viewport.
292 self.setHelpString( MarqueeContext.help_string )
295 def initializePlugin(plugin):
299 pluginFn.registerContextCommand( MarqueeContextCmd.kPluginCmdName, MarqueeContextCmd.creator)
301 logger.error(
"Failed to register context command: %s\n" % MarqueeContextCmd.kPluginCmdName)
306 def uninitializePlugin(plugin):
309 pluginFn.deregisterContextCommand(MarqueeContextCmd.kPluginCmdName)
311 logger.error(
"Failed to unregister command: %s\n" % MarqueeContextCmd.kPluginCmdName)