Copying Node Objects with Python's Copy Module


I didn’t see an easy answer to this on the forums. I’m building a game as a hobby with Panda3d to learn more about coding; my render process is very clearly removed from the internal game and physics logic (which is handled by the Grid object). My Grid object is passed to the Scene object, which inherits from ShowBase, and is responsible for (as part of its initialization procedure) ‘building’ the scenegraph with the information supplied in all the objects inside the Grid object.

Basically, I’m trying to find a more efficient means to store the data that tells the Scene object what to load into the render tree (or the aspect2d tree!). Right now, I have a pretty crappy ‘special case’ system, where the Scene object looks at each individual object inside the Grid, reads its data, then makes some quick and dirty decisions about what type of node it should be loading (textnode? a model object? a card from the cardgenerator?).

One of my initial solutions to this was to create multiple Grid objects, each responsible for a different type of graphic (ie, one Grid handles all the objects that are represented by cubes; another handles all the objects that are textnodes). But there’s a lot of inefficiency there, and it makes flexibility hard (what if I decide I want cubes and spheres? I have to add a new Grid object to handle spheres, and figure out how it interacts with the Grid object that handles cubes).

Part of the problem I’m encountering is that I can’t ‘preload’ models outside of the ShowBase (I’ve experimented with trying to pull the loader attribute out of ShowBase, but with limited success), so I can’t ‘preload’ these things into the objects on the Grid before it even arrives at the Scene object.

One of my current ‘workarounds’ for this is to use Python’s copy module to create ‘shallow copies’ of models and nodes I expect to be using (textnodes, cards, cubes…). Preload each possible unattached Node, bring in the game object, then make a shallow copy of that Node and stick it inside the game object. Then attach that shallow copy to the render or aspect2d grid.

My concern here is that I don’t know much about the Node objects and what the consequence of using Python’s copy function might be with them. Is a shallow copy going to cause problems? Is it going to be crucially inefficient? Is there a better way to ‘preload’ nodes so I don’t have to keep making special cases for them when it comes time to build the SceneGraph at startup?

(Keep in mind, as part of this situation, I don’t need a /lot/ of different objects–my game is very simple, and uses very primitive objects like cubes, cards, and text!)

Any and all help is appreciated. If the above is a confusing mess and you have no idea what I’m talking about, feel free to say so and I’ll try to phrase it better! Keep in mind, I’m a complete newbie when it comes to programming.

(As a note, I’m not even sure if what I’m describing is a serious problem–my code works right now, I just dislike it aesthetically because it uses a ‘special case’ system to make important decisions, and I’d rather this decision be more streamlined)

Panda’s nodes support Python’s copy and deepcopy mechanisms, as of recent versions of Panda3D.