Титульная страница
ISO 9000 ISO 14000
GMP Consulting
 
Mastering 3D Studio MAX R3

Previous Table of Contents Next


To access this data later, all you would need to do is type variable.value; e.g., oct2599 .name will return “John”. The same applies to all other properties.

Below is a sample script that will convert the local time to a structure variable storing the date and the number of seconds. It shows you almost all the concepts taught in this chapter. We will use this script later in Chapter 17.

struct clock (day, month, year, sec)
fn right_now =
(
ctime = clock()
l = localtime
m = (substring l 1 ((findstring l “/”)-1)) as integer
l_tmp = substring l ((findstring l “/”)+1) l.count
d = (substring l_tmp 1 ((findstring l_tmp “/”)-1)) as integer
if m > 12 do (tmp = d; d = m; m = tmp)
l_tmp1 = substring l_tmp ((findstring l_tmp “/”)+1) l_tmp.count
y = (substring l_tmp1 1 2) as integer
l_tmp2 = substring l_tmp1 ((findstring l_tmp1 “:”)-2) l_tmp1.count
h = (substring l_tmp2 1 2) as integer
l_tmp3 = substring l_tmp2 ((findstring l_tmp2 “:”)+1) l_tmp2.count
mm = (substring l_tmp3 1 ((findstring l_tmp3 “:”)-1)) as integer
l_tmp4 = substring l_tmp3 ((findstring l_tmp3 “:”)+1) l_tmp3.count
s = (substring l_tmp4 1 2) as integer
if l_tmp4.count > 2 then
 (
 if h == 12 then h = 0
 if (substring l_tmp4 (l_tmp4.count-1) l_tmp4.count) == “PM” do h += 12
 )
ctime.day = d
ctime.month = m
ctime.year = y
ctime.sec = s + mm * 60 + h * 60 * 60
return ctime
)

This script will check to see whether the time configuration in your system is A.M./P.M. or 24-hour, but it will not accept dates in dd/mm/yyyy format. It works by cropping the localtime string, outputting the date and time as integer values. Then the script converts the hour, minute, and second values to seconds only. Manipulating only the seconds and converting the output result to hours, minutes, and seconds later saves script code and processing time.

All the result values are placed in a structure variable, which separates the data to be reused easily later.

To execute it, simply assign a new variable to the function right_now. This will assign the current time to this variable. This script is useful for calculating how long a certain function takes to process. For instance, start = right_now() will return #clock(day:27, month:2, year:00, sec:23119). You can later assign a new variable and compare it with this one, calculating the time lapse.

Text File I/O: Reading and Writing Data

You can read and write data to a text file, giving you flexibility to work in MAXScript. This file can contain initial settings for a script, data to be imported and processed, or simply the results of any calculation made in any script.

Managing Files

As in any programming language, files must be opened or created to be manipulated in MAXScript.

Creating Files

The CREATEFILE command creates a new file and allows you to write to it. For instance,

fname = createfile “c:\\3dsmaxr3\\scripts\\test.dat”

will create this file. The value assigned to this variable is a filestream value. Filestream is a File I/O class, which means this variable represents a file opened for input/output.


NOTE Notice that this command uses \\ instead of \, because \\ is the special character that is used to represent a single \ in strings.

Later, when you start writing to this file, you can simply use the PRINT TO command, like this: print “Sample text” to:fname, which will write this string into the destination file. (If the destination file isn’t empty, PRINT TO adds the string to the end of the file.)


TIP If you want to write data to a file and you want it to create a line break, make sure you add \n to the string.

Opening Existing Files

The OPENFILE command will open existing files for read or append. It works exactly like CREATEFILE, except that you can specify which mode are you opening the file in: “a” is for appending and “r” is read-only.

If the file does not exist, OPENFILE will return undefined. For instance,

fname = openfile “c:\\3dsmaxr3\\scripts\\test.dat” mode:”r”

will open the test.dat file as read-only.


WARNING All files must be closed after they are used (whether they were created or opened), using close filename. If you do not close the file, you will not be able to access it outside of MAXScript, and you will be wasting memory.

Reading and Writing Data

Once you open a file or create one, you can read or write data to it. All data read from a file is in string format and must be converted to integer, float, or another format if needed.

Writing Data

Writing data to a file is as simple as outputting data in the Listener. The PRINT and FORMAT commands have an option that allows their output to be directed to a file. Simply add TO:filestream, where filestream is the variable you assigned the file to, using either OPENFILE or CREATEFILE. For instance, you could have:

fname = createfile “C:\\test.dat”
format “This is sample text.\nThis is the second line.” to:fname
close fname

This would create a file and write two lines of text in it. After creating the file, open it in Explorer, and you will see the data written to it. Notice you’ve closed the file after printing data to it. If you hadn’t closed it, you would not be allowed to open it in Explorer.

Reading Data

Reading data from a file is similar to writing it. You can use a series of commands to locate text, position the file, etc. Let’s see some of these.

The READLINE command allows you to read one line of the file. For instance, continuing to work on the file you created before:

fname = openfile “C:\\test.dat” mode:”r”
str = readline fname
close fname

str will be the first line of the file, which is “This is sample text.”

The READCHAR command allows you to read a single character of the file and works the same way as READLINE.

The EOF condition will tell you if the file is over or if there is still data to be read. It’s useful in a WHILE command that reads all the data in a file. For instance, the script below will read all lines of the file and store them in the str array:

fname = openfile “C:\\test.dat”
str = #()
count = 1
while not eof fname do
 (
 str[count] = readline fname
 count += 1
 )
close fname

There are more commands to read and manipulate data from a text file, like reading encrypted files, reading delimited strings, seeking data, and so on, but these commands are not commonly used. You can learn about them on the Online Help.

INI Files

MAXScript has implemented special read/write commands to manipulate INI files. INI files are special files with the following syntax:

[Section1]
key1 = value1
key2 = value2
[Section2]
...

It’s easier to manipulate these files through MAXScript because you don’t need to worry about opening or closing files. All you need to do is to define a useful format.

Let’s create a single file that will be manipulated later:

fname = createfile “c:\\test.ini”
print “[Objects]” to:fname
print “\nGeometry=” to:fname
print “\nLights=” to:fname
close fname

After the file is created, you can manipulate it using GETINISETTING or SETINISETTING. GETINISETTING will read the value stored in a keyword in a given section. SETINISETTING will write a new value to a keyword.

For instance, in our example, type:

setinisetting “c:\\test.ini” “Objects” “Geometry” “Box”

This will write the “Box” string to the Geometry keyword. If the keyword or the section does not exist, they are created. If you want to read this value, simply use:

getinisetting “c:\\test.ini” “Objects” “Geometry”

NOTE If the INI file is blank, or non-existent, SETINISETTING creates it and also creates the structure for you.

Summary

The concepts in this chapter have helped you understand better the way MAXScript works. You’ve learned also a bit of programming logic, which is the way the computer “thinks.”

In the next chapter, you’ll learn how to manipulate scene elements, how to interact with objects, and how to create materials, using several concepts learned in this chapter.


Previous Table of Contents Next

© 2000, Frol (selection, edition, publication)

 
Rambler's Top100