Mouse release event and QApplication.processEvents()

344 views
Skip to first unread message

Ahmidou Lyazidi

unread,
May 27, 2016, 6:16:07 PM5/27/16
to Python Programming for Autodesk Maya
Hi,
This is my first post here,
I'm trying to make a generalized pickobjects tool and I want it to stop the script evaluation until
the user exit the picking cession.
I've seen this asked here and there and there might just not be a solution for it, but I'm giving it a shot.
I'm actually quite close and I'm just using a while loop with processEvents() to still have access to the UI,
unfortunately I'm having an odd behavior on mouse release, it seems it's re-running the whole code, queuing it
and finally execute the queued code.

import maya.OpenMayaUI as om
from PySide import QtGui, QtCore
from PySide.QtCore import *
from PySide.QtGui import *
from shiboken import wrapInstance
import shiboken


class MouseEventFilter(QtCore.QObject):
   
def __init__(self, label=None):
       
self.__label = label
       
QtCore.QObject.__init__(self)
       
self.x = 0
       
self.y = 0
       
   
def eventFilter(self, obj, event):
        typ
= event.type()
       
if event.type() == event.MouseMove:
           
print event.x(), event.y()
           
self.x = event.x()
           
self.y = event.y()
       
if event.type() == event.MouseButtonPress:
           
print "pressed"
       
if event.type() == event.MouseButtonRelease:
           
print "release"
       
return False

class KeyEventFilter(QtGui.QWidget):
   
def __init__(self, label=None):
       
self.__label = label
       
QtGui.QWidget.__init__(self)
       
self.exit = False
       
self.setFocusPolicy(QtCore.Qt.StrongFocus)

   
def eventFilter(self, obj, event):
       
if event.type() == QtCore.QEvent.KeyPress:
            typ
= event.type()
           
print typ
           
self.exit = True
           
return True
 
       
return False


mayaMainWindowPtr
= om.MQtUtil.mainWindow()
mayaMainWindow
= wrapInstance(long(mayaMainWindowPtr), QWidget)
kEventFilter
= KeyEventFilter(mayaMainWindow)
mayaMainWindow
.installEventFilter(kEventFilter)

view
= om.M3dView.active3dView()
widget_ptr
= view.widget()
widget
= wrapInstance(long(widget_ptr), QWidget)
mEventFilter
= MouseEventFilter(widget)
widget
.installEventFilter(mEventFilter)

while True:
   
QtGui.qApp.processEvents()
   
#cmds.evalDeferred(flop, low=True)

   
#print eventFilter.x, eventFilter.y
   
if kEventFilter.exit == True:
        widget
.removeEventFilter(mEventFilter)
        kEventFilter
.deleteLater()
        mayaMainWindow
.removeEventFilter(kEventFilter)
        kEventFilter
.deleteLater()
       
break


print "should run after"

If someone have an idea about it!
Thanks
-A

Ahmidou Lyazidi

unread,
May 27, 2016, 6:20:30 PM5/27/16
to Python Programming for Autodesk Maya
I forgot to add, the procedure:
-run the script
-do a few clicks in the viewport
-press any key to escape the click cession and resume the script

Cheers
-A

Justin Israel

unread,
May 29, 2016, 12:32:33 AM5/29/16
to Python Programming for Autodesk Maya
I would be concerned that you have created a busy loop, by using a while loop and processEvents() without using WaitForMoreEvents. Maybe this is a cause of your issues, I dont know. Have you looked at using a QEventLoop? 

You can create one and pass a reference to your event watchers. Any one of them call call myEvent.quit() to break the event loop. Then in the spot where you want to block, you call myEvent.exec_(). After the exec returns, you can do all your clean up.

Justin



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/69984688-54a8-42bd-ac92-1add31e3b48f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages