PDA

View Full Version : A way for intField to only accept odd numbers?


PozestStar
11-15-2012, 08:00 PM
Pretty self explanatory title. I know it's kinda odd (pun!) but I was wondering if it's possible to have an intField only accept odd numbers. If it isn't possible, that's fine. I just thought I'd ask. So far, I've been doing something like this:


import maya.cmds as mc

def exampleUI():
exampleWin = mc.window()
mc.columnLayout()
testField = mc.intField(value = 63)
number = mc.intField(testField, query = True, value = True)
mc.button(label = "OK", command = lambda *args: exampleDef(number) )

mc.showWindow( exampleWin )

def exampleDef(number):
if number % 2 != 0:
print "It's odd! Yay!"
else:
print "Your number is even, prepare to die!"


Thanks!

Nyro
11-15-2012, 09:33 PM
You could make use of the intFields -changeCommand flag to provide a script that checks whether, after a change, the current value is odd (and leave it as is) or even (in which case you could issue a warning, or reset the value, or what have you)

NaughtyNathan
11-16-2012, 10:38 AM
I'll second what Nyro suggests, something like this:
def setMyOddInt(integer):
oddInt = integer/2*2+1
mc.intField('oddIntField', e=1, v=oddInt)

def exampleUI():
exampleWin = mc.window()
mc.columnLayout()
testField = mc.intField('oddIntField', value=0, changeCommand=setMyOddInt)
mc.showWindow( exampleWin )

Nyro
11-16-2012, 11:24 AM
Nathan, you got me confused:

oddInt = integer/2*2+1
That's essentially saying:
oddInt = integer+1
Essentially just increasing the input by 1 (4 becomes 5, 5 becomes 6, but still even and odd). Unless there's some weird operator precedence in Python that defies everything I learned in 5th grade?

Ah. this just occurred to me: does Python do implicit typecasting and the input is assumed a literal integer type and somehow automatically gets rounded (as in floor())? If that's the case it makes total sense and is actually quite smart!

NaughtyNathan
11-16-2012, 12:18 PM
That's essentially saying:
oddInt = integer+1
not quite, but you figured it out anyway.
as all expressions in the equation are integers, only integer maths is done on them, and as we all know, in integer maths: 3/2 = 1. so yes, all odd numbers are automatically floored by the first "/2". This would be the same in MEL btw, and in most other languages as far as I know.

Nyro
11-16-2012, 12:37 PM
I learn yet again. Thanks Nathan ;)

PozestStar
11-16-2012, 01:37 PM
That's brilliant! I'd gone the

if int % 2 != 0:
int + 1
bla bla

route, but I really like this other way. Thanks so, so much to the both of you! :bounce:

NaughtyNathan
11-16-2012, 01:55 PM
your method is just as valid Melissa, in fact it's probably safer and more versatile all round. With the %2 +1 approach you could easily round down your odd numbers if you wanted to rather than accept even numbers just getting +1...

DeadlyNightshade
11-16-2012, 04:46 PM
I would go with a modulus -approach as well. Seems to be the safer option.

CGTalk Moderation
11-16-2012, 04:46 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.