PDA

View Full Version : Save and read data from external file


Thorn444
12-09-2009, 11:15 PM
Hi guys

I have spent today some time write easy script which save and read data from .txt file. In my case it was position of selected objects. It works but i am sure it must be much more sophisticated method how this must be done and i would like know if somebody can help me find you again. My biggest problem is transfer data which come like string back into value. Thanks for every help.


Part for save position data from selected objects

Obj_array = getcurrentselection()
newTXT = createFile "C:\\TEMP\\ObjectSelection.txt"
ObjPos_array = for i in Obj_array collect i.pos
for i=1 to Obj_array.count do
(
a = ObjPos_array[i]
format "%,%,%\n" a[1] a[2] a[3] to:newTXT
i = i +1
)
close newTXT
Part for read this data back into max like value array

in_name = "C:\\TEMP\\ObjectSelection.txt"
in_file = openfile in_name
Array_pos = #()
while not eof in_file do
(
Array_xyz = #()
x = readvalue in_file
append Array_xyz x
y = readvalue in_file
append Array_xyz y
z = readvalue in_file
append Array_xyz z
append Array_pos Array_xyz
)

denisT
12-09-2009, 11:37 PM
Hi guys

I have spent today some time write easy script which save and read data from .txt file. In my case it was position of selected objects. It works but i am sure it must be much more sophisticated method how this must be done and i would like know if somebody can help me find you again. My biggest problem is transfer data which come like string back into value. Thanks for every help.


Part for save position data from selected objects

Obj_array = getcurrentselection()
newTXT = createFile "C:\\TEMP\\ObjectSelection.txt"
ObjPos_array = for i in Obj_array collect i.pos
for i=1 to Obj_array.count do
(
a = ObjPos_array[i]
format "%,%,%\n" a[1] a[2] a[3] to:newTXT
i = i +1
)
close newTXT
Part for read this data back into max like value array

in_name = "C:\\TEMP\\ObjectSelection.txt"
in_file = openfile in_name
Array_pos = #()
while not eof in_file do
(
Array_xyz = #()
x = readvalue in_file
append Array_xyz x
y = readvalue in_file
append Array_xyz y
z = readvalue in_file
append Array_xyz z
append Array_pos Array_xyz
)


here is a cleaner version:


-- WRITE
Obj_array = getcurrentselection()
if (newTXT = createFile "C:\\TEMP\\ObjectSelection.txt") != undefined do
(
for obj in Obj_array do format "%\n" obj.pos to:newTXT

flush newTXT
close newTXT
)


-- READ
in_name = "C:\\TEMP\\ObjectSelection.txt"
if (in_file = openfile in_name) != undefined do
(
Array_pos = #()
while not eof in_file do append Array_pos (readvalue in_file)

close newTXT
)

Thorn444
12-10-2009, 12:09 AM
what say maybe just when i grow i want be good like denisT :) This is exactly about what i was talking. Cheers Denis

CGTalk Moderation
12-10-2009, 12:09 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.