Notifications
Clear all

[Closed] How to Read/SAve File contents

Really thanks denisT, that’s the reason why that worked fine with a test file, but no with the presets, i tried binstream method, but i’m not sure if i did it fine.

Thanks again.

how to read and write binary (rps) files:


fn loadPresetFile buffer:undefined filename: =
(
 if filename == unsupplied do filename = getOpenFilename caption:"Pick a preset file" types:"Render Preset File (*.rps)|*.rps"
 if filename != undefined do
 (
  local ss = fopen filename "rb"
  if ss != undefined do 
  (
   buffer = #()
   while (val = ReadByte ss #unsigned) != undefined and not keyboard.escpressed do
   (
	append buffer val
   )
   fflush ss
   fclose ss
  )
 )
 buffer
)
fn savePresetFile buffer:undefined filename: = if iskindof buffer Array do 
(
 if filename == unsupplied do filename = getSaveFilename caption:"Pick a preset file" types:"Render Preset File (*.rps)|*.rps"
 if filename != undefined do
 (
  local ss = fopen filename "wb"
  if ss != undefined do 
  (
   for b in buffer do WriteByte ss b
   fflush ss
   fclose ss
  )
 )
 filename
)

the best place to save a binary buffer is some persistent global … (IMHO)
if you want to save buffer with any node use #intTab cust attribute with variable tab size…

why not a INI file, with format to:<thefile> and execute to run the attributes ??

maybe Pete(LoneRobot) could show us some examples using XML to do that.
=)

2 Replies
(@marcobrunetta)
Joined: 11 months ago

Posts: 0

Because you’d still have to send the INI along with the file and that’s not what he wants.

(@denist)
Joined: 11 months ago

Posts: 0

how do you want to use INI file? To store a link to the preset file? or to store byte array itself?

as I understood CerberusC wants to store everything about preset file inside max file (not outside)

Yes, that’s it, so i can have multiple presets within the file, if i save the file, reinstall my pc, and reopen the file, my presets will remain untouched, and there will be only one file at all, the max file itself.

About the persistent global variables, can i have as much persisten variables as i want or this can be a problem?

And about the #intTab, in the documentation is not very clear, but i will search in the forum to see what i find.

Thanks for the help, tomorrow i’ll test it, thanks!

you can declare as many persistent globals as you want… but if you want to have them many it’s better to organize a structure of them… i don’t like to declare too many globals.

Hey!

I’m back after a weekend of hard work

I plan to test your code tomorrow, but i’m tempted to ask you a thing about what you told me:

when you say “organize a structure of them”(variables), what do you mean, my objective is to create as many variables as render presets i have, so if i have 10 presets i’ll create 10 variables, if i have 150 presets…well you understand

So whan you mean to organize an structure what do you men, i have in mind to use some prefix to make my variables nearly to uniques so they don’t interfere with other scripts/plugins, specially because are global.

So are you referring to this or is something else?

Thanks denisT you are being so helpfull, i think you really honor your “Sr. Technical Artist” title

Cheers!

what do need to store? Name of preset, filename (if so), and data (array of bytes).


-- define a structure:
struct RenderPresetStruct (name, filename, data = #())
-- declare a persistent global
persistent global MyRenderPresets = #()
-- collect all presets in this array as RenderPresetStruct value
-- pseudo code:
MyRenderPresets = for f in presetsFiles where (d = getPresetData f) != undefined collect 
(
 RenderPresetStruct name:(getfilenamefile f) filename:f data:d
)  

 

something like that…

Ok, the code for saving/loading binary files is working like a charm, now i encountered another problem.

I have the name of the desired variable stored in other variable, let’s say this:

variablename = “vairable_1”

so when i’m going to define the persisten variable i need to say:

persistent global vairable_1 = #()

but the name is stored inside a variable, so is there a way to do this?

Cheers and thanks for everything.

you could use execute:

execute (“persistent global “+variablename+” = #()”)

Even though it’s not really good practice, sometimes there’s no way around it.

Why is not a good practice Marco?

Thanks

Page 3 / 5