2

我正在使用 python 在 Maya 中创建一个自定义 UI,但我遇到了此行发生的错误:

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()    

基本上,我的脚本正在尝试创建一个带有按钮的 UI,里面有一些功能。在这种情况下,我试图创建一个文本字段按钮,用户键入一组坐标,并根据给定的一组坐标创建基于定位器的曲线。但是,我只能设法创建一个创建默认曲线的按钮。有人能告诉我如何去创建一个考虑到人给出的坐标并输出特定曲线的按钮吗?

4

2 回答 2

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()

而且我知道现在回复太晚了,你现在肯定会成为 python 大师;)

希望它会帮助别人:)

于 2013-06-21T23:07:56.453 回答
0

要在函数之间共享变量,例如“button2”,您需要将其声明为“global”。现在这两个函数分别创建了 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 声明,我得到了你的定位器曲线就好了。

于 2011-05-04T20:10:20.610 回答