Execute Python Script in Maya on Double Click

I’m looking to execute a python script on double click in Maya’s viewport, but seem to be running into a few problems.

Does anyone have experience setting something like this up, or pointers on where I should start.

cheers in advance

You are into some obscure territory. The Maya api is pretty vast, and I may have missed it, but I don’t know of any easy way to do this. Take a look at http://download.autodesk.com/us/maya/2011help/API/class_m_px_context.html
There may also be a way to add an event callback directly to QT if you are using Maya >= 2012, but you’re not going to find a lot of examples.

If found a way, in python moreover :):

import time

DOUBLE_CLICK_MAX_DELTA = 1.0
class doubleClick():

    # our static member to keep track of the last click
    lastClick = time.time()
    
    # our static method to avoid having to deal with instances
    @staticmethod
    def clicked():
        print "clicked"
        currentTime = time.time()
        
        if currentTime - doubleClick.lastClick < DOUBLE_CLICK_MAX_DELTA :
            print "double click !"
            # to force 2 clicks before the next double click is detected, otherwise if click fast enough all clicks are seen as double clicks
            doubleClick.lastClick  = 0.0
        else :
            doubleClick.lastClick  = currentTime
        
        return
        
def main():
    cmds.draggerContext( 'dummyName', releaseCommand='doubleClick.clicked()')
    cmds.setToolTo('dummyName')
    return
    
main()


You can’t call it twice in the same session because the context created by the first call isn’t destroyed (and that’s what we want :):slight_smile:

I wish Autodesk was reading this.
we really need some improvement to the default native user input events in Maya, and while I haven’t dug into Qt yet, I am sure the libraries must have these features already.

draggerContext has its uses, and can be quite a decent tool, except it’s been left hanging after the migration to Qt and could use a major overhaul.

Yes, its not the best solution. Its slow and depending on Maya’s pleasure…
But its quite simple.

The best way might be to find the widget containing the viewport (among the 10k+ widgets maya’s interface has) and connect its mouse events to your tool slots. But its just guessing.

cheers for the replies, ill update the thread when I have a working version :slight_smile:

Does it have to be a double click?

You could get the same functionality by clicking on the viewport with a modifier.

You do not actually have to build a menu to take advantage of executing a command with a viewport mouse button click via popupMenu’s postMenuCommand. Just parent the popupMenu to viewPanes.

cmds.popupMenu( parent=‘viewPanes’, alt=True, SHIFT=True, button=3, pmc = yercommand )