Datasheet
16
Chapter 1 Customization and MEL Scripting
a
Passing Information to Files
Maya allows you to write and read custom binary files. This is useful when a MEL script needs
to call on a list or if the script needs to permanently record events (for example, a dynamic
simulation). To write to a file, you can use this code:
$fileName = ( `internalVar -userWorkspaceDir`
+ “note.tmp” );
$fileWrite = `fopen $fileName “w”`;
fprint $fileWrite “Maya Rules”;
fclose $fileWrite;
In this example, the written file, named note.tmp, is one line long and contains the text
Maya Rules. The
fprint command undertakes the writing. The fclose command frees the
written file; if
fclose is skipped, note.tmp will remain inaccessible to future fprint commands.
The
intenalVar variable, which is a permanent and global, establishes the directory to which
the file is written.
-userWorkspaceDir represents the current project directory. Other inter-
nalVar
flags represent other default locations; for example, -userTmpDir represents the Maya
temp directory and
-userScriptDir represents the user script directory.
To read and print the line contained within
note.tmp, use the following lines:
$fileName = ( `internalVar -userWorkspaceDir`
+ “note.tmp” );
$fileRead = `fopen $fileName “r”`;
string $readText = `fgetline $fileRead`;
print $readText; fclose $fileRead;
To write a longer file, you can create a script that contains these commands:
proc appendFile (){
string $valueToWrite = ($test + “\n”);
$fileWrite = `fopen note.tmp “a”`;
fprint $fileWrite $valueToWrite;
fclose $fileWrite;
}
In this example, fopen, fprint, and fclose commands are contained within a procedure
named
appendFile. To append the file, fopen uses the a option instead of the w option. Each
time
appendFile is called, it writes $valueToWrite to a binary file named note.tmp. Since no
07405c01.indd 1607405c01.indd 16 1/17/07 8:30:55 PM1/17/07 8:30:55 PM