Find Me on

Contact:

Riyadh, Saudi Arabia

Tel: +966 533 238 935

Alaa@vfxarabia.co

Skype: 

Copyright © 2014 - 2019 VFXArabia.

Read an external text file:

My friend asked me how to read and external text file using maxscript. He wanted to create a tool that grab system directory paths from an external predefined text file.

 

I did a small search and the result for that was something called Filestream which by definition is:

A FileStream class implements text file input and output in MAXScript. A FileStream value represents an open text file in MAXScript. Text file I/O is performed by calling functions on the FileStream value.

It has a bunch of commands to deal with external files but I only need 5 of them, what I wanna do is:

-Open the file.

-Count the lines.

-Grab each line and append it to an array or assign it to a variable (putting in mind that each line holds only one path).

-Close the file.

 

First I will assume that your text file that holds the paths is located in D:\temp\test.txt

and let's say it contains:

Note:

In this tutorial you will learn how to read from an external file, but what about writing to that file. To know how check out this tutorial:

Print & Format...

D:\temp
D:\3D
C:\Windows

Next we will define some variables and we are going to meet the first Filestream command (Openfile) It's like telling maxscript to prepare the file for you to be read:

local file = openFile @"D:\temp\test.txt"
local line_cnt = 0
Global Paths=#() --the array that will hold the paths

After opening the file we will start going through the lines and assign each path to an array item, here will use two Filestream commands:

- (eof) command returns true if there is no more data in the file, false otherwise. this will help us in creating a loop for all the lines

- (readline) This one will read the next line each time it's executed. It's worth mentioning that this command will convert the line in the text file into a string so you can't execute it.

while not eof file do 
(

    r=  readLine file --read the first line and store as a string it in r
    append paths r --append the first line to paths array
    line_cnt += 1 --add 1 to the counter
)

Most of the work is done by now, and all of the paths are stored in the array.

We will use now the final Filestream two command:

(seek) I will use it to reset the file i.e if you wanna use the readline command after our loop the reading will start from the begining again, bare in mind it's not nessesary to use it in our case but it's a good practice.

(Close) Flushes the memory buffers and closes the file, so you can access it from another program and modify it.

seek file 0    -- reset position to beginning

close file -- closes the file

seek file 0    -- reset position to beginning

close file -- closes the file

Now if you typed paths and hit enter you will get this result:

#("D:\temp", "D:\3D", "C:\Windows")

So the overall script will look like this:

(

local file = openFile @"D:\temp\test.txt"
local line_cnt = 0
Global Paths=#() --the array that will hold the paths

 

while not eof file do 
(

    r=  readLine file --read the first line and store as a string it in r
    append paths r --append the first line to paths array
    line_cnt += 1 --add 1 to the counter
)

seek file 0    -- reset position to beginning

close file -- closes the file

)

I hope you found it benefitial, please if you have and question do comment here I'll be glad to answer them.

 

Kindest Regards

Alaa Alnahlawi^_^