CGTalk > Software > Autodesk Maya > Maya Programming
Login register
Thread Closed share thread « Previous Thread | Next Thread »  
Thread Tools Search this Thread Display Modes
Old 02-26-2013, 02:14 PM   #1
neelz's Avatar
Niels Vaes
Houthalen, Belgium
Join Date: Jul 2003
Posts: 174
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?

Old 02-26-2013, 03:04 PM   #2
austin, USA
Join Date: Nov 2006
Posts: 364
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)
Old 02-26-2013, 08:06 PM   #3
New Member
Jesse Capper
Oakland, USA
Join Date: Mar 2009
Posts: 6
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"))
Old 02-26-2013, 08:12 PM   #4
backwheelbates's Avatar
Eric Bates
Senior Animator
Image Engine
Vancouver, Canada
Join Date: Nov 2005
Posts: 128
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.
Old 02-26-2013, 09:43 PM   #5
neelz's Avatar
Niels Vaes
Houthalen, Belgium
Join Date: Jul 2003
Posts: 174
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...

Old 02-26-2013, 09:43 PM   #6
CGTalk Moderation
CGTalk Forum Leader
Join Date: Sep 2003
Posts: 1,066,478
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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 07:36 PM.

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