View Full Version : Multiple intFields using one function?

11 November 2012, 04:21 AM
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:

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)


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.


11 November 2012, 10:40 AM
The solution is to define setOddInt to accept a string argument that holds the name of the intField to edit.

Here's pseudocode:

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):

$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.

11 November 2012, 12:53 PM
Unless you want to switch back to mel, I would suggest using a lambda function.

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"))


def setOddInt(field):
oddInt = mc.intField(field, q=True, v=True)
oddInt = oddInt/2*2+1
mc.intField(field, edit = True, value = oddInt)



11 November 2012, 06:18 PM
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 :banghead: 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!

CGTalk Moderation
11 November 2012, 06:18 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.