View Full Version : reading data from an external text file

10 October 2010, 03:26 AM
Hey everybody,

I'm trying to make a 3D starmap, and I want to write a MEL script that will take a star object that I've modelled, read coordinates from an external text file, and place the object at those coordinates.

The data text file will contain a series of numbers like this:

1 2 3

4 5 6

7 8 9

and so on...

Here is a basic description of what I want to happen in the MEL script:

(1) select the object to be duplicated

(2) open a data file for reading

(3) until the end of the data is read, repeat the following loop

read back one line of data (three numbers)

assign those three numbers to variables X, Y, and Z

duplicate object and move it to the xyz coordinates

end of loop

I know how to take an object and place it at a specific set of coordinates, but what I'm having difficulty with is reading data from an external text file. What I'm specifally having trouble with is knowing how to properly use the fopen command.

It's critical that I am able to draw the coordinate data from an external file because there are over a thousand stars that I need to map, so I need to automate the process.

The Maya command reference has not been helpful, as they don't explain in the examples specifically what each thing does.

If anyone can help me figure this out, I'd really appreciate it.

10 October 2010, 02:27 PM
the fopen docs won't help you at all, but if you look at the fgetline command the examples there should tell you all you need to know. However, this exact topic has come up at least twice in recent memory, do a CGTalk search and you'll be bound to find it.

here's one to get you started:


10 October 2010, 06:05 PM
That code did exactly what I wanted. Thanks.

10 October 2010, 03:57 AM
That was just what I needed. Thanks.

One other thing, in addition to xyz coordinates, I also want to include a couple other items of info about each star (name, spectral class, etc) in the data file, and I want to rename the star object to include these bits of info.

for example, a typical line in the data file would read:

1, Proxima_Centauri, M5Ve, 15.4, 0.9, -0.9, 0

Here is the code that I have so far. I've added additional variables for the other items in the data file.

//open file to read
string $filename = "c:/data.txt";
int $fileId = `fopen $filename "r"`;

//prevents Maya from hanging if file doesn't exist
if (!`filetest -f $filename`) error "Specified File not Found!";

//repeat following loop until end of file is reached
while (!`feof $fileId`)
string $nextLine = `fgetline $fileId`;
// tokenize the line
string $tokens[];
tokenize $nextLine ", " $tokens;
float $h = $tokens[0];
float $d = $tokens[1];
float $sp = $tokens[2];
float $mg = $tokens[3];
float $x = $tokens[4];
float $y = $tokens[5];
float $z = $tokens[6];

//duplicate star object
duplicate -rr;

//rename object with data
rename "star_01" "star_" + $h + "_" + $d + "_" + $sp + "_" + $mg;

//move it to the coordinates
move $x $y $z;
fclose $fileId;
// end of code

The result I'm trying for is star_01 is renamed...


...but instead, I 'm getting a syntax error.

I think it's because I'm not tokenizing those other items correctly. If so, what is the correct way? :shrug:

10 October 2010, 08:56 AM
always put brackets around your string catenations.

rename "pSphere1" ("star_" + $h + "_" + $d + "_" + $sp + "_" + $mg);:nathaN

10 October 2010, 07:30 PM
Thanks. That solved part of the problem. But now I'm discovering that Maya is not reacting well to spaces in the star names. Sometimes it will simply replace the space with an underscore. But other times, like when there is a single letter after the name (Rigel Kentaurus B, for example), Maya doesn't know what to do with it, and sometimes converts it to a zero.

Is there a way that I can get Maya to automatically convert any spaces to underscores? I would just change them in the data file, but there are over 80,000 stars, so that's not really a good option.

10 October 2010, 09:00 PM
I'm not really sure how you are getting the names, I assumed that $d / $token[1] was the name, but in your previous code this is declared as a float?.

Anyway, you've got the name as a string somehow I take it? (e.g. "Rigel Kentaurus B") so you just need to use the substituteAllString command:

string $newName = `substituteAllString $name " " "_"`;:nathaN

CGTalk Moderation
10 October 2010, 09:00 PM
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.