Jump to content
Blacko0ps

How to run an event only when left mouse bottom being held down?(Python

Recommended Posts

Hi Everyone,

I was wondering if there is anyway in Python to run a function only when the left mouse bottom being held down?

Thanks,

Share this post


Link to post
Share on other sites

It should be possible to do using Qt event system if you familiar with it.

What you need to do is to install eventFilter to Houdini's QApplication instance, and catch QMouseEvent there. 

Quick untested code from the top of my head:

app = QApplication.instance()

class Filter(QObject):
    def eventFilter(self, event, **kwargs):
        if isinstance(QMouseEvent, event):
            if event.button() ==  Qt.RightButton:
				# DO stuff
	return super(Filter, self).eventFilter(event, **kwargs)

app.installEventFilter(Filter())

 

Edited by Stalkerx777
  • Like 2

Share this post


Link to post
Share on other sites

Thanks Alex! Unfortunately, I am not familiar with QT event system. Is it a separate package or it's built in in Houdini ? 

Update- Ok, after doing some research I think this what you have been refering to : http://doc.qt.io/qt-4.8/qobject.html#installEventFilter

Still trying to figure out how to install it for Houdini 

Edited by Blacko0ps

Share this post


Link to post
Share on other sites
Posted (edited)

Any chance this thread can be revisited? I'd also like to know how, specifically, the code posted by @Stalkerx777 is meant to be added to Houdini.

When I run the code directly within an instance of Houdini (Houdini 17.5), I get this warning:`, this warning is

Qt Warn: QObject::installEventFilter(): Cannot filter events for objects in a different thread.

and the event is not installed. The same warning happens when I try to apply the event to hou.qt.mainWindow(). And suggestions on this?

Edited by Red Scholar

Share this post


Link to post
Share on other sites

H17 Introduced a new Python API for the viewport (Python states), this is a way to go if you need viewport interaction. If you want to catch an event in the whole Houdini app, then the trick I posted above should work, however, keep in mind, it must run in the main thread. Pretty much every place from which you can start Python code in Houdini will put it in the main thread, EXCEPT the Python shell - it runs in its own thread.

Share this post


Link to post
Share on other sites

I see. Python states are definitely the way to go for my purposes. Thanks Alex!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×