Notifications
Clear all

[Closed] Save and read data from external file

Hi guys

[left]  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.
[/left]
  
  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 "%,%,%
" 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
      )
      
2 Replies
1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

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 "%
" 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
)


what say maybe just when i grow i want be good like denisT This is exactly about what i was talking. Cheers Denis