Page 1229 - Kitab3DsMax
P. 1229

Chapter 49: Automating with MAXScript
                                                 i = -50
                                                 For s in spheres do
                                                 (
                                                  s.position = [i,i,i]
                                                  i = i + 10
                                                 )
                                             Study this script to make sure that you understand what’s going on. You use a for loop to process each
                                             sphere in your collection of spheres. For each one, you set its position to [i,i,i], and then you change
                                            the value of i so that the next sphere is at a different location.
                                             Functions
                                             The last feature of basic MAXScript that you’ll look at is the function. Functions are small chunks of
                                             MAXScript that act like program building blocks. For example, suppose that you need to compute the
                                             average of a collection of numbers many times during a script you’re writing. The MAXScript to do this
                                             might be:
                                                 Total = 0
                                                 Count = 0
                                                 For n in numbers do
                                                 (
                                                  total = total + n
                                                  count = count + 1
                                                 )
                                                 average = total / (count as float)
                                             Given a collection of numbers called numbers, this MAXScript computes the average. Unfortunately, every
                                             time you need to compute the average, you have to type all that MAXScript again. Or you might be smart
                                             and just cut and paste it in each time you need it. Still, your script is quickly becoming large and ugly, and
                                             you always have to change the script to match the name of your collection you’re averaging.
                                             A function solves your problem. At the beginning of your script, you can define an average function like this:
                                                 Function average numbers =
                                                 ( -- Function to average the numbers in a collection
                                                  local Total = 0
                                                  local Count = 0
                                                  For n in numbers do
                                                  (
                                                  total = total + n
                                                  count = count + 1
                                                  )
                                                  total / (count as float)
                                                 )
                                             Now any time you need to average any collection of numbers in your script, you could just use this to take
                                             all the numbers in the collection called num and store their average in a variable called Ave:
                                                 Ave = average num      -- assuming num is a collection
                                             Not only does this make your script much shorter if you need to average numbers often, but it makes it
                                             much more readable, too. It’s very clear to the casual reader that you’re going to average some numbers.
                                             Also, if you later realize that you wrote the average function incorrectly, you can just fix it at the top of the


                                                                                                                      1181
   1224   1225   1226   1227   1228   1229   1230   1231   1232   1233   1234