Search code examples
mayahotkeys

Script allowing the Toggle between two scripts by one key?


I use the hot keys for two scripts in maya, A key for A script and B key for B script Instead of this, is it possible to toggle between two scripts,by just using one key. For example, when I push the A key first, it runs the A script, then I push the A button again, it runs the second script, and loop.

Is it possible in ?

then, what I have to add in two scripts ?

Thank you guys, I will really appreciate for your help.


Solution

  • To make a hotkey, you just create a runTimeCommand object using the Maya UI or with cmds.runtTimeCommand in Python. The script will handle the toggling and then call the other scripts.

    If you just want the toggle behavior in a single session, you'd do something like this:

    global script_state
    try:
        script_state = not script_state
    except NameError:
        script_state = True
    
    if script_state:
        print "on"
    else:
        print "off"
    
    
    script_state = not script_state
    

    If you run that several times in the listener you'll see it toggling after each run. You'd just replace the prints with your actual functions.