Any way to save and restore baked LigthmapGI data for generated meshes?

Godot Version

4.7

Question

I have a custom node that extends Node3D and has a hidden child that is a MeshInstance3D. The node generates a mesh which is then put on the child MeshInstance3D. This generated mesh object includes UV2 coordinates, so it is possible to use it for baking lightmaps.

This works fine until I have to save and restore the file. When the scene reloads, the child mesh instance has it’s mesh object recreated, which doesn’t include the baked data from the LightmapGI.

There does seem to be a work around where I create the child MeshInstance3D in my object’s _init() method and then later force the LightmapGI to refresh by caching it’s light_data parameter, setting it to null and then setting it back to its old value again. But this seems like a poor solution, and I was wondering if there might be a better way to restore the cached lightmap data when I rebuild my mesh?

The LightmapData only contains the NodePath to the MeshInstance3D node. If you create the MeshInstance3D at runtime it needs to be done before the LightmapGI node is added to the tree and it must have the same name (aka same NodePath) as the one you used when baking it.

Assigning the LightmapGI.light_data again after your scene is ready as you found already should do the trick. Or removing and adding the LightmapGI node after the scene is ready.