How to add multiple conditions in max scripts


#1

Hello All,
I’m trying to change the ProOptimizer Vertex% value based on vertex count I got for different objects
if Vertexcount of selected object is less than 500 the ProOptimizer vertex% should be 80
if Vertexcount of selected object is greater than 500 and less than 2000 the ProOptimizer vertex% should be 60 and so on…

Here is the my code

(
for obj in objects do
(
select obj

	optMod = ProOptimizer()
	addModifier obj optMod
	
	optMod.Calculate = true
	redrawviews()
            a= optMod.VertexCount<=500
            b= optMod.VertexCount>500 && optMod.VertexCount<=2000
            c= optMod.VertexCount>2000 && optMod.VertexCount<=5000
            d= optMod.VertexCount>5000

   case of

(
a: optMod.VertexPercent = 80
b: optMod.VertexPercent = 60
c: optMod.VertexPercent = 40
d: optMod.VertexPercent = 20

)

ConvertTo obj Editable_Poly

)

)

Anyone can assist me on the above code

Thanks !


#2
(
	for obj in objects do
	(
		select obj
		optMod = ProOptimizer()
		addModifier obj optMod
		
		optMod.Calculate = true
		redrawviews()

		vertCnt = (getpolygoncount obj)[2]
		if vertCnt <= 500 then
		(
			optMod.VertexPercent = 80
		)
		else
		(
			if vertCnt <= 2000 then
			(
				optMod.VertexPercent = 60
			)
			else
			(
				if vertCnt <= 5000 then
				(
					optMod.VertexPercent = 40
				)
				else
				(
					optMod.VertexPercent = 20
				)
			)
		)
		
		ConvertToPoly obj
	)
)

#3

Another one:

(
	local numVertRanges = #(500, 2000, 5000)
	local vertPercents = #(80, 60, 40, 20)
	
	fn getVertPercent vertCount =
	(
		index = 1
		for n in numVertRanges while (vertCount > n) do index += 1
		vertPercents[index]
	)
	
	for obj in objects do
	(
		select obj
		optMod = ProOptimizer()
		addModifier obj optMod
		
		optMod.Calculate = true
		redrawviews()
		vertCnt = (getpolygoncount obj)[2]
		
		optMod.VertexPercent = getVertPercent vertCnt
		ConvertToPoly obj
	)
)

Edit: for your code, just change ‘&&’ by ‘and’ and it will work fine.


#4

Alternatively you could use a case statement to get the correct percentage.

(
	
	fn GetOptimizationPercent node =
	(
		numverts = node.mesh.numverts
		
		percent = case of
		(
			(numverts > 5000): 20
			(numverts > 2000): 40
			(numverts >  500): 60
			default: 80
		)
		
		return percent
	)
	
	GetOptimizationPercent $
	
)

#5

the almost ideal answer is above. maybe only by mxs coding rules it’s just enough:

(
	fn GetOptimizationPercent node =
	(
		numverts = node.mesh.numverts
		
		case of
		(
			(numverts > 5000): 20
			(numverts > 2000): 40
			(numverts >  500): 60
			default: 80
		)
	)
	
	GetOptimizationPercent $
)

#6

“almost”… :stuck_out_tongue_winking_eye:
I like to put the return line whenever I can. You know :wink:


#7

you remember, we discussed RETURN in the mxs code. it doesn’t effect performance if it is at the end of function. but it shouldn’t be used to “break” the code. so I prefer to not use it at all.


#8

and it’s not the issue anymore for most recent versions… at least 2014+

so my “not using of RETURN” is just an “old schooling” :upside_down_face:


#9

IMHO, it’s a very hardcoded solution.


#10

you can see:

fn returnTest1 =
(
	for k=1 to 100 do
	(
		if k >= 50 do return ()
	)
)

fn returnTest2 =
(
	for k=1 to 100 do
	(
		if k >= 50 do break
	)
)

fn returnTest3 =
(
	for k=1 to 100 while k <= 50 do
	(
	)
)


(
	t0 = timestamp()
	h0 = heapfree

	for k=1 to 10000 do
	(
		
returnTest1()
--returnTest2()
--returnTest3()
		
	)

	format "time:% heap:%\n" (timestamp() - t0) (h0 - heapfree)
)

it’s still an issue in some situations…


#11

Did you forget that RETURN at the end of the functions was broken in … 3ds Max 2016(or some other new version in the 3ds Max back in the years)? :slight_smile:


#12

Did you forget that RETURN at the end of the functions was broken in … 3ds Max 2016(or some other new version in the 3ds Max back in the years)?

I remember this :), but I believe there is no more reason to worry about RETURN


#13

No one has expected this bug back in the days. And yet AD surprised us. :slight_smile:


#14

Yes, I do rememeber. However, I never run into a problem using return() as I use it where I can and never to break the code.

What I never use are break() and continue(). Those are real enemies :slight_smile:


#15

It’s even worse, you don’t need to use it in a loop to break the performance:

(
	
	fn fn1 =
	(
		1
	)
	
	fn fn2 =
	(
		return 1
	)
	
	fn fn3 =
	(
		return 1
		return 0
	)
	
	cnt = 100000
	
	st = timestamp()
	for j = 1 to cnt do fn1()
	format "FN1 -> time:%\n" (timestamp()-st)
	
	st = timestamp()
	for j = 1 to cnt do fn2()
	format "FN2 -> time:%\n" (timestamp()-st)
	
	st = timestamp()
	for j = 1 to cnt do fn3()
	format "FN3 -> time:%\n" (timestamp()-st)
	
)

FN1 -> time:33
FN2 -> time:33
FN3 -> time:3038

BTW, I know the second return() in fn3() will never be reached, even though it breaks the performance.


#16

nice catch!


#17

It’s working now, Thanks Miauu


#18

My problem has been solved, thanks everyone for your time:grinning: