[Closed] How to Read/SAve File contents
Hi all. i’ve been out of here for a long time, i’m back
I have a question:
Is there any way to read the content of a txt file (or whatever is the extension), paste it inside the custom properties of a dummy, and after that, when you push a button (or do something) take the custom propierties of that dummy and re-save as a text file (or whatever extension)
Thanks!!
Check out “FileStream Values” in the reference for ways to open/edit/write text files in maxscript.
newTXT = createFile "C:\\TEMP\ est.txt"
format "value X: %
" $.pos.x to:newTXT
format "value Y: %
" $.pos.y to:newTXT
format "value Z: %
" $.pos.z to:newTXT
close newTXT
ShellLaunch "C:\\TEMP\ est.txt" ""
I’m looking at it right now, sorry, i think i posted the question too fast
But what i want is to read the content of a file and paste it into the custom properties section of a dummy, and later take that custom properties and paste “as is” inside another file.
It’s like a copy paste operation, but i’m holding the information inside the custom properties of a dummy, so if i want to save that information within the scene i can do it.
Also i’m looking at the documnetation, but if you know some way and don’t bother you to post it, please do it
Really thanks!
Cheers.
EDIT 1: Maybe i understand your method, need to test, thanks!
I have a problem with the read value method, all that i want is to read ALL the content of a file, it has some strange characters, so i want to paste all the file inside the user defined properties, as a kind of notepad, then paste all the user defined properties inside another file.
I think i know how to paste all the proerties into a file, but i don’t get to read the file, because i don’t want to evaluate all, i want the content readed as a string.
Cheers.
Ok…i cannot find a solution for what i want.
I want to read a render preset and store it inside the scene, i thought i can store it inside user defined properties of a dummy, but when i do that i think i’m cracking the preset, so when i paste it as a preset again it does not work.
Some ideas?
The main point is to store render presets within the scene, without any additional file.
Some thoughts?
Cheers.
instead of using cust attributes it’s better to use appData in your case. The AppData stores with file.
fn fileToAppData node file:"" channel:30 = if iskindof node Node and doesfileexist file do
(
if (file_ss = openfile file) != undefined do
(
data_ss = stringstream ""
while not eof file_ss do format "%
" (readline file_ss) to:data_ss
flush file_ss
close file_ss
setAppData node channel data_ss
ok
)
)
fn appDataToFile node file:"" channel:30 = if iskindof node Node and getAppData node channel != undefined do
(
if (file_ss = createfile file) != undefined do
(
while not eof data_ss do format "%
" (readline data_ss) to:file_ss
flush file_ss
close file_ss
ok
)
)
I read about that in the maxscript reference, and i canot finish to understand your code very well, but indeed this is exactly what i need
Can you explain that a bit please
And thanks!
i gave you two functions: “read from file and write to appData” and “read from appData and write to file”. To use them you have to define the node, filename, and channel index…
it’s better to use appData because appData is string stream… StringStream has same methods as FileStream…
you can get more info about AppData in MXS help (Access to MAXWrapper AppData)
fn fileToAppData node file:"" channel:30 = if iskindof node Node and doesfileexist file do
(
if (file_ss = openfile file) != undefined do -- open file for READ and check if it can be READ
(
data_ss = stringstream "" -- create empty String Stream to write to
-- read every line of the file, write to string stream, and check the end of the file
while not eof file_ss do format "%
" (readline file_ss) to:data_ss
-- close file stream
flush file_ss
close file_ss
-- set node's app data in channel 30 (you can use any from 1 to 256 (maybe higher, i didn't check))
setAppData node channel data_ss
ok
)
)
fn appDataToFile node file:"" channel:30 = if iskindof node Node and getAppData node channel != undefined do -- check if app data in channel 30 exists
(
if (file_ss = createfile file) != undefined do -- open file for WRITE and check if it can be WRITTEN
(
-- read every line of the data, write to file stream, and check the end of the app data
while not eof data_ss do format "%
" (readline data_ss) to:file_ss
-- close file stream
flush file_ss
close file_ss
ok
)
)
Thanks denisT.
I think that is exactly what i need! I’m going to check the new commented code.
Thanks again!
I’m looking into this, and it don’t seem to do what i expected.
Let’s work with some real file.
What i have in mind is to take a render preset file (rps) and store it inside an object (a dummy, or something) inside a channel, just like you said.
But this code don’t seem to this, it stores a line, not the whole file.
Some clue about this or some alternative solution?
Cheers.