|
Mastering
3D Studio MAX R3 |
Entering
Data
Sometimes you might need the user to type a value or text in the Listeneran
object name, a function value, a position, and so on. MAXScript provides
functions for these types of tasks.
Entering Text
Using the GETKBLINE command you can ask the user to type text in the
Listener. It may or not use a prompt to ask the user to type something.
For example, you can use
user_name = getkbline prompt:”Enter your name:”
The user name will be stored in the user_name variable.
Entering Values
Using the GETKBVALUE command you can ask the user to type a numerical
value in the Listener. It works exactly the same way as GETKBLINE, except
that it returns a float or an integer variable.
|
| TIP Both GETKBVALUE and
GETKBLINE can be used to ask the user to enter numeric values, but
when using GETKBLINE, the script must convert the variable to float
or integer, using variable = variable as float or as
integer. If the user entered a letter instead of a number,
the conversion will return undefined.
|
Using Conditions
Conditions are essential for any programming language. They allow you
to test values and perform different actions, based on the result. Conditions
make the script think and react the way you want it to.
If...
The IF command is very powerful. It can test and check for values
and perform the selected actions if the comparison is met.
Comparing Variables
First, you need to learn how comparisons work in MAXScript. You can check
for basically four conditions: whether two variables are equal or different,
or whether one is greater or smaller than the other. This is done through
a simple syntax: variable1 operation variable2. This expression will return
true if the comparison is true, or false if it fails.
If a comparison is entered in the Listener, MAXScript will output true
or false. If its part of a command, no value will be printed, but
the command will read the true/false and will act as needed.
These are the comparisons that can be made in MAXScript:
| Symbol
| Comparison
|
| ==
| equal to (two equal signs; remember,
this is different from =)
|
| !=
| different from
|
| >
| greater than
|
| <
| less than
|
| >=
| greater than or equal to
|
| <=
| less than or equal to
|
Equal to and different from can be used in all variable types or functions.
The remaining comparisons can be used only with numerical values or functions.
Actions Based on Comparisons
Using the IF command and comparisons, you can make the script react the
way you want it to. We will use a simple example here to illustrate how
conditions work, but you will see that we use conditions in all scripts
we write, like checking whether the user selected a valid object, if the
object has a material applied, if the value is a valid color value (color
values must be in the range 0255), etc. The next few chapters are
full of practical examples of conditions and comparisons.
In this example you can check the current temperature to see whether
its cold or hot. Then, if its cold, the script will output
a sentence telling you its cold. If its hot, it can tell you
its hot. This is a simple example of comparisons and actions based
on comparisons.
The IF command requires two keywords to define which action to take:
THEN and ELSE. It works by making a comparison; THEN, if the comparison
is true, it runs a series of actions; ELSE, if the comparison is false,
it runs a different set of actions. The ELSE statement can be omitted
if a false result means the script just doesnt do anything.
|
| NOTE The IF... THEN... command
always waits for an ELSE to output all results. To see the result
on screen after the latest IF, type something, and youll see
the result being evaluated. Instead of IF... THEN... ELSE, you can
use IF... DO, which will not wait for an ELSE; DO will evaluate the
result automatically.
|
For instance, lets write a small script to test whether the current
temperature (30јF) is hot or cold:
cur_temperature = 30
if cur_temperature > 80 then print “It is hot.”
if cur_temperature < 40 then print “It is cold.”
In our example, the temperature will be checked, and the result will
be output on the screen. If you want to go a bit further and allow the
user to interact with the script, you can change the first line to
cur_temperature = getkbvalue prompt:”Enter current temperature:”
Now, the script works by itself and allows you to test for different
temperature values.
Multiple Comparisons
Sometimes you might need to make more than one comparison. This can be
done using Boolean expressions, such as AND and OR. The AND Boolean will
return true only if all comparisons are true, and OR will return
true if at least one returns true. For instance, (2 4) AND (sin
45 < 1) will return true, because both conditions are met.
The expression (2 4) OR (sin 45 > 1) will also return
true, because at least one of the conditions is true.
NOT is also a Boolean expression that will invert the expression being
checked. For instance, NOT (2 > 4) will return true,
because youre checking to see if 2 is not greater than 4, which
is true.
Using Boolean expressions makes it easier to create multiple comparisons,
instead of creating a series of comparisons and commands. Lets redo
the temperature example, now using Boolean expressions and ELSE:
cur_temperature = getkbvalue prompt:”Enter current temperature:”
if (cur_temperature <= 80) AND (cur_temperature >= 40) then
print “It is warm”
else
if cur_temperature > 80 then
print “It is hot”
else
print “It is cold”
|
| TIP Its useful to
tabulate a script (that is, indent it with spaces or tabs like the
example code above) so you can keep track of the organization of the
comparisons. The MAXScript Editor makes it easy to create this formatting,
inheriting the same tabulation when you break lines.
|
© 2000, Frol (selection,
edition, publication)
|