View Full Version : Passing Command Argument in a Button (PYTHON)

06 June 2011, 04:58 PM
So I'm just not sure what syntax to use in order to define a button's command flag as a function that takes a variable. Here's what I have:

from functools import partial
import maya.cmds as cmds

class ButtonWin(object):

def __init__(self):
#create the layout and items = cmds.window()
self.layout = cmds.columnLayout(
self.fieldGrp = cmds.intFieldGrp( label = "enter padding number", value1=4 )
#store the value of the fieldGrp in a variable
fieldVal = cmds.intFieldGrp (self.fieldGrp, query=True, value1=True)
#create the button
cmds.button( label = "Save", parent=self.layout, c='aw_save(fieldVal)'

f = ButtonWin()

It errors as such when I press the button:
# Error: NameError: name 'fieldVal' is not defined #

Any help would be greatly appreciated!

06 June 2011, 06:36 PM
Do a search.This was discussed many times.
There many ways to do this (decorators,nested functions , partial , lambda) , and bottom line is that you should pass function to button command not string.
From what i can see you are trying to use OOP , but don't understand some fundamental things.
My advice is to read about scopes in python , and than your life will be easier...

06 June 2011, 06:59 PM
I searched google and docs, but I didn't search specifically on CGTalk since I wasn't sure what exactly to input as a search string. I'll try to do that now...

06 June 2011, 07:17 PM
Looks like you're right and next time I'll RTFM. Thanks!

If anyone gets stuck here, this is the answer:

CGTalk Moderation
06 June 2011, 07:17 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.