2013-08-03 93 views
0

這是我的代碼:#NameError:全局名稱「時間1」沒有定義#瑪雅蟒蛇

def animate(*arg): 
    cvList = [] 
    myList = cm.ls(sl =True) 
    randList1 = [rand.uniform(-10,10)for items in range(10)] 
    for i in myList: 
     cvList = cm.ls(i+".cv[:]" , flatten = True) 
    for j in cvList: 
     cm.setKeyframe(j , ".cv[0].xValue" , t = cm.intField(time1 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[0].xValue" , t = cm.intField(time2 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[0].yValue" , t = cm.intField(time1 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[0].yValue" , t = cm.intField(time2 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[0].zValue" , t = cm.intField(time1 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[0].zValue" , t = cm.intField(time2 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[1].xValue" , t = cm.intField(time1 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[1].xValue" , t = cm.intField(time2 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[1].yValue" , t = cm.intField(time1 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[1].yValue" , t = cm.intField(time2 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[1].zValue" , t = cm.intField(time1 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[1].zValue" , t = cm.intField(time2 , query = True , value = True) , v = rand.choice(randList1)) 


animWindow = cm.window(t = 'Animation controls' , w = 150 , menuBar = True , bgc = [.2,.2,.2]) 
cm.menu(l = 'About' , tearOff = False) 
cm.menuItem(l = 'Version 1.0.0') 
cm.columnLayout() 
cm.text(l = 'Time 1' , w = 150) 
time1 = cm.intField() 
cm.text(l = 'Time 2' , w = 150) 
time = cm.intField() 
cm.button(l = 'Set key' , c = animate , w = 150, bgc = [.3,.3,.3]) 
cm.showWindow(animWindow) 

這是我得到的錯誤:

# NameError: global name 'time1' is not defined # 

我知道,瑪雅想後告訴我時間2相同

我在這一個用這個和它的工作:

def tinyRandomize(*arg): 
    myList = cm.ls (sl = True) 
    randoms = [rand.uniform(cm.floatField(Ceil, q = True , v = True),0.5)for i in range(30)] 
    for objects in myList: 
     cm.xform('%s.cv[0]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[1]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[2]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[3]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[4]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[5]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[6]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[7]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[8]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[9]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[10]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[11]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[12]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[13]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[14]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[15]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[16]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[17]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[18]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[19]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 
     cm.xform('%s.cv[20]'%objects , r = True , t = [(rand.choice(randoms)),(rand.choice(randoms)),(rand.choice(randoms))]) 

我爲這個def增加了一個gui,它完全工作

請幫助我。

問候

+0

你需要通過時間1,時間2 – sihrc

+1

你爲什麼寫一個函數有21個幾乎相同的複製粘貼行而不是2行循環? – user2357112

+0

你的第一個代碼片段恰好在類內嗎? – user2357112

回答

0

我發現它更容易,更清潔,更簡潔的創建與默認瑪雅UI元素,簡單的接口時,有一個類。當在init()中定義它們時,它們立即被調用,並且您的接口對象現在在您的本地範圍內。

import maya.cmds as cm 
import random as rand 

class RandomAnimation(): 
    def __init__(): 
     self.animWindow = cm.window(t = 'Animation controls' , w = 150 , menuBar = True , bgc = [.2,.2,.2]) 
     cm.menu(l = 'About' , tearOff = False) 
     cm.menuItem(l = 'Version 1.0.0') 
     self.cLayout = cm.columnLayout() 
     self.timeText = cm.text(l = 'Time 1' , w = 150) 
     self.time1 = cm.intField() 
     self.cm.text(l = 'Time 2' , w = 150) 
     self.time = cm.intField() 
     self.myButton = cm.button(l = 'Set key' , c = self.animate , w = 150, bgc = [.3,.3,.3]) 
     cm.showWindow(animWindow) 
     # Since the ui is in __init__() it will be ran once the class is instantiated.. 

    def animate(*args): 
     # This will gather all of the cvs in all of your selected curves.. 
     for cv in [for j in [cm.ls(i + ".cv[:]", fl=1) for i in cm.ls(sl=True)]: 
      # This will move all the cvs randomly.. 
      cm.xform(cv, r=1, t=[rand.uniform(-1, 1), rand.uniform(-1, 1), rand.uniform(-1, 1)]) 

查看this瞭解更多關於類的更全面的解釋。

0

您聲明變量時間1 def 動畫。當函數試圖運行時,它不知道time1是什麼。

@Argiri的答案是長遠的正確方法 - 將UI和回調函數放入一個類中是一種更簡潔,更好的組織代碼的方法。

使其真正清楚什麼引起的問題,試試這個:

from functools import partial 
import random as rand 

def animate(time1, time2, ignore): 
    print time1, time2, ignore 
    cvList = [] 
    myList = cm.ls(sl =True) 
    randList1 = [rand.uniform(-10,10)for items in range(10)] 
    for i in myList: 
     cvList = cm.ls(i+".cv[:]" , flatten = True) 
    for j in cvList: 
     cm.setKeyframe(j , ".cv[0].xValue" , t = cm.intField(time1 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[0].xValue" , t = cm.intField(time2 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[0].yValue" , t = cm.intField(time1 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[0].yValue" , t = cm.intField(time2 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[0].zValue" , t = cm.intField(time1 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[0].zValue" , t = cm.intField(time2 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[1].xValue" , t = cm.intField(time1 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[1].xValue" , t = cm.intField(time2 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[1].yValue" , t = cm.intField(time1 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[1].yValue" , t = cm.intField(time2 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[1].zValue" , t = cm.intField(time1 , query = True , value = True) , v = rand.choice(randList1)) 
     cm.setKeyframe(j , ".cv[1].zValue" , t = cm.intField(time2 , query = True , value = True) , v = rand.choice(randList1)) 


animWindow = cm.window(t = 'Animation controls' , w = 150 , menuBar = True , bgc = [.2,.2,.2]) 
cm.menu(l = 'About' , tearOff = False) 
cm.menuItem(l = 'Version 1.0.0') 
cm.columnLayout() 
cm.text(l = 'Time 1' , w = 150) 
time1 = cm.intField() 
cm.text(l = 'Time 2' , w = 150) 
time2 = cm.intField() 
cm.button(l = 'Set key' , c = partial(animate, time1, time2) , w = 150, bgc = [.3,.3,.3]) 
cm.showWindow(animWindow) 

模塊functools包括一個名爲部分一個對象,它捆綁了一組參數與功能,使他們能夠一起叫。通過在代碼運行時將按鈕命令設置爲partial,可以確保在回調運行時,intField對象將進入函數 - 因此,當def向兩個字段詢問它們是什麼時,它知道如何找到他們。

所有這一切說,使用類這樣的事情。如果你對班級不清楚,請花上幾天時間從瑪雅人的東西中學習,這對你未來代碼的質量非常重要。

您應該檢查出Maya python上的Mechtley's book(特別是關於GUI代碼的章節)。也有有益的討論herehere,並here

PS:有人可能會張貼在時間1和時間2和「全球時間1,時間2」的前提出「全球化」的動畫DEF內修復。 不要那樣做。 Globals =邪惡。

+0

哈哈先生先生的全球=邪惡! –

相關問題