PDA

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


Daemonecles
06-10-2011, 05: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
self.win = cmds.window()
self.layout = cmds.columnLayout(parent=self.win)
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)'
cmds.showWindow()

f = ButtonWin()

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

Any help would be greatly appreciated!

ginodauri
06-10-2011, 07: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...

Daemonecles
06-10-2011, 07: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...

Daemonecles
06-10-2011, 08:17 PM
Looks like you're right and next time I'll RTFM. Thanks!

If anyone gets stuck here, this is the answer:
http://www.luma-pictures.com/tools/pymel/docs/1.0/ui.html
http://www.thnkr.com/wiki/index.php?title=Pass_arguments_to_a_button_command

CGTalk Moderation
06-10-2011, 08: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.