2010-08-08 61 views
2

创建于玛雅UI我使用python创建在Maya中自定义UI和我卡在这个错误是在这条线出现:使用Python脚本

parts = button2.split(",") # NameError: global name 'button2' is not defined 

这里是我的脚本:

import maya.cmds as cmds 

def createMyLayout(): 
    window = cmds.window(widthHeight=(1000, 600), title="lalala", resizeToFitChildren=1) 
    cmds.rowLayout("button1, button2, button3", numberOfColumns=5) 

    cmds.columnLayout(adjustableColumn=True, columnAlign="center", rowSpacing=10) 

    button2 = cmds.textFieldButtonGrp(label="LocatorCurve", 
            text="Please key in your coordinates", 
            changeCommand=edit_curve, 
            buttonLabel="Execute", 
            buttonCommand=locator_curve) 
    cmds.setParent(menu=True) 

    cmds.showWindow(window) 

def locator_curve(*args): 
    # Coordinates of the locator-shaped curve. 
    crv = cmds.curve(degree=1, 
       point=[(1, 0, 0), 
         (-1, 0, 0), 
         (0, 0, 0), 
         (0, 1, 0), 
         (0, -1, 0), 
         (0, 0, 0), 
         (0, 0, 1), 
         (0, 0, -1), 
         (0, 0, 0)]) 


    return crv 

def edit_curve(*args): 
    parts = button2.split(",") 
    print parts 


createMyLayout()  

基本上我的脚本是试图创建一个用户界面内的按钮做东西。在这种情况下,我试图创建一个用户在一组坐标中键入的文本字段按钮,并根据给定的坐标集创建基于定位器的曲线。但是,我只能设法创建一个创建默认曲线的按钮。有人可以告诉我如何创建一个按钮,考虑到一个人给出的坐标并输出特定的曲线?

+0

你要必须向我们展示错误,或者我们无法弄清楚出了什么问题...... – 2010-08-08 16:45:04

+0

这是我得到的错误,parts = button2.split(“,”) #NameError:全球名称'button2'没有定义# 我想通过文本字段空间内的任何用户密钥,并打破它们并存储em作为创建曲线函数的变量。 但是,我不知道如何去做,所以我一直在随机尝试。我希望你们能给我一些关于如何去做的建议?谢谢! – Tom 2010-08-08 17:11:25

回答

0

要共享函数之间的变量,如“button2”,您需要声明它为“全局”。现在这两个函数分别创建button2变量,并且它们不能看到对方的。

所以要开始,在全局范围内声明该变量,而不是函数。然后在你想使用它的任何函数内再次声明它,然后你可以像使用普通变量一样使用它。

global myVar 
myVar = 1 

def test1(): 
    global myVar 
    print myVar 
    myVar += 1 

def test2(): 
    global myVar 
    print myVar 
    myVar += 1 
    print myVar 

test1() 
test2() 

# output: 
# 1 
# 2 
# 3 

至于格式化的条目字符串,它看起来像你在正确的轨道上 - 我只是宣布全球BUTTON2,然后添加的每个函数内部全球BUTTON2声明,我得到了你的定位曲线就好了。

+1

注意:第一个“全局myVar”(两个函数之外)不是绝对必要的。另一方面,它是**这种声明的良好实践,因此即使读者不了解函数的细节,阅读者也会意识到全局。有问题,createMyLayout&edit_curve开始处的“全局按钮2”会修复它。任何声明之前的“全局button2”都是可选的,但很好。 – ToolmakerSteve 2013-11-28 00:16:33

3

你可以使用全局变量,但更优雅的方法是实现一个类。

下面是一个如何解决这个问题的例子。注意我们如何使用self.button2而不是仅仅使用button2。这使其成为该类的实例变量并可在其他函数中访问,而button2是本地变量,在离开函数时不再可访问。

import maya.cmds as cmds 

class createMyLayoutCls(object): 
    def __init__(self, *args): 
     pass 
    def show(self): 
     self.createMyLayout() 
    def createMyLayout(self): 
     self.window = cmds.window(widthHeight=(1000, 600), title="lalala", resizeToFitChildren=1) 
     cmds.rowLayout("button1, button2, button3", numberOfColumns=5) 

     cmds.columnLayout(adjustableColumn=True, columnAlign="center", rowSpacing=10) 

     self.button2 = cmds.textFieldButtonGrp(label="LocatorCurve", 
             text="Please key in your coordinates", 
             changeCommand=self.edit_curve, 
             buttonLabel="Execute", 
             buttonCommand=self.locator_curve) 
     cmds.setParent(menu=True) 

     cmds.showWindow(self.window) 

    def locator_curve(self,*args): 
     # Coordinates of the locator-shaped curve. 
     crv = cmds.curve(degree=1, 
        point=[(1, 0, 0), 
          (-1, 0, 0), 
          (0, 0, 0), 
          (0, 1, 0), 
          (0, -1, 0), 
          (0, 0, 0), 
          (0, 0, 1), 
          (0, 0, -1), 
          (0, 0, 0)]) 
     return crv 

    def edit_curve(self,*args): 
     parts = self.button2.split(",") 
     print parts 
     txt_val = cmds.textFieldButtonGrp(self.button2, q=True,text=True) 
     print txt_val 


b_cls = createMyLayoutCls() 
b_cls.show() 

而且我知道这是tooooooooo来不及回复,你会现在肯定是在python的高手;)

希望这将有助于一些别人:)

+0

使用类而不是EVIL全局变量+1! – theodox 2013-06-22 04:48:52

+0

肯定这是更好的方式去。我会补充说,既然你有一个类,除非你打算重写__init__方法,否则你不需要定义__init__方法。 – renderbox 2013-11-13 00:56:27

+0

@ theodox-在python中,“globals”不像**在某些语言中是**,因为它们仅限于它们声明的模块。然而,我同意这个解决方案(类)更好比使用全球。 :) – ToolmakerSteve 2013-11-28 00:22:08