Serializing the Scenegraph in Python

I have grepped around the fora and found all kinds of snippets, but I have not found something I can hold onto.

Here’s the thing:
I want to write an animation editor. Set keyframes, begin end, goto; that sort of thing. Right now I am in the gathering-info stage to see if I can even hope to do this in Panda.

The SG is a tree already. I want to hang my own stuff off of each node. My plan is to subclass PandaNode as ‘dnode’.
Each of my dnodes will have rich information I need to save out when the app closes (or the user saves their work.)

As the user works, so the dnodes will change content - this is dynamic. The final structure will be a fairly deep tree of dnodes.

How would I save/snapshot this tree (the SG) to a file and how would I restore it?

I want to avoid having to write my own SG walker (I don’t grok recursion at the best of times) so I am hoping there is a “batteries included” solution.

Any clues? Any suggestions for the structure.
\d

If you are subclassing PandaNode, you will be working in C++. (You can subclass PandaNode using Python, but your new node type won’t actually be saved in the scene graph. See “Subclassing” in the manual.) If you would rather work in Python, you can hang dynamic content on a node using nodePath.setPythonTag(), and this has the advantage that it also works on nodes that are already subclassed from PandaNode, like GeomNode or ModelNode.

In any case, you can save an entire tree of nodes with nodePath.writeBamFile(‘myfile.bam’), and read it in again by loading the bam file. This does, of course, write a bam file, which is somewhat version-dependent, so it may not be the best way to save the results of someone’s level editing for posterity. But it may be acceptable, depending on your needs.

For other approaches, you’ll have to work out something on your own.

Edit: I just realized that the “python tags” described with setPythonTag() are not saved in the bam file with nodePath.writeBamFile(). Instead, you’ll have to use the ordinary setTag(), which are. These only store strings, though, so you’ll have to format all of your auxiliary data as strings. The pickle module can make this easier for you if you want to store things like dictionaries and classes.

David

I see. All you said confirms what I have read up to now. I don’t want to have trouble with BAM file version issues, so I guess I am sol. I will have to walk the tree for save loads after all. Are there any snippets showing the basics of walking?

\d

It’s not difficult:

def walkGraph(node):
    print node
    for child in node.getChildren():
        walkGraph(child)

David

Great, thanks David. I wish my brain was more reliable, but I can’t hack the wetware :smiley:

\d