|
|||||||||||||||||||||||||
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 DataYou 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 FilesAs 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.
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 isnt empty, PRINT TO adds the string to the end of the file.)
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.
Reading and Writing DataOnce 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 youve closed the file after printing data to it. If you hadnt 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. Lets 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. Its 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] ... Its easier to manipulate these files through MAXScript because you dont need to worry about opening or closing files. All you need to do is to define a useful format. Lets 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”
SummaryThe concepts in this chapter have helped you understand better the way MAXScript works. Youve learned also a bit of programming logic, which is the way the computer thinks. In the next chapter, youll learn how to manipulate scene elements, how to interact with objects, and how to create materials, using several concepts learned in this chapter.
© 2000, Frol (selection, edition, publication) |
|||||||||||||||||||||||||
|