Sunday, October 17, 2010

MEL Scripting Tutorial: How To Read A Text File

This is a scripting tutorial that will show you how to use MEL to read a text file and use the data from it in Maya. As a scripter it's an essential skill to have. A lot of times there will be data you need in a file that's outside of your program and you'll need to get it. The file may contain animation data you need to access, skin weighting data, a list of objects needed in a scene or any other type of data you could imagine a text file having. This tutorial assumes you have a basic understanding of MEL syntax and how to run a script in Maya.

Skill Level: Beginner to Intermediate

First off we need a data file to test with, so let's make one. Open up your favorite text editor (notepad will work if you don't have anything else), create a new .TXT file. and save it somewhere as "myDataFile.txt". For this lesson, I recommend saving your file somewhere with a relatively simple file path, e.g. "C:\mel\myDataFile.txt".

Copy and paste the following data into your file and save:

//objectName,posX,posY,posZ,rotX,rotY,rotZ,scaleX,scaleY,scaleZ
pCube1,0,0,0,0,0,0,1,1,1
pSphere1,-5,10,-5,-90,90,-45,1,1,1
pCylinder1,2.5,2.342,4.2823,0,0,0,1,1,1
pPlane1,0,0,0,360,180,45,1,1,1

-----------------------------------------------------------
Tip: The file doesn't necessarily have to use a .TXT file extension. Any text-based file format will work the same way. You can even make up your own file extension. I like to use .RIG files.
-----------------------------------------------------------

Now that we have a file to test with let's look at what's in it. It's important to look at what type of data you're working with so you can plan on how to bring it in and in what format you'll need it to be in once it's in Maya.

In this case there are multiple lines of text, with each line having ten values separated by commas. We refer to this type of data as comma-separated. Our example has the name of an object and it's nine transform values (translate X,Y,Z, rotate X,Y,Z, scale X,Y,Z). Note that the first line doesn't have any actual values, it just serves to tell us what each value represents. This is common in data files. You may need to adjust your script to skip the first line when it brings in the data.

Using this data we're going to find the objects in our Maya scene and set their transforms. Let's get to it!

The first part of our script is putting the path to the file we just created into a string variable.


// Define Your File Path
string $filePath = "C:/mel/myDataFile.txt" ;

-----------------------------------------------------------
Tip: File paths in Maya (and most packages) require that you use forward slashes (/) instead of backslashes (\). You will get errors if you use backslashes.
-----------------------------------------------------------

Now that we have the path saved in $filePath we're going to use a MEL command called fopen . This command tells Maya to find the file and open it and also tells Maya what we're going to do with the file. There is an optional string argument (I recommend always using it) that will tell Maya if you're going to just read the file, write to it, or append to the end of the existing file. Check out the MEL Documentation for a more in-depth description on how these work, but for now just be aware that since we're reading the data from the file, we're going to use "r" for read.

// Open Your File
$fileId = `fopen $filePath "r"` ;

With the line of code above we've stored our "open" file into the variable $fileId. Now anytime we want to refer to that file in our script we can refer to $fileId. By using the "r" string in the command we've also told Maya that it only has permission to read from the file and nothing else. This way we don't have to worry about the file being changed in any way.

-----------------------------------------------------------
Tip: In the fread command, besides "r" (read), you can also use "w" (write) or "a" (append). More on this in the next tutorial.
-----------------------------------------------------------

Now that the file is open, let's interact with it. There are two commands we're going to look at in this tutorial. The first command is fread. This is used in the following way:

// Read The Data Using fread
string $data = `fread $fileId $data` ;

The usage is a little odd since we're simultaneously defining a string variable and using it as an argument in the command, but this is the way fread works. What it will do is take all of your data in the file and return it as a single string. In this case our return string value would be:

// Result: objectName,polygonType,posX,posY,posZ,rotX,rotY,rotZ,scaleX,scaleY,scaleZ
pCube1,0,0,0,0,0,0,1,1,1
pSphere1,-5,10,-5,-90,90,-45,1,1,1
pCylinder1,2.5,2.342,4.2823,0,0,0,1,1,1
pPlane1,0,0,0,360,180,45,1,1,1//

Nice! All of the data from the text file is in Maya! Success!

The only problem is...ALL of that information is in a single string. It's not much use to you in this format since the actual data is dense and on multiple lines. If you really wanted to you could take that string and break it up into all of the data you need (a mixture of loops and heavy usage of MEL's tokenize command)...but there's an easier way to handle it. There is another command we can use called fgetline.

The fgetline command will get a single line of data from the file and put it into a string variable. This becomes useful because we can now create a string array and put each line into it separately. This will give us a string array containing our entire text file.

The only hiccup here is fgetline will simply get one line of text and then stop. It's not smart enough to continue through the whole text file, we have to tell it to do that. We do this with a WHILE loop. Check it out below ...let's combine everything we've done so far:

// Define Your File Path
string $filePath = "C:/mel/myDataFile.txt" ;

// Open Your File
$fileId = `fopen $filePath "r"` ;

// Get The First Line
string $nextLine = `fgetline $fileId` ;

// Loop Until The String Size Is Zero (No Data)
while (size($nextLine) > 0) {

// Strip Whitespace From The Beginning And End Of The Line
string $cleanLine = strip($nextLine) ;

// Print Line
print ($cleanLine+"\n") ;

// Get Next Line And Continue
$nextLine = `fgetline $fileId` ;

}

If you run this, you'll see Maya print out the five lines of our text file. This time, each of those lines was printed one at a time. Ultimate success!

Let's make a few more additions and turn this into a procedure. First I want it to return an array, so I'm going to define a string array and then put each of my lines into it when I get them from the text file. Next I want to add an argument to tell it to skip the first line. This is useful if I know the first line of my file contains data I don't want. Lastly, instead of hard coding the path into the function, I want to pass the file path into my procedure as an argument to make it more versatile.

When we're finished our procedure looks like this:

// **********************************************************
// Reads A Text File And Returns A String Array Of Each Line
global proc string[] jgTextFileToStringArray (int $skipFirstLine, string $filePath) {

// Open File
$fileId = `fopen $filePath "r"` ;

// Define String Array
string $dataArray[] ;

// Get The First Line
string $nextLine = `fgetline $fileId` ;

// Loop Until The String Size Is Zero (No Data On That Line)
while (size($nextLine) > 0) {

// Strip Whitespace From The Beginning And End Of The Line
string $cleanLine = strip($nextLine) ;

// Add To Array
$dataArray[size($dataArray)] = $cleanLine ;

// Get Next Line And Continue
$nextLine = `fgetline $fileId` ;

}

// Remove First Line
if($skipFirstLine) stringArrayRemoveAtIndex(0,$dataArray) ;

// Return Array
return $dataArray ;

}

Check out the finished version here:
jgTextFileToStringArray.mel

The next tutorial will extend on this and go over how to write to and append to a text file.

Leave a comment below if you found this helpful or have any questions.

Looking for more tutorials? Check out Script Swell's Technical Artist Tutorials page.

4 comments:

  1. many thanks for your perfect tutorial. I have just a question. if we need to process data from file how should I do? my mean is for example if we have a data set like IRIS in a text file and then we want to draw these 4 attributes data by dot or sphere what should we add to the code? because we need process every line and take 4 numeric attribute plus 1 label attribute from file and then find the exact place in our multi dimensional scene for each of line and put them.

    ReplyDelete
  2. From here, each line of the text file will be in your array, and from there it's highly dependent on what you're trying to do with it so I can't give you a direct answer.

    Check out the tutorial on Tokenizing though, that might help you parse the data and set you up to use it. Good luck!

    ReplyDelete
  3. Hey great tutorial!
    So coming from environment art, i am a MEL noob that's trying to create some tools to help myself out:
    So here's a simple script to save a UV snapshot, and immediately open it up with photoshop:

    uvSnapshot -aa -n "C:\\Users\\NACHO\\Documents\\maya\\projects\\default\\images\\outUV.tga" -xr 2048 -yr 2048 -r 255 -g 255 -b 255 -o -ff tga;
    system ("Start C:/Program Files/Adobe/Adobe Photoshop CS6 (64 Bit)/Photoshop.exe");
    system ("load C:\\Users\\NACHO\\Documents\\maya\\projects\\default\\images\\outUV.tga");

    I was trying to create a UI, got that working but not functional, so my question is:
    How would I make it so that a user can change the snapshot directory and file size in a UI window?

    ReplyDelete

Scripting Topics

MEL (41) Maya (39) Scripting (32) Scripts (21) programming (14) Free Mel Scripts (8) MaxScript (7) Coding (6) Rigging (5) tutorial (5) 3ds Max (4) Python (4) Tricks (4) faceware (4) image metrics (4) Learn (3) Namespace (3) Namespacing (3) animation (3) facial (3) webinar (3) Code (2) GDC (2) Game Developers Conference (2) Multiple Namespaces (2) Print Selected Objects (2) Recursive (2) Removing Namespace (2) Return (2) Set Driven Keys (2) TOkenize (2) Tips (2) Toggle Background Color with MEL (2) animation tools (2) animators resource (2) deformers (2) learning (2) maya tools (2) mesh (2) modeling (2) nodes (2) procedure (2) script swell (2) transforms (2) Animschool (1) Attribute (1) Background Color (1) Beer (1) Blur (1) Character Setup (1) Check if an object exists (1) Class (1) Command Line (1) Constraints (1) Create SDK (1) Create a directory with mel (1) Data (1) Export (1) FilterString (1) Fix (1) Floating Slider Time (1) Functions (1) Get Maya Version MEL (1) Get Parent (1) Google (1) Holiday (1) How To Write To A Text File (1) Import (1) Incremental Save (1) Index (1) Joint Chain (1) Make Set Driven Keys (1) Maya Version (1) Modules (1) Objects (1) Orient Constraint (1) PYMEL (1) Parent (1) Parent Constraint (1) Point Constraint (1) Position (1) Print (1) Print Current Selection (1) Print Random Quotes (1) Print Selection (1) Print Vertices (1) Progress Bar (1) Progress Window (1) PyQT (1) Removing Spaces From Names (1) Scene File Name (1) Select Connections (1) Select Outgoing Nodes (1) Split Bones (1) Split Joints (1) St. Patrick's Day (1) String Array (1) System (1) Transfer UVs (1) Viewport (1) White Space (1) Windows Username (1) Zero Out Attributes (1) animButtonState (1) arrays (1) articles (1) auto key (1) better (1) blendshapes (1) break (1) confirm dialog (1) continue (1) convention (1) e3 (1) efficiency (1) error (1) eval (1) executable (1) fclose (1) fopen (1) fprint (1) games (1) improving (1) infinite loop (1) joints (1) listHistory (1) listRelatives (1) logic (1) loops (1) milestone (1) nodeType (1) objExists (1) recursion (1) rotates (1) rotations (1) schools (1) sculpting (1) setAttr (1) shout outs (1) source (1) source a script with a variable (1) speed (1) tech-artists.org (1) translates (1) video (1) warning (1) world matrix (1) worldMatrix (1)
 
Script Swell - Blogged