Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk 3ds® Max® / MaxScript / weirdness in do-while loop that reads from an external txt file
  RSS 2.0 ATOM  

weirdness in do-while loop that reads from an external txt file
Rate this thread
 
22285
 
Permlink of this thread  
avatar
  • Location: Honolulu, HI
  • Total Posts: 34
  • Joined: 07 March 2008 05:39 AM

animList = openFile(fbx_filepath)
while not eof(animlist) do
(
print (readLine(animList))
)

fbx_filepath is simply the path to a .txt file that contains a list of .fbx file names, one per line.
this isn’t really a major problem stopping me in my tracks, but i’m curious if anyone can explain what’s happening.

if you run that block of code (after plugging in a valid .txt file path for fbx_filepath), it will loop through each line and print out the file name.  but for some reason, when it hits the end of the file, it prints the last line twice. 

it seems like it’s running the last line in the do-while loop.  if you run:

animList = openFile(fbx_filepath)
while not eof(animlist) do
(
print (readLine(animList))
i += 1
)

it will increment i after printing each file.  you’ll notice it doesn’t print i out until the very end, after reaching the last line in the file.



- Howard Hsu http://www.linkedin.com/in/howardab234

Now using:
Windows XP 64 Pro v2003 SP2 (yes XP64 is still around)
Core 2 Quad CPU Q6600 @ 2.4GHz
2GB RAM
NVidia GeForce 8800 GTX

Max 2008 x64
Maya 2008 64
MotionBuilder 7.5

Replies: 0
avatar
  • Location: West Midlands, England, UK
  • Total Posts: 14445
  • Joined: 06 August 2007 11:06 PM
  • Permlink of this post

A couple of things first.

Always state the version of Max you’re using. Especially important in non version-specific forums like this one and even more so with Maxscript because of the differences between versions. Put the version (and your brief system specs) in your sig then you can’t forget.

Always enlose code fragments in [ code ] ... [ /code ] tags. The forum software plays havoc with them if you don’t - they are not copy/pastable.

What you’re seeing is:-
Partly buffering (the missing “i")
Partly the results of the fileRead function not the print of it
The whole script is being treated as a function and the reult of that function is the last line read (at least that is what it seems like), hence the doubled last filename.

(
local fNamefi
    
fName 
"c:\\3dsMax2009-64\\scenes\\scenelist"
openFile fName mode:"r"
1

while not eof f do
    (
    
format "Filename #% = %\n" (readline f)
    
1
    
)
    
close f
)

Filename #1 = 3. vaja Forum Master.max
Filename #2 = 3. vaja Forum Master01.max
Filename #3 = Align Problam.max
Filename #4 = Align Problam01.max
Filename #5 = BD_Bevel Profiles.max
...(removed a bunch of files here for clarity)
Filename #99 = visibility.max
Filename #100 = VPTest.max
Filename #101 = VPTest01.max
OK



Max 4.2 through 2013.
XP-64 (SP2)
NVidia 9800GTX-512 (Driver 266.58).
Core 2 Quad Q6600 2.4GHz, 8Gb Ram, DX9.0c.

Replies: 0
avatar
  • Kenc
  • Posted: 04 February 2009 09:34 AM

Steve is right. In your first sample ‘print (readLine(animList))’ is the last executed statement before it leaves the while. Place ‘print’ after ‘print (readLine(animList))’ and you will see that ‘print()’ will be echoed after executing ‘print().

In your second sample ‘i += 1’ is the last executed statement before it leaves the while, therefore it echoes the last value of i.

In Steve’s sample ‘ the close f’ echoes back ‘OK’ as it is the last statement of the block of code.



Replies: 0