How to use Undo/Redo with StaticMeshInstances?

currently I am trying to implement Undo/Redo for a custom editor mode that places StaticMeshInstances but I am struggling to get it working. What we want to do is similar to the foliage tool:

  • mouse down → start new transaction and start painting static mesh instances
  • mouse up: stop painting, end transaction

But I can’t get the instances to behave correctly on undo/redo. When I undo one of these transactions, all static mesh instances that were painted this way disappear (even if their component was not affected by this transaction). However, they are still listed in the InstancedStaticMeshComponents they were created in, they are just not rendered. Sometimes when I paint again after an undo, the newly painted instances are displayed again, sometimes they are not. I have not found a reason for this yet.
Doing a Redo after an Undo doesn’t bring the meshes back.

As the static mesh instances work for the foliage renderer, I am clearly missing something.
I tried several combinations when to call Modify() (before or after the changes) and on which objects (always calling Modify on the actor, calling it on the components does not change anything), but nothing works. I compared my code to the foliage tool but could not see any significant difference.

Edit: I just noticed that I have exactly the same problem with decals that we paint in the same manner (as DecalComponents added to our actor).

Found it accidentally!

There is some sneaky code hidden in AInstancedFoliageActor::PostEditUndo() that “reapplies” the instances to the component after an undo/redo operation.
With that code working I also cleared my confusion about the time to call Modify(). It should be called before the change.

This actually does not solve my problem with the decals but I suppose there is similar code somewhere to achieve the same.