0

这是我的代码:

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 #

我知道在那之后玛雅想告诉我time2是一样的

我在这个中使用了它并且它有效:

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,它完全有效

请帮我。

此致

4

2 回答 2

0

我发现在使用默认 Maya 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)])

看看这个关于类的更详尽的解释。

于 2013-08-04T01:45:06.213 回答
0

您在 def animate之外声明了变量time1。当函数尝试运行时,它不知道 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 的东西——这对您未来代码的质量非常重要。

您可能应该查看Mechtley关于 Maya python 的书(尤其是关于 GUI 代码的章节)。这里这里这里也有有用的讨论

PS:有人可能会发布一个修复程序,将“全局”放在时间 1 和时间 2 前面,并将“全局时间 1,时间 2”放在动画 def 中。不要那样做。全局=邪恶。

于 2013-08-04T18:31:08.580 回答