2

I am creating a custom UI inside maya using python and I'm stuck on this error that occurs on this line:

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

Here is my script:

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

Basically my script is trying to create a UI with buttons inside that do stuff. In this case, I am trying to create a textfield button which the user keys in a set of coordinates and the locator based curve is created according to the set of coordinates given. However, I could only manage to create a button that creates a default curve. Can some one tell me how to go about creating a button that takes into consideration what coordinates a person gives and outputs the specific curve?

2
  • You going to have to show us the errors too, or there's no way we're going to be able to figure out what went wrong... Commented Aug 8, 2010 at 16:45
  • Here is the error i got, parts = button2.split(",") # NameError: global name 'button2' is not defined # I am trying to pass whatever the user key inside the text field space and break them up and store them as a variable for the create curve function. But however, i got no idea how to go about doing it so I have been trying it at random. I hope you guys can give me some advice on how to go about doing it? Thanks! Commented Aug 8, 2010 at 17:11

2 Answers 2

3

You can use global variables, but the more elegant way is to implement a class.

Here's an example of how to fix this problem with a class. Notice how we're using self.button2 instead of just button2. This makes it an instance variable of the class and accessible in other functions, while button2 is a local variable and is no longer accessible when you leave a function.

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

And I know it's tooooooooo late to reply, and you'll be a master in python by now for sure ;)

Hope it will help some one else :)

Sign up to request clarification or add additional context in comments.

3 Comments

Definitely this is the better way to go. I would add that since you have a class, you should not need to define the init method unless you plan to override something in it.
@theodox- BTW, in python, "globals" aren't as evil as in some languages, since they are limited to the module they are declared in. Nevertheless, I agree that this solution (class) is better than using a global. :)
Some more reference for lurkers dealing with similar issues: techartsurvival.blogspot.com/2014/04/…
0

To share a variable between functions, such as "button2", you need to declare it as "global." Right now the two functions each create the button2 variable separately, and they can't see each other's.

So to start, declare the variable in the global scope, outside of the function. Then declare it again inside any function you want to use it, and then you can use it like a normal variable.

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

As for formatting an entry string, it looks like you're on the right track -- I just declared button2 globally, then added global button2 declarations inside each function, and I got your locator curve just fine.

1 Comment

NOTE: the first "global myVar" (outside of the two functions) is not strictly necessary. On the other hand, it is good practice to have that declaration, so people reading are aware of the global, even if they don't read the details of the functions. In question, "global button2" at start of createMyLayout & of edit_curve would fix it. "global button2" before any declaration would be optional, but nice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.