removeNode vs detach vs destroy?

Hi all again,
I’m very confused.
I tried searching on the forum and reference, but I did not understand anything anyway.

Can anyone tell me what is the difference, also with some example?

thanks all

This page offers some information, at least (under the heading “Reparenting nodes and models”).

In short, and if I’m not much mistaken:

“detach()” simply removes the NodePath from the scene-graph; the NodePath retains all of its data, and could, for example, be reattached later, or elsewhere in the graph.

“removeNode()” not only removes the NodePath from the graph but also cleans up its contents and memory; it’s intended to be used when you’re completely done with the NodePath.

I don’t see a “destroy” method in NodePath - is it defined elsewhere?

This topic interest me.

Suppose you detach a NodePath, and no pointers of this node remain at all in the user code, will it also be removed ?

I don’t want to do any definitive deleting in my API. I’d like to keep my options open as much as possible and as easily as possible. So it would be a really good news if NodePath were garbage collected like that :slight_smile: !

destroy() is for directGUI elements.

most of the time you want to remove a nodepath when you no longer need it. should you need it again, panda automatically caches loading of models so it’s usualy fast enough to simply load stuff again.

use detatch when you know that you will need to add this node again.

This is not quite true. removeNode() does not clean up any memory. In fact, removeNode() and detachNode() are almost identical.
detachNode() removes the node from the parent’s list of children, meaning, the node will be detached from the scene graph. The extra thing that removeNode does is that it makes the NodePath no longer point to that specific node (node() will return None after a call to removeNode()), thus essentially reducing the reference count of the node by one.

Functionally, removeNode is almost equivalent to this:

nodePath.detachNode()
nodePath = None

Only when the reference count of the underlying node reaches zero will the node be destroyed from memory, which is when there are no more references to it left, be it by its parent, your NodePath or by some other reference you hold to it.

So, in summary:
if I want to delete all data of a “directgui” object is sufficent to use destroy() without having to
use removeNode() ?

if instead I have two instances of the same pandaNode o nodePath object as:

nodePath2 = nodePath1

I can use:

nodePath1.removeNode ()

and nodePath2 still exist?
If I use detachNode() on both instances, the data will remain in memory forever?
The “garbage collector” can automatically handle this possibility?

Is all right?
thanks all.

Ah, that was perhaps a silly mistake on my part - thank you for the clarification!

Well, until all references to the node are removed, I imagine. I don’t know, however, whether there are internal references that might cause it to hang around until program completion, however, so I’ll leave a more definitive answer to another, I think.

You cannot actively delete data in memory. You can however clean up references to it, which will result in the data being deallocated from memory automatically. In the case of DirectGui, destroy() should be sufficient to clean up any internal references (assuming you drop the reference afterward), I believe it actually calls removeNode() under the hood.

No, because a NodePath is not a PandaNode. In this case, nodePath1 and nodePath2 will point to the same NodePath object, so you’re cleaning up the same reference.

If you made a copy of the NodePath, using this:

nodePath2 = NodePath(nodePath1)

then it would be true what you said, destroying one nodePath will keep the other intact and therefore the existence of the underlying PandaNode would be maintained. Note that this would not copy the underlying PandaNode, both NodePaths will still refer to the same PandaNode which will only get destroyed after both node paths (and any other references) are gone…

If you use detachNode on both instances, the data will remain in memory as long as you still hold any reference to it. detachNode insures that the scene graph no longer references it, but if you want the object to be destroyed, you should also make sure that you don’t hold any more references to it.