A way for intField to only accept odd numbers?

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
Old 11 November 2012   #1
A way for intField to only accept odd numbers?

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()
	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!"
		print "Your number is even, prepare to die!"

Old 11 November 2012   #2
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)
Old 11 November 2012   #3
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()
	testField = mc.intField('oddIntField', value=0, changeCommand=setMyOddInt)
	mc.showWindow( exampleWin )
Old 11 November 2012   #4
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!
Old 11 November 2012   #5
Quote: 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.
Old 11 November 2012   #6
I learn yet again. Thanks Nathan
Old 11 November 2012   #7
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!
Old 11 November 2012   #8
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...
Old 11 November 2012   #9
I would go with a modulus -approach as well. Seems to be the safer option.
Nightshade UV Editor (script for Maya) - Extends Maya's default UV Texture Editor with additional tools.
Old 11 November 2012   #10
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 01:12 PM.

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