Python: passing arguments from a button to a function

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
  02 February 2013
Python: passing arguments from a button to a function

def DoSomething(var):
    print var


variable = "hello world!"
cmds.button(label="My Button", command=DoSomething(variable))

I have the following pseudo code. All I want to do is have the function DoSomething print whatever is passed to it. But whatever I try, I get the following error:

# Error: TypeError: Invalid arguments for flag 'command'.  Expected string or function, got NoneType # 

How can you pass an argument from the button to a function?

  02 February 2013
the command flag is expecting a string.

normally it would look like this:

cmds.button('button1', edit=True, command='doSomething("arg")')

to pass in a variable, do this first:

cmd = 'doSomething("{0}")'.format(var)
cmds.button('button1', edit=True, command=cmd)
  02 February 2013
Command flags in Maya expect either strings or function objects. I recommend function objects, and they're easier to build and read.

When you put () after a function, you are calling the function and the return value of that function is being passed into the -command flag, which you don't want.

If the function you want called has no arguments, you can simply pass the function object:

cmds.button(label="My Button", command=DoSomething)

If you want to pass arguments, you can use a lambda (or functools.partial):

cmds.button(label="My Button", command=lambda x: DoSomething("arg"))
  02 February 2013
I highly recommend using pymel's Callback function for this.

#make sure to include
  from pymel.all import *
  createButton = cmds.button('yourButton', label='yourButton',  c=Callback(yourFunction, args,args))

This 'Callback' function has saved me from a lot of problems with buttons.
  02 February 2013
Thanks for the replies everyone!

I ended up using the partial, because it made the most sense to me

I'll just add it what I used here in case someone else stumbles across this thread while looking for the same answer.

At the very beginning of your script:

from functools import partial

When creating the button:

cmds.button(label = "Newr", w = 300, command = partial (CreateCharacter, 180, 20, 100, "George"))

And then when you define the method:

def CreateCharacter(height, width, health, name, *args):
	#code where you can use the arguments...

  02 February 2013
Thread automatically closed

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.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 03:58 PM.

Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.