def _HitTest(self, panel, cube1, cube2, cube3): width = cmds.modelEditor(panel, q=True, width=True) height = cmds.modelEditor(panel, q=True, height=True) # The scene is set up so it looks like this from the "top" camera: # # #-----------+ +-----------# # | +-------+ | # # | | | | # # cube2 | | cube1 | | cube3 # # | | | | # # | +-------+ | # #-----------+ +-----------# # # cmds.refresh(force=True) test1 = bool(cmds.hitTest(panel, width / 2, height / 2)) self.assertEqual(test1, cube1) cmds.refresh(force=True) test2 = bool(cmds.hitTest(panel, 2, height / 2)) self.assertEqual(test2, cube2) cmds.refresh(force=True) test3 = bool(cmds.hitTest(panel, width - 2, height / 2)) self.assertEqual(test3, cube3)
def dynSelection(self, *args, **kwargs): cmds.undoInfo(swf=False) #Stop Undo/Redo queue without flushing it! panel = cmds.getPanel(underPointer=True) panelType = cmds.getPanel(typeOf=panel) cursorPos = self.mousePos() if panelType == 'modelPanel': shapesUnderCursor = cmds.hitTest(panel, cursorPos[0], cursorPos[1]) if len(shapesUnderCursor) > 0: shape = cmds.ls(shapesUnderCursor, flatten=True)[0] if cmds.objectType(shape) == 'mnt_poseScope': if shape != self.oldShape: self.oldattrValue = cmds.getAttr(shape + '.opacity') if self.oldShape: cmds.setAttr(self.oldShape + '.opacity', self.oldattrValue) cmds.setAttr(shape + '.opacity', 0.15) self.oldShape = shape if len(shapesUnderCursor) == 0: if self.oldShape: cmds.setAttr(self.oldShape + '.opacity', self.oldattrValue) self.oldShape = None cmds.undoInfo(swf=True)
def object_under_cursor(): """Get objects under cursor""" pos = QtGui.QCursor.pos() widget = QtGui.qApp.widgetAt(pos) relpos = widget.mapFromGlobal(pos) panel = cmds.getPanel(underPointer=True) or "" if not "modelPanel" in panel: return return (cmds.hitTest(panel, relpos.x(), relpos.y()) or [None])[0]
def eventFilter(self, obj, event): #try: if event.type() == event.Enter: obj.grabKeyboard() if event.type() == event.Leave: obj.releaseKeyboard() if event.type() == event.KeyRelease: ctx = cmds.currentCtx() currentTool = cmds.contextInfo(ctx, c=True) if currentTool != self.currentTool or event.key() == QtCore.Qt.Key_Escape: self.pickedObjects = None self._quit(obj) if event.key() == QtCore.Qt.Key_Enter: self._quit(obj) if event.type() == event.MouseButtonPress: modifier = 0 qMod = event.modifiers() if qMod == QtCore.Qt.ShiftModifier: modifier = 1 elif qMod == QtCore.Qt.ControlModifier: modifier = 2 elif qMod == QtCore.Qt.AltModifier: modifier = 3 if event.button() == Qt.MouseButton.RightButton: # Escape condition if self.pickedObjects.isEmpty(): self.pickedObjects = None self._quit(obj) return True else: pickedName = None # Outliner picking if obj.metaObject().className() == "TpanelDagOutliner": pickedName = cmds.hitTest( obj.objectName(), event.x(), event.y())[0] # Viewport picking self.hit = cmds.dagObjectHit(mn="dummyMenu") if self.hit is True: popChild = cmds.popupMenu( "dummyMenu", q=True, itemArray=True) mItem = cmds.menuItem(popChild[0], q=True, l=True) pickedName = mItem.replace("...", "") cmds.popupMenu("dummyMenu", edit=True, deleteAllItems=True) if pickedName is not None: picked = om.MSelectionList() picked.add(pickedName) node = self._getLastDagNode(picked) if not picked.isEmpty() and (len(self.typeFilter) == 0 or node.apiTypeStr() in self.typeFilter): self.pickedObjects.merge(picked) self.hilite(picked) views = omui.M3dView for i in range(views.numberOf3dViews()): view = views.get3dView(i) view.refresh() if event.button() == Qt.MouseButton.LeftButton: self.buttonPressed.append(1) elif event.button() == Qt.MouseButton.MidButton: self.buttonPressed.append(2) self.modifierPressed.append(modifier) return True if obj.metaObject().className() == "TpanelDagOutliner": return False return True #return False #if event.type() == event.MouseButtonRelease: #if self.hit == False: #return False """except: self.pickedObjects = None obj.releaseKeyboard() self.unHilite() self.event.quit() return False""" return False
def getObjUnderCursor(): panel = mc.getPanel(underPointer=True) or "" relpos = getCursorPos() obj = mc.hitTest(panel, relpos[0], relpos[1]) return obj
def eventFilter(self, obj, event): #try: if event.type() == event.Enter: obj.grabKeyboard() if event.type() == event.Leave: obj.releaseKeyboard() if event.type() == event.KeyRelease: ctx = cmds.currentCtx() currentTool = cmds.contextInfo(ctx, c=True) if currentTool != self.currentTool or event.key( ) == QtCore.Qt.Key_Escape: self.pickedObjects = None self._quit(obj) if event.key() == QtCore.Qt.Key_Enter: self._quit(obj) if event.type() == event.MouseButtonPress: modifier = 0 qMod = event.modifiers() if qMod == QtCore.Qt.ShiftModifier: modifier = 1 elif qMod == QtCore.Qt.ControlModifier: modifier = 2 elif qMod == QtCore.Qt.AltModifier: modifier = 3 if event.button() == Qt.MouseButton.RightButton: # Escape condition if self.pickedObjects.isEmpty(): self.pickedObjects = None self._quit(obj) return True else: pickedName = None # Outliner picking if obj.metaObject().className() == "TpanelDagOutliner": pickedName = cmds.hitTest(obj.objectName(), event.x(), event.y())[0] # Viewport picking self.hit = cmds.dagObjectHit(mn="dummyMenu") if self.hit is True: popChild = cmds.popupMenu("dummyMenu", q=True, itemArray=True) mItem = cmds.menuItem(popChild[0], q=True, l=True) pickedName = mItem.replace("...", "") cmds.popupMenu("dummyMenu", edit=True, deleteAllItems=True) if pickedName is not None: picked = om.MSelectionList() picked.add(pickedName) node = self._getLastDagNode(picked) if not picked.isEmpty() and (len(self.typeFilter) == 0 or node.apiTypeStr() in self.typeFilter): self.pickedObjects.merge(picked) self.hilite(picked) views = omui.M3dView for i in range(views.numberOf3dViews()): view = views.get3dView(i) view.refresh() if event.button() == Qt.MouseButton.LeftButton: self.buttonPressed.append(1) elif event.button() == Qt.MouseButton.MidButton: self.buttonPressed.append(2) self.modifierPressed.append(modifier) return True if obj.metaObject().className() == "TpanelDagOutliner": return False return True #return False #if event.type() == event.MouseButtonRelease: #if self.hit == False: #return False """except: self.pickedObjects = None obj.releaseKeyboard() self.unHilite() self.event.quit() return False""" return False