2014-01-16 41 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