Notifications
Clear all

[Closed] Serializing data?

Hi,
Is there a way to serialize a variable in maxscript to a string that can be exported to a file? And then later retreived and un-serialized? Similar to what php can do.
I’d like to build a maxscript Struct, serialize it, store it, then retrieve it later and use its contents.

Any ideas?

7 Replies
1 Reply
(@jeff_hanna)
Joined: 10 months ago

Posts: 0

Could you write out each member of the struct to a key in an INI file? MAXScript has pretty robust INI file handling with setIniSetting() and getIniSetting(). You just need to cast each struct member as a string when you set it to the INI file.


  setIniSetting <filename> <section> <key> (MyStruct.FloatVar as string)
  

When you load it back in you could initialize your struct and then set each member with getIniSetting(). The data from the INI file will come in as a string so you’ll need to convert it to the proper format with the “as” keyword


  MyStruct.FloatVar = (getIniSetting <filename> <section> <key>) as float
  

Hello Scorpion,

This would be as simple as writing a custom exporter to export your data to a file (CSV file?) and then importing it again no?
It would heavily depend on the data. There are a myriad of different types of exporters and importers available at www.scriptspot.com

Maybe post a sample of the kind of struct you would like to export and read about filestream values in the MXS help.

J.

Hi, thanks for the ideas.
Regarding ini setting, its not a bad idea, and I may use it, its just that wont it have to access the file 100 times if i had 100 members (, and then 100 more times when retrieving each member)? With serializing (in php at least) you write the data once, then retrieve it all in 1 go, as 1 block of text, which you can unserialize and the parser will understand the variables as they were set.

I guess its a decent method though.

1 Reply
(@bobo)
Joined: 11 months ago

Posts: 0

While there is no dedicated method to serialize a struct, you could do what you described, it just needs a bit of love because you would have to deal with strings and backslashes in path names etc.

The idea is this:

You can use the getPropNames() method to get all properties in the struct, then iterate through them and write all to a text file, prefixing with the struct name.
Each line of the file would look like
myStruct.myProperty=SomeValue
In the case of strings, you will have to add quotes around the value, and in the case of backslashes replace them with double-backslashes, because if a string ends with a single backslash followed by a quotation mark, the parsing would fail as the rest of the file would be assumed inside the first quotation mark of that same string…

Once you have written that file, you have all values of all properties of the struct loadable with a single fileIn() call.

Obviously, the struct has to be GLOBAL because fileIn evaluates in global scope.

I had to do this 3 days ago and it worked for me…

Hi Bobo, sounds like a solid plan!
By making the struct global, do you mean an instance of the struct, or can you actually declare the struct as global?

Also, since you know more about maxscript than anyone else, (and I havent been able to PM you, full inbox), do you think you could look at my other thread http://forums.cgsociety.org/showthread.php?t=330852 and see if you know the cause of the problem? I think its quite an annoying bug in maxscript…

[EDIT]
Hmm, how would you get the values of the properties in the loop? Say it goes like this:


  mystruct = my_struct()
  names = getPropNames mystruct 
  for prop in names do
  (
  	format "%=%
" (prop as string) mystruct.($prop) -- dunno what to write here?
  )
  
1 Reply
(@bobo)
Joined: 11 months ago

Posts: 0

I meant the instance of the struct, since you will be loading into it using fileIn() which should be able to see the instance’s variable in the global scope in order to assign the values.

Haven’t used encrypting since Max 2, will see if I can find a second to try that out.

Oh, and btw I know at least 3 people who know more about MAXScript…

ah ok, i will try that.

Hehe, really? seeing as you ‘wrote the book’ (reference) on it quite literally, I assumed you might be quite knowledgable