PDA

View Full Version : Serializing data?


scorpion007
03-19-2006, 04:26 AM
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?

j-man
03-20-2006, 11:38 AM
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 (http://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.

Jeff_hanna
03-20-2006, 01:54 PM
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?

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

scorpion007
03-20-2006, 11:06 PM
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.

Bobo
03-21-2006, 12:13 AM
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.

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...

scorpion007
03-21-2006, 12:23 AM
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 "%=%\n" (prop as string) mystruct.($prop) -- dunno what to write here?
)

Bobo
03-21-2006, 12:30 AM
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...

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... :)

scorpion007
03-21-2006, 12:32 AM
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 :)

CGTalk Moderation
03-21-2006, 12:32 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.