Multiple intFields using one 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
  11 November 2012
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:

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

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!
  11 November 2012
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:35 PM.

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