Why customized method `Node.init` not called for already loaded nodes?

As an addon developer I would expect the method init be called when:

  • a node type has been registered in an add-on, and user loads a file with such nodes
  • user has loaded a file with custom nodes, and then enables addon that registers such node type

In practice (with CompositorNodeCustomGroup in 2.83), this doesn’t happen.

Is it a bug or a feature?

The API description says Initialize a new instance of this node. This only gets called for newly created nodes, not existing ones that are loaded.

1 Like

That quite makes sense.

However, this may result in unpleasant situation: when runtime code changes because if new version of addon or new iteration of development, the new code will try to use old structure.

Is there some way to deal with it?

You can run a function that removes old sockets and adds new ones, or even replaces old nodes completely.
As an example, this is how I do it in the LuxCore addon for Blender: https://github.com/LuxCoreRender/BlendLuxCore/blob/master/utils/compatibility.py

Note that it is not enough to call this function in a load_post handler or so on startup, because the user might link/append outdated nodes at any time even after the main .blend was loaded. So I run this compatibility function before the start of each render, but in your addon usecase you might want to call it at some other time, before evaluating your node tree.

Sure I can write such function.
But scanning all the nodes in all loaded files for nodes is definitely not a good idea.

On the other hand, the moment of creating rna for a node seems to be the perfect moment to do such work, or at least to check compatibility.