Hey all!
Just a quick mini tutorial/blurb about using JSON. In the first version of the Animation and Rigging Toolkit, I used cPickle, because I didn't know any better :) If you're unfamiliar with cPickle and JSON, they are both Python modules (in this case) that allow you to write out Python data, like lists or dictionaries, and have them be read back in as that data type, instead of the usual string you'd get back from file.readlines().
There are a few reasons to switch to json over cPickle. First, as I learned with the tools, cPickle and source control do not mix well. cPickle seems to be very dependent on its new line/line ending characters staying intact, and Perforce would corrupt this by changing the line endings in the file, unless you set your line endings setting in P4 to use something like 'unix' instead of 'universal line endings'. This is somewhat okay in a studio setting, it's an easy enough thing to control, but when tools are mass distributed, this can become a nightmare. The other downer of cPickle is that the file is not human readable. Depending on what you're trying to save out, this might be acceptable. I found for saving out poses or templates or animation data though, that being able to open, read, and edit the data inside the file would have been mighty useful!
So, onto JSON! The functions for both are very similar, and they do very similar things, but JSON seems to be more forgiving with not getting corrupted when using source control, and it's human readable, so at the very least, one could edit the file to fix any possible issues.
To use json to write out a list of data, first I created the list I wanted to save out, then:
I use maya's fileDialog2 command to grab the name of the file the user wants to save, create that file (or open if it exists), and then use json.dump(data, f) to dump my list (data) into my file (f). If you're wondering what "*.template" is, with text files you can save with any arbitrary file extension you want. In this case, since I was saving out data for a rig template, I made a template file extension.
Here's a snippet of what that file looks like if I open it in wordpad:
To load this data back in as a list that python understands, you can simply use the following:
Super short and sweet. Again, using Maya's fileDialog2 command to get the file to open from the user, then opening the file, using json.load() on that file (storing into the variable data), and then closing the file! Now you can iterate over the list, data, to get whatever it was you needed(in this case, template information).
If you've looked at the json documentation, you'll notice there are two load functions: load() and loads(). The load() function is what you want to use if you are passing in an actual file, whereas the loads() function is what you'd use to pass in a string/unicode. All of the examples I had seen online before looking that up were using loads(), but they weren't actually passing in a file object, so when I tried using it, it would error out!
That's pretty much it! I hope someone finds this useful, as it's now my go to module for saving out data!