PDA

View Full Version : writing data out to a text file


thegarland
10-18-2010, 08:53 PM
Hey everybody,

I'm trying to write a MEL script that randomly generates a series of coordinates, and then writes those numbers out to an external text file.

The numbers will generally look like this:

1 2 3
4 5 6
7 8 9

What command do I want to use? Fwrite? And if so, what would be the proper syntax. I've tried to consult the Maya Command Reference, but it doesn't explain what each thing in the example code does.

Generally, I want the script to do th following:

open a file to write

execute the following loop 100 times

$x = rand(-10,10);
$y = rand(-10,10);
$z = rand(-10,10);

read $x, $y, and $z out to the file
start a new line

end of loop

close the file

If anybody can show me how to do this, I'd appreciate it.

NaughtyNathan
10-18-2010, 10:21 PM
the fwrite command is for writing out BINARY files (which it does say in the docs). you want a text/ASCII file, so you'd need fprint. The MEL doc examples would give you everything you need to get this working, as long as you know how to create a basic loop (and you actually read them! ;) )

string $exampleFileName = "c:/work/myOutput.txt";
$fileId=`fopen $exampleFileName "w"`;
for ($n=0; $n<100 ; $n++)
{
float $x = rand(-10,10);
float $y = rand(-10,10);
float $z = rand(-10,10);
fprint $fileId ($x + " " + $y + " " + $z + "\n");
}
fclose $fileId;
:nathaN

thegarland
10-19-2010, 03:00 AM
Worked like a charm. Thanks. :bowdown:

CGTalk Moderation
10-19-2010, 03:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.