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 CLOSED
 
Thread Tools Search this Thread Display Modes
  11 November 2012
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()
	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!
 
  11 November 2012
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)
 
  11 November 2012
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 )
 
 
  11 November 2012
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!
 
  11 November 2012
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.
 
  11 November 2012
I learn yet again. Thanks Nathan
 
  11 November 2012
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!
 
  11 November 2012
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...
 
  11 November 2012
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.
 
  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
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 12:44 PM.


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