CGTalk > Software > Autodesk Maya > Maya Programming
Login register
Thread Closed share thread « Previous Thread | Next Thread »  
 
Thread Tools Search this Thread Display Modes
Old 11-25-2012, 04:21 AM   #1
PozestStar
Your Majesty
portfolio
Melissa Starks
Cloth Sim Queen! Kinda...
Starving Artist ><
USA
 
Join Date: May 2007
Posts: 185
Send a message via AIM to PozestStar
Multiple intFields using one function?

I'm really showing my inexperience here, but I can't figure this out. I have three intFields that will need to use the same function. I have something like this:

Code:
def exampleWin(): win = mc.window(title = "Help me!") layout = mc.rowLayout(nc = 4) fieldOne = mc.intField("one", changeCommand = setOddInt) fieldTwo = mc.intField("two", changeCommand = setOddInt) fieldThree = mc.intField("three", changeCommand = setOddInt) mc.showWindow(win) def setOddInt(number): oddInt = number/2*2+1 mc.intField(edit = True, value = oddInt)


It gives me this error: line 14: No object name specified.

I know why, it's because on line 14, I haven't specified which intField to edit the value of. I didn't do that because more than one intField needs to use this chunk of code.

I'm not sure how to make this able to be used by multiple intFields.

I'd appreciate any help you could give me.

Thanks
 
Old 11-25-2012, 10:40 AM   #2
Nyro
Lurking
 
Nyro's Avatar
portfolio
A. D.
Vertex Slinger
Munich, Germany
 
Join Date: Dec 2006
Posts: 1,436
The solution is to define setOddInt to accept a string argument that holds the name of the intField to edit.

Here's pseudocode:

Code:
window MyWindow; rowLayout -nc 3; // first, just create the intfields (name as result into variables) $int1 = intField "one"; $int2 = intField "two"; $int3 = intField "three"; // now, with the variables populated with the names, each // intField's -cc can be supplied with that field's name. // Hence creating and editing the fields is separated into 2 commands* intField -e -cc ("setOddInt " + $int1) $int1; intField -e -cc ("setOddInt " + $int2) $int2; intField -e -cc ("setOddInt " + $int3) $int3; showWindow myWindow; proc setOddInt(string $intfield) { int $value = intField -q -value $intField; int $oddInt = $value/2*2+1; intField -e -value $oddInt $intfield; } // * Perhaps Python has a way to do this 'in one go'


I separate the intField's creation and the subsequent assignment of a -cc into 2 commands, because the following will not work (in MEL anyway):

Code:
$intField_1 = `intField -label "one" -changeCommand("myFunction " + $intField_1)`;


In MEL, you get a variable unkown error because you can't use $variableName in the declaration of $variableName (self-referencing).
Perhaps Python offers a way to do this more elegantly.
 
Old 11-25-2012, 12:53 PM   #3
djx
Where's my pony?
 
djx's Avatar
David Johnson
vfx & creature td
Melbourne, Australia
 
Join Date: Jun 2003
Posts: 3,389
Unless you want to switch back to mel, I would suggest using a lambda function.

Code:
def exampleWin(): win = mc.window(title = "Help me!") layout = mc.rowLayout(nc = 4) fieldOne = mc.intField("one", changeCommand = lambda x:setOddInt("one")) fieldTwo = mc.intField("two", changeCommand = lambda x:setOddInt("two")) fieldThree = mc.intField("three", changeCommand = lambda x:setOddInt("three")) mc.showWindow(win) def setOddInt(field): oddInt = mc.intField(field, q=True, v=True) oddInt = oddInt/2*2+1 mc.intField(field, edit = True, value = oddInt) exampleWin()


David
__________________
http://www.djx.com.au
 
Old 11-25-2012, 06:18 PM   #4
PozestStar
Your Majesty
portfolio
Melissa Starks
Cloth Sim Queen! Kinda...
Starving Artist ><
USA
 
Join Date: May 2007
Posts: 185
Send a message via AIM to PozestStar
Thanks!

Many thanks to both of you.

Nyro, I'd seen people create fields and later edit them, but I didn't do it because I thought it took up too much space I'm learning that that wasn't a good idea. I think I'm going to go back and take your advice.

djx, your lambda solution worked perfectly.

Thanks again for helping me. I really appreciate it!
 
Old 11-25-2012, 06:18 PM   #5
CGTalk Moderation
Lord of the posts
CGTalk Forum Leader
 
Join Date: Sep 2003
Posts: 1,066,481
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
CGSociety
Society of Digital Artists
www.cgsociety.org

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

All times are GMT. The time now is 02:06 AM.


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