如何定期执行Maya MEL程序

时间:2014-01-16 14:35:47

标签: timer intervals maya mel

我希望每隔x秒执行一次Maya MEL程序。有没有办法做到这一点?

2 个答案:

答案 0 :(得分:2)

mel设置将是

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

然而,很难从梅尔系统中获得时间("时间/ t")会让你有时间到时间而不是第二次在Windows上。在Unix系统中("日期+ \"%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空闲事件相同的限制;如果玛雅很忙,他们就不会被解雇。

答案 1 :(得分: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

我不知道开销是多少,但它只能在空闲时运行,所以这可能不是什么大问题。

大多数情况可以在Mel中完成(但通常不如优雅......)。最大的障碍是节省时间。在梅尔,你必须解析一个system time电话。

编辑:保留此Python,然后您可以在python timed_function()

中调用您的Mel代码