29 from maya.api 
import OpenMaya, OpenMayaRender, OpenMayaUI
 
   30 from maya 
import OpenMayaRender 
as OpenMayaRenderV1
 
   32 logger = logging.getLogger(
'lassoTool')
 
   40     This context command class creates instances of the LassoToolContext. 
   42     kPluginCmdName = 
"lassoToolContext" 
   50         This factory method creates an instance of the LassoToolContextCmd class. 
   56         This factory method creates an instance of the LassoToolContext class. 
   58         return LassoToolContext()
 
   62     This context class extends a bounding box as the user drags the cursor during a selection 
   66     help_string = 
"Drag mouse to select points by encircling" 
   72     cursor_bits = bytearray( [
 
   73         0x00, 0x00, 0x00, 0x00, 0xe0, 0x07, 0x10, 0x08, 0x10, 0x10, 0x08, 0x10,
 
   74         0x08, 0x10, 0x08, 0x10, 0x08, 0x08, 0x08, 0x08, 0x14, 0x08, 0x14, 0x04,
 
   75         0x08, 0x07, 0xf4, 0x00, 0x02, 0x00, 0x01, 0x00] )
 
   76     cursor_mask_bits = bytearray( [
 
   77         0x00, 0x00, 0x00, 0x00, 0xe0, 0x07, 0x10, 0x08, 0x10, 0x10, 0x08, 0x10,
 
   78         0x08, 0x10, 0x08, 0x10, 0x08, 0x08, 0x08, 0x08, 0x14, 0x08, 0x14, 0x04,
 
   79         0x08, 0x07, 0xf4, 0x00, 0x02, 0x00, 0x01, 0x00] )
 
   84         Create and return an instance of the LassoToolContext class. 
   86         return LassoToolContext(cls)
 
   90         Initialize the context member variables. 
   94         self.sorted_lasso = []
 
   96             height=self.cursor_height,
 
   97             hotSpotX=self.cursor_x_hot,
 
   98             hotSpotY=self.cursor_y_hot,
 
   99             bits=self.cursor_bits,
 
  100             mask=self.cursor_mask_bits)
 
  102         self.first_draw = 
False 
  103         self.list_adjustment = 0
 
  105         self.setTitleString(
'Lasso Tool')
 
  108         self.setCursor(self.lasso_cursor)
 
  111         self.setImage(
'lassoTool.xpm', OpenMayaUI.MPxContext.kImage1)
 
  113     def stringClassName(self):
 
  115         Return the class name string. 
  119     def toolOnSetup( self, event ):
 
  121         Perform any setup operations when the tool is created.  In this case, 
  124         self.setHelpString( LassoToolContext.help_string )
 
  126     def do_press_common( self, event ):
 
  128         Perfom the press operations common to both VP2.0 and the Legacy Viewport. 
  133         if event.isModifierShift() 
or event.isModifierControl():
 
  134             if event.isModifierShift():
 
  135                 if event.isModifierControl():
 
  137                     self.list_adjustment = OpenMaya.MGlobal.kAddToList
 
  140                     self.list_adjustment = OpenMaya.MGlobal.kXORWithList
 
  142             elif event.isModifierControl():
 
  144                 self.list_adjustment = OpenMaya.MGlobal.kRemoveFromList
 
  146             self.list_adjustment = OpenMaya.MGlobal.kReplaceList
 
  148         start = event.position
 
  149         self.lasso.append([start[0], start[1]])
 
  150         self.min = [start[0], start[1]]
 
  151         self.max = [start[0], start[1]]
 
  152         self.first_draw = 
True 
  155     def do_release_common( self, event ):
 
  157         Perfom the release operations common to both VP2.0 and the Legacy Viewport. 
  161         self.append_lasso(self.lasso[0])
 
  162         self.sorted_lasso = sorted(self.lasso, key=
lambda x: (x[1], x[0]))
 
  172                                            OpenMaya.MGlobal.kReplaceList )
 
  188         found_entire_objects = 
False 
  189         found_components = 
False 
  191         while not iter.isDone():
 
  193             dag_path = iter.getDagPath()
 
  194             component = OpenMaya.MObject.kNullObj
 
  195             if iter.hasComponents():
 
  196                 sel = iter.getComponent()
 
  199             if component.isNull():
 
  201                 found_entire_objects = 
True 
  205             found_components = 
True 
  207             if component.apiType() == OpenMaya.MFn.kCurveCVComponent:
 
  208                 curve_cv_iter = OpenMaya.MItCurveCV( dag_path, component)
 
  209                 while not curve_cv_iter.isDone():
 
  211                         point = curve_cv_iter.position(OpenMaya.MSpace.kWorld)
 
  212                         pt = self.view.worldToView(point)
 
  213                         if self.point_in_lasso(pt):
 
  214                             single_component = curve_cv_iter.currentItem()
 
  215                             new_list.add ((dag_path, single_component))
 
  220             elif component.apiType() == OpenMaya.MFn.kSurfaceCVComponent:
 
  222                 while not surf_cv_iter.isDone():
 
  224                         point = surf_cv_iter.position(OpenMaya.MSpace.kWorld)
 
  225                         pt = self.view.worldToView(point)
 
  226                         if self.point_in_lasso(pt):
 
  227                             single_component = surf_cv_iter.currentItem()
 
  228                             new_list.add((dag_path, single_component))
 
  233             elif component.apiType() == OpenMaya.MFn.kMeshVertComponent:
 
  235                 while not vertex_iter.isDone():
 
  237                         point = vertex_iter.position(OpenMaya.MSpace.kWorld)
 
  238                         pt = self.view.worldToView(point)
 
  239                         if self.point_in_lasso(pt):
 
  240                             single_component = vertex_iter.currentItem()
 
  241                             new_list.add((dag_path, single_component))
 
  246             elif component.apiType() == OpenMaya.MFn.kMeshEdgeComponent:
 
  248                 while not edge_iter.isDone():
 
  250                         point = edge_iter.center(OpenMaya.MSpace.kWorld)
 
  251                         pt = view.worldToView(point)
 
  252                         if self.point_in_lasso(pt):
 
  253                             single_component = edge_iter.currentItem()
 
  254                             new_list.add ((dag_path, single_component))
 
  259             elif component.apiType() == OpenMaya.MFn.kMeshPolygonComponent:
 
  261                 while not polygon_iter.isDone():
 
  263                         point = polygon_iter.center(OpenMaya.MSpace.kWorld)
 
  264                         pt = view.worldToView(point)
 
  265                         if self.point_in_lasso(pt):
 
  266                             single_component = polygon_iter.currentItem();
 
  267                             new_list.add ((dag_path, single_component))
 
  275         if found_entire_objects 
and (
not found_components):
 
  283         self.sorted_lasso = []
 
  285     def draw_lasso_gl( self ):
 
  287         Draw the lasso using OpenGL.  This method is used by the Legacy Viewport. 
  292         gl_renderer = OpenMayaRenderV1.MHardwareRenderer.theRenderer()
 
  293         gl_ft = gl_renderer.glFunctionTable()
 
  294         gl_ft.glBegin( OpenMayaRenderV1.MGL_LINE_LOOP )
 
  295         for i 
in range(len(self.lasso)):
 
  296             gl_ft.glVertex2i( self.lasso[i][0], self.lasso[i][1] );
 
  301     def append_lasso( self, pt ):
 
  303         Append the given point to the points defining the lasso. 
  307         [ix, iy] = self.lasso[-1]
 
  308         ydif = int(math.fabs( y - iy ))
 
  314         if ( self.min[0] > x ):
 
  316         if ( self.max[0] < x ):
 
  318         if ( self.min[1] > y ):
 
  320         if ( self.max[1] < y ):
 
  331         for i 
in range(ydif):
 
  332             self.lasso.append([int(fx), int(cy)])
 
  336     def point_in_lasso( self, pt ):
 
  338         Check the given point to see if it's inside the loop defined by the lasso. 
  340         for i 
in range(len(self.lasso)):
 
  341             if (self.sorted_lasso[i][1] == pt[1]):
 
  342                 while ((self.sorted_lasso[i][1] == pt[1]) 
and (self.sorted_lasso[i][0] < pt[0])):
 
  344                 if (self.sorted_lasso[i][1] != pt[1]):
 
  348                 while (self.sorted_lasso[i][1] == pt[1]):
 
  358     def doPressLegacy( self, event ):
 
  360         Handle the mouse press event in the Legacy Viewport. 
  362         self.do_press_common(event)
 
  363         self.first_draw = 
False 
  365     def doDragLegacy( self, event ):
 
  367         Handle the mouse drag event in the Legacy Viewport.  Add to the growing lasso. 
  369         self.view.beginXorDrawing(
True, 
True, 1.0, OpenMayaUI.M3dView.kStippleDashed)
 
  370         if not self.first_draw:
 
  374             self.first_draw = 
False 
  377         current_pos = event.position
 
  378         self.append_lasso( current_pos )
 
  382         self.view.endXorDrawing()
 
  384     def doReleaseLegacy( self, event ):
 
  386         Handle the mouse release event in the Legacy Viewport. 
  391             self.view.beginXorDrawing(
True, 
True, 1.0, OpenMayaUI.M3dView.kStippleDashed);
 
  393             self.view.endXorDrawing()
 
  395         self.do_release_common( event );
 
  397     def doPress( self, event, drawMgr, context ):
 
  399         Handle the mouse press event in VP2.0. 
  401         self.do_press_common(event)
 
  403     def doRelease( self, event, drawMgr, context ):
 
  405         Handle the release press event in VP2.0. 
  407         self.do_release_common(event)
 
  409     def doDrag( self, event, draw_mgr, context ):
 
  411         Handle the mouse drag event in VP2.0. 
  414         current_pos = event.position
 
  415         self.append_lasso( current_pos )
 
  418         draw_mgr.beginDrawable()
 
  420         for i 
in range(len(self.lasso)-1):
 
  421             draw_mgr.line2d( 
OpenMaya.MPoint( (self.lasso[i][0], self.lasso[i][1])), \
 
  425         draw_mgr.line2d( 
OpenMaya.MPoint( (self.lasso[-1][0], self.lasso[-1][1])), \
 
  427         draw_mgr.endDrawable()
 
  429     def doEnterRegion( self, event ):
 
  431         Handle the enter region event.  This method is called from both VP2.0 and the Legacy Viewport. 
  433         self.setHelpString( LassoToolContext.help_string )
 
  436 def initializePlugin(plugin):
 
  440         pluginFn.registerContextCommand( LassoToolContextCmd.kPluginCmdName, LassoToolContextCmd.creator)
 
  442         logger.error(
"Failed to register context command: %s\n" % LassoToolContextCmd.kPluginCmdName)
 
  447 def uninitializePlugin(plugin):
 
  450         pluginFn.deregisterContextCommand(LassoToolContextCmd.kPluginCmdName)
 
  452         logger.error(
"Failed to unregister command: %s\n" % LassoToolContextCmd.kPluginCmdName)