|
Mastering
3D Studio MAX R3 |
Using Strings
Every text variable is a string. Strings are used to format data output,
add prompts for commands, and to indicate file paths.
Any variable can be converted to a string simply by using
new_variable = old_variable as string
(You can convert to any variable type using this as method,
except you cannot convert a string to a number.)
String variables can also be added to an existing string to create a
larger string, and to format data output. For instance, suppose you have
the variable T = 30 and you want to write a script to output
it, just like Temperature = T, but substituting the variable
value for T. You can do it this way:
“Temperature = “ + T as string
which will return Temperature = 30.
Outputting
String Variables
All string variables can be output to the Listener. This is useful either
to show the result of script calculations, or to prompt the user for input.
The PRINT and FORMAT commands are used to output strings.
The PRINT command simply prints the string to the Listener, to a new
script window, or to a file. For instance, in our script that checks whether
its hot or cold, you could output the result using the PRINT command
this way:
print “It is hot.”
For more information on using PRINT to a new script or to a file, see
Text File I/O later in this chapter.
The FORMAT command does the same as the PRINT command, but its
easier to use when printing a series of variables and results. To use
the FORMAT command, type the % character in the middle of the string,
and it will be replaced by the variable results. For example, in
format “If temperature is below % it is cold. Above % it is hot.” cold hot
the first % character will be replaced by the value of the cold
variable and the second will be replaced with the value of the hot
variable.
Using Special
Characters in String Values
Some symbols and characters have special meanings when used for string
values and must be used in a different format to differentiate them from
the actual symbol or character. For instance, in the FORMAT command, the
% value represents a variable value that will substitute for it, but if
you need to use the actual % symbol, you can use \% to
represent it.
The next list presents some of the commonly used symbols and characters
and their special formatting, and the results youll see when you
use that format:
| Character
| Result
|
| \
|
|
| \n
| New line
|
| \\
| \
|
| \%
| %
|
| \?
| ?
|
| \x{hex}
| Hexadecimal character
|
In addition, \\ characters are very important for file path definition.
Special Characters in Strings
When using \x{hex}, make sure you have a space after
it, or any following text will be understood as part of the hex
value. For instance, print 35\xb0C will
not return the degree symbol, but instead will evaluate b0c
as a hexadecimal character. To get the degree symbol, you will need
to separate the strings and add them, this way:
print (“35” + “\xb0” + “C”)
This will return 35ºC.
Use \n (for a new line) at the end of a string,
to make the interface look cleaner in the Listener. As an example,
when you create an error message, you can have multiple lines to
show the user what the error was, as in this example:
print “Error!\nSelect a 3D Object.”
This will return:
“Error!
Select a 3D Object.”
|
Manipulating
Strings
Sometimes you might need part of a string, or you might need to search
for words and characters inside strings. MAXScript provides you a series
of commands that allow you to do it.
Searching for Text
The FINDSTRING command searches for a string inside another string, and
returns the character position of the match. If there is more than one
match, it returns only the first. For instance, findstring It
is hot here. h will return 7.
|
| TIP All strings have an
option that shows how many characters are in the string. Simply type
variable.count and youll get the number of characters
in that string variable.
|
Cropping Text
The SUBSTRING command crops part of a string and returns it as a new
string. It works by defining the character position and the number of
characters to copy. For instance, substring It is hot above
80F. 7 3 will return hot.
FINDSTRING is very useful when used together with SUBSTRING, to search
and filter the string portion you want. For instance, you can use
substring “It is hot above 80F.” (findstring “It is hot above 80F.” “h”) 3
This line will search for and copy the letter h and the next
two letters, in a single command.
Transforming Text into Commands
The EXECUTE command executes the string as a command. Its very
useful when you need to use an object name in the scene or read some information
from a file.
For instance, execute temperature = 25.9 would
set the value of the variable temperature to 25.9. This
value could have been read from a file, or entered as text in any field.
|
| TIP You can use the \
special character to insert string variables or values inside an EXECUTE
command. For instance, execute a = \temperature\.
|
© 2000, Frol (selection,
edition, publication)
|