How to run a certain function at every n seconds until tool is close

32 views
Skip to first unread message

kiteh

unread,
Jul 29, 2019, 5:01:56 PM7/29/19
to Python Programming for Autodesk Maya
Does Maya have some sort of callbacks or script jobs that will allows me to run a function every 30 seconds (eg. for updating the contents in the tool) and this callback.scriptjob will be removed upon tool closure?

Justin Israel

unread,
Jul 29, 2019, 5:26:31 PM7/29/19
to python_in...@googlegroups.com
On Tue, Jul 30, 2019 at 9:01 AM kiteh <kiteh...@gmail.com> wrote:
Does Maya have some sort of callbacks or script jobs that will allows me to run a function every 30 seconds (eg. for updating the contents in the tool) and this callback.scriptjob will be removed upon tool closure?

I don't believe Maya directly provides the equivalent of a cron task. But you can easily do that yourself with Python threading or Qts QTimer, and you can use a scriptJob to know when to stop it, if it provides relevant event conditions that meet your needs.

--
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/181c8fd3-8a6f-4407-ad56-64deefdfd97a%40googlegroups.com.

kiteh

unread,
Jul 29, 2019, 5:32:05 PM7/29/19
to Python Programming for Autodesk Maya
Hi Justin, 

I was looking at the use of `threading` while waiting for replies. 
I tried the following:

import threading
from maya import cmds
t = None

def run_check():
    global t
    t = threading.Timer(5.0, run_check)
    t.start()
    get_panels()

def get_panels():
    for pane in cmds.getPanel(visiblePanels=True):
        print '>>> pane: ', pane
 

run_check()

It seems to be running and printing out the correct results on the first run, but as soon as the next 5 seconds is up, instead of expecting the same results, I got the following error:
# # Exception in thread Thread-8:
# Traceback (most recent call last):
#   File "/builds/python_maya/2.7.11/852a2c8c4f/lib/python2.7/threading.py", line 801, in __bootstrap_inner
#     self.run()
#   File "/builds/python_maya/2.7.11/852a2c8c4f/lib/python2.7/threading.py", line 1073, in run
#     self.function(*self.args, **self.kwargs)
#   File "<maya console>", line 11, in run_check
#   File "<maya console>", line 29, in test
TypeError: Flag 'visiblePanels' must be passed a boolean argument

Pretty sure that I am passing a boolean argument into `visiblePanels`... Any ideas?

Ravi Jagannadhan

unread,
Jul 29, 2019, 5:33:08 PM7/29/19
to python_in...@googlegroups.com
What about MTimerMessage? That could help run something at regular intervals.



--
Where we have strong emotions, we're liable to fool ourselves - Carl Sagan

Justin Israel

unread,
Jul 29, 2019, 5:39:59 PM7/29/19
to python_in...@googlegroups.com
On Tue, Jul 30, 2019 at 9:33 AM Ravi Jagannadhan <enr...@gmail.com> wrote:
What about MTimerMessage? That could help run something at regular intervals.

That does sound like a better option
 

kiteh

unread,
Aug 1, 2019, 9:08:47 PM8/1/19
to Python Programming for Autodesk Maya
Cool, noted on that, thank you all. Will try it out.
Reply all
Reply to author
Forward
0 new messages