So we talked about basic If tests and how the test gets evaluated to give us a value of ‘true’ to continue with the script. We could satisfy multiple conditions by writing an If test within side an if test….
a = 5 b =3 if a == 5 then ( if b == 3 then ( print "a is 5 and b is 3" ) )
…or, we can use ‘and’ to test both together.
if a == 5 and b == 3 then print "a is 5 and b is 3"
And so if we’re thinking about this in result terms,
if true and true then Print "....."
We can also use or to test against two values…..
if a == 5 or a == 3 then print "a is 5 or 3"
Make sure you realise that the or goes between tests, not values…. ie… this doesn’t work… if a == 5 or 3 then, because we’re not providing two tests, we’re providing one test and the number 3.
The test above gets evaluated and we pass the first test so maxscript doesn’t bother evaluating the second, this is useful to know because sometimes you can avoid having to handle errors i.e…
if classof $Sphere001.material == VrayMtl and $Sphere001.material.reflection_subdivs == 16 then $Sphere001.material.reflection_subdivs == 32
Now because we’re passing the first test the second test will get evaluated, if the first test wasn’t passed, and we tried to evaluate the second test, lets say it was a VrayLightMtl, then we’d get an error because there would be no’ reflection_subdivs’ property for a VRayLightMtl, but as the second test will only get evaluated when the first one is passed we don’t get this problem, which is really really useful (especially if you’re as lazy as I am)
We can also combine the ‘and’ and ‘or’, it might be helpful to bracket things to make it clear to see what is happening.
If (a == "5" and b == "3") or b == "5" then print "blah blah blah"
We can force the way things get evaluated by putting in brackets which change the normal order of evaluation.
a = 5 b = 3 c = 5 if a == 5 and b == 3 and (c == 6 or b == 3) then print "hello"
Without the brackets this would get evaluated as… if (a ==5 and b == 3 and c == 6) or b == 3 then…. so this would fail as c does not equal (!=) 6.
If you need to test loads of different things for different values sometimes it can be easier to use ‘Case of’ for example…. here we specify the ‘factors’ that we want to write outcomes for.
Case (classof $Sphere001.material) of ( VRayMtl : ( $Sphere001.material.diffuse = color 0 0 0 ) VRayLightMtl : ( $Sphere001.material.color = color 0 0 0 ) default: ( $Sphere001.material = Standardmaterial diffuse:(color 0 0 0) ) )
With this we can write code for the different outcomes of getting the class of the Sphere’s material, and if it’s not either a VRayMtl or a VRayLightMtl then we default to the default value, which in this case assigns $Sphere001 with a Standard Max Material with a black diffuse colour. You don’t need to have a default option set.
You can also use Case of to evaluate expressions…
a = 4 b = 6 c = 10 Case of ( (a < b) : print "a is less than b" (b < c) : Print "B is less than c" )
Now, quickly look at the example above, you’ll see that actually both expressions will return true, so you might expect both statements to be printed but unfortunately the Case of function will only return one value so it can only deal with the first expression it finds that returns true, and then returns the code associated with that factor, So in this case it will only print “a is less than b”.