@clement @mikity_kogekoge
Without properly understanding the script here:
A thought that came to mind now is… why not trying to access the layerpanel and the control displaying the layertree?
https://developer.rhino3d.com/api/RhinoCommon/html/P_Rhino_UI_PanelIds_Layers.htm
I might give this a try, but maybe that’s not even possible?