2014-01-16 42 views

回答

1

一般来说,没有。然而在Python我能创造一些非常有效:

import time 

def createTimer(seconds, function, *args, **kwargs): 
    def isItTime(): 
     now = time.time() 
     if now - isItTime.then > seconds: 
      isItTime.then = now   # swap the order of these two lines ... 
      function(*args, **kwargs)  # ... to wait before restarting timer 

    isItTime.then = time.time() # set this to zero if you want it to fire once immediately 

    cmds.scriptJob(event=("idle", isItTime)) 

def timed_function(): 
    print "Hello Laurent Crivello" 

createTimer(3, timed_function) # any additional arguments are passed to the function every x seconds 

我不知道的开销是什么,但它只能在空闲运行,无论如何,所以它可能不是一个大问题。

这大部分都可以在梅尔完成(但像往常一样没有优雅...)。最大的障碍是获得时间。在梅尔,你必须解析一个system time电话。

编辑:保持这条巨蟒,你就可以从Python timed_function()

2

中调用你的梅尔代码梅尔设置将会是

scriptJob -e "idle" "yourScriptHere()"; 

但是很难从中获取的时间以秒梅尔系统(“时间/吨”)会让你时间到分钟,但不会在第二个窗口。在Unix系统中(“date + \”%H:%M:%S \“”)会让你获得小时,分钟和秒钟。

这里scriptJob的主要缺点是,当用户或脚本正在运行时,空闲事件将不会被处理 - 如果GUI或脚本执行了一些操作,您将不会在该时间段内触发任何事件。

您可以用Python线程也这么做:

import threading 
import time 
import maya.utils as utils 

def example(interval,): 
    global run_timer = True 
    def your_function_goes_here(): 
     print "hello" 

    while run_timer: 
     time.sleep(interval) 
     utils.executeDeferred(your_function_goes_here) 
     # always use executeDeferred or evalDeferredInMainThreadWithResult if you're running a thread in Maya! 

t = threading.Thread(None, target = example, args = (1,)) 
t.start() 

线程是更加强大和灵活的 - 和一个很大的痛苦的对接。他们也遭受与scriptJob空闲事件相同的限制;如果玛雅人很忙,他们不会开火。

+0

您确定Python函数不会挂断Maya,直到它完全返回?另外,我不会推荐'idleEvent' *。 [doc](http://download.autodesk.com/global/docs/maya2013/en_us/Commands/scriptJob.html)正确地说*谨慎使用idleEvents。*它具有不良的副作用,其中一个是它们是空白属性编辑器,干净的石板!通过使用它,你最终在自己的脚中射击。虽然你建议'闲置',但我不确定它是否有任何不同。 –

+0

Maya UI存在于一个线程中,所以它总是被在UI线程(mel或Python)中运行的代码阻塞。然而,对于python线程,除了传递结果(executeDeferred和evalDeferredInMainThreadWithResult调用将该执行位弹出到主线程中)时,您可以运行长时间后台进程_without_ blocking。不幸的是大多数场景操作也在UI线程中。空闲是“安全”的,因为它是有效的 - 但你只想在闲置时运行非常轻量级的代码,否则你会让程序感觉非常慢。 – theodox