Timer in Maya to run a function every x seconds?

Hi there!

Sorry for the basic question. I am trying to find a way to run a function every x seconds, Iv tried to find a MTimer or QTimer way to do but didnt have success.

I wanted to run the function when open a simple cmds.window() and stop the function when closes this window.

Somebody knows how to do that?

thanks in advance!

Multiple ways to achieve it, but the simplest is prob just to record the current time when you first load the window and then check if x seconds have past and reset the new time goal. something like below. Someone might have a cleaner better way. I havent tested this code but the idea may help, feeding the windowName to the process and running it as long as the window exists.

def timeBasedExecution(windowName, x):
    timeGoal = cmds.currentTime( query=True ) + x
    while(cmds.window(windowName, exists=True)):
        if cmds.currenTime( query=True) >= timeGoal:
            doProcess()
            timeGoal += x

You can also do it via the Python builtin threading module, with some simple sleep/pauses in the func being called to.

Some example of threading (but without the pause, but it should get you going) here:

https://mayamel.tiddlyspot.com/#[[How%20can%20I%20track%20the%20mouse’s%20position%20in%20realtime%3F]]

https://mayamel.tiddlyspot.com/#[[Python%20Threading%20in%20Maya]]

As long as you don’t want to interact with maya in a thread, you can use it. But as soon as you access or manipulate data in maya from a thread it can become unstable. To the best way is to setup a MTimer scene message. You can use it with OpenMaya.