::serialization fun!::

October 29th, 2011 by hamish download the zooToolBox

I’ve been tinkering around with this little project for awhile now.  I’ve always wanted to try to write a simple little serialization format.  I know there solutions already out there – and in fact pyYAML seems to do everything mine does WAY better than I’ve managed.

But thats not the point.  The point is I wanted to try and do it myself as an exercise. Its kinda tricky writing a nice serialization format and it was actually a fun little project.

Anyway, so what does it do?  Well, I wanted a text based serialization format that was easy to read and on the odd occasion hand edit.  You could write them by hand too if you’re feeling masochistic, but I strongly discourage it.  Having an easily readable, text based serialization format is terribly helpful for the occasions that you just want to crack the data open in an editor and see what the hell is going on.

Of course, you want to be able to write out interesting data too.  Which is obviously why pickle is incredibly appealing.  But pickle files are basically impossible for us mere mortals to read.  Plus they can also be abused by bad programmers (bad meaning incompetent or malicious).  I had a case recently where one of our tools was being hellishly slow.  So I spent a heap of time in a profiler trying to figure out why.  Turned out this particular tool, which was storing out data in pickle files, was pickling a database abstraction object.  The mere act of unpickling the file would cause a bunch of database activity.  In this particular case the tool was unpickling lots of these files which ended up taking a long time.  It took awhile to even bother looking at file loading as the culprit because the files themselves were only a few hundred bytes.

This is an easy mistake to make inadvertently in a language like python.

Pickle is amazingly cool.  Its also a VERY powerful tool to put in the hands of noobs and can sometimes cause headaches.

Anyway enough talking, lets look at some codez!

import os
from maya import cmds
import sobject

objToSerialize = sobject.SObject()
objToSerialize.sceneName = cmds.file( q=True, sn=True )
objToSerialize.currentSelection = cmds.ls( sl=True )
objToSerialize.someRandomDict = { 'data': os.getenv( 'USER' ), 'cwd': os.getcwd() }
objToSerialize.subObj = subObj = sobject.SObject()
subObj.interestingNumbers = range(3)
subObj.cyclicReference = objToSerialize

Kinda pointless data, but basically the objToSerialize object will be able to serialize itself to disk in a nice human readable way. The above code serialized to disk looks like this:

42035216:
	sceneName(str):c:/somePath/someFile.ma
	currentSelection(list):
		(str):pSphere1
		(str):pCube1
	someRandomDict(dict):
		(str)data(NoneType):None
		(str)cwd(str):c:\Users\macaronikazoo\Documents\maya\scripts
	subObj(*):42035376:
		interestingNumbers(list):
			(int):0
			(int):1
			(int):2
		cyclicReference(*):42035216

Thats fairly readable, right? Hardly a Dan Brown novel, but certainly easy enough to digest should you need to go poking around. And obviously if you need to get this information back into python, its as simple as passing either the string data to the Unserialize class method, or passing a filepath to the Load classmethod. Like so:

objToSerialize.write( '~/sobject_test_file.txt' )
unserializedFromFile = sobject.Load( '~/sobject_test_file.txt' )  #unserialize from file
assert objToSerialize == unserializedFromFile

Anyway – if you’re interested in taking a look at the code, feel free to! Its right here.

Share

This post is public domain

This entry was posted on Saturday, October 29th, 2011 at 16:16 and is filed under main. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.