Lesson 6: If that case then do what huh now? Part 2

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”.


About davewortley

Somewhere between an artist and a programmer, I like technical things, but being creative. I love problem solving and coming up with elaborate solutions. This blog is where I shall share ideas, tips, tutorials and anything that amuses me.
This entry was posted in Lessons, MaxScript. Bookmark the permalink.

2 Responses to Lesson 6: If that case then do what huh now? Part 2

  1. Thanks for sharing. I have a question regards the case breakdown in the material case code. I copied the code you typed and inserted it in a max file with a sphere and evaluated the code in the maxscript window and it worked as expected. However if inserted that code in the maxscript listener i get this error : — Syntax error: at ), expected
    — In line: )

    Why is that?

    ps: the tips are really helping out.


    • davewortley says:

      The maxscript listener is just an intermediate coding window, you can’t run multi-line code in it. It’s just for one liners and querying values basically. If your script is multi-line then you should be using a maxscript window and evaluating the script each time.

      Hope this helps.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s