我希望每 x 秒执行一次我的 Maya MEL 程序之一。有什么办法吗?
问问题
2882 次
2 回答
3
梅尔设置将是
scriptJob -e "idle" "yourScriptHere()";
然而,很难从 Mel 获得以秒为单位的时间 - system("time /t") 会让你的时间精确到分钟,而不是 Windows 上的秒。在 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 空闲事件相同的限制;如果玛雅很忙,他们就不会开火。
于 2014-01-16T19:06:00.877 回答
2
一般来说,没有。但是在 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
我不知道开销是多少,但无论如何它只会在空闲时运行,所以这可能没什么大不了的。
大部分都可以在 Mel 中完成(但像往常一样不那么优雅......)。最大的障碍是获得时间。在 Mel 中,您必须解析一个system time
呼叫。
编辑:保留这个 Python,然后你可以从 python 中调用你的 Mel 代码timed_function()
于 2014-01-16T16:52:15.323 回答