2015-04-06 55 views
0

更新:) 我有一個用戶界面,帶有2個控制播放,「播放」和「停止」的按鈕。 我定義了一個CustomLocator對象。當按下開始按鈕後播放正在運行時,我希望每次播放幀時創建並繪製一個customLocator 。現在當按下開始按鈕時繪製定位器。但它每幀播放時間不抽,我可以」 T數字該怎麼辦呢?感謝您的幫助Maya Python Playback每次播放幀時創建對象

class PlayBackDoSomething(): 
 
    def __init__(self): 
 
     self.drawUI() 
 
     self.state = False 
 
     
 
    
 
    def drawUI(self): 
 
     if pm.window("UI_MainWindow", exists = True): 
 
      pm.deleteUI("UI_MainWindow") 
 
      
 
     pm.window("UI_MainWindow", title = "test playback...", w = 150, h = 150, mnb = False, mxb = False, sizeable = True) 
 
     pm.columnLayout("UI_MainLayout", w = 150, h =300) 
 
     pm.button("UI_pbStartButton", label = "Start", w = 150, command=self.pbStart) 
 
     pm.button("UI_pbStopButton", label = "Stop", w = 150, command=self.pbStop) 
 
     pm.showWindow("UI_MainWindow") 
 

 
    def pbStart(self,*args): 
 
     pm.play(state=True) 
 
     self.state = True 
 
     self.doTheJob()   
 
          
 
    def pbStop(self,*args): 
 
     pm.play(state=False) 
 
     self.state = False 
 
     self.stopTheJob() 
 
      
 
    def doTheJob(self): 
 
     #this is basically part of code that i want to execute each time a frame is updated after pressing Play button 
 
     print ("start of job") 
 
     a = pm.keyframe(query = True, timeChange = True) 
 
     print a 
 
     
 
     t = pm.currentTime() 
 
     cl = CustomLocator(t, 0,0) 
 
     cl.draw() 
 
     
 
       
 
    def stopTheJob(self): 
 
     print ("end of job") 
 

 
    
 
class CustomLocator(): 
 
    def __init__(self, x, y, z): 
 
     self.x = x 
 
     self.y = y 
 
     self.z = z 
 
     
 
    def draw(self): 
 
     pm.spaceLocator(p=(self.x, self.y, self.z)) 
 
     pm.spaceLocator(p=(self.x, self.y, self.z-5)) 
 
     pm.spaceLocator(p=(self.x, self.y, self.z+5)) 
 
     
 
      
 
     
 
def main(): 
 
    pb = PlayBackDoSomething() 
 

 

 
main()

回答

0

您可以通過將

打電話給你的打印
print (time); 

插入場景中對象的表達式。我只會在調試時這樣做,因爲打印會在您處理場景時減慢場景的交互式性能。如果需要的話,可以將表達式添加到像一個拍照的時候按下起動按鈕,然後刪除它,當你完成

更新

你可以做同樣的事情,用於創建定位器(再次,我建議動態創建和播放時停止或完成移除的表達。下面是一個創建定位器爲立方體啞例如在播放四處移動

pCube1.translateX = sin(time); 
float $tx = pCube1.translateX; 
float $ty = pCube1.translateY; 
float $tz = pCube1.translateZ; 
spaceLocator -p $tx $ty $tz; 

你也可以只強制使用cmds.currentTime()更新的時間,做你的爲每個框架進程即對於傳統的編程任務來說,這比將它們轉化爲表達式要容易得多。

+0

感謝您的幫助,因爲我的代碼對於我真正期望做的事情有點困惑,我只是更新了我的帖子 – user2383294 2015-04-07 03:01:51

+0

表達式編輯器與python對象非常慢,所以我寧願不考慮該選項。強制更新使用cmds.currentTime()的時間並不壞..我可以啓動一個循環並用我的開始按鈕迭代cmds.currentTime(i + = 1),但我不知道如何使用停止按鈕來分解它。 .. – user2383294 2015-04-07 05:32:15

+0

中斷這樣一個過程的唯一方法是使用主進度條,將其設置爲可互換,並讓用戶從那裏停止。手動創建的UI元素在腳本執行期間不會處於活動狀態。 – theodox 2015-04-07 18:09:01