Hi,
If I understood your case correctly, this post 46 might be able to help. The issue with the snippet is that it has not been extended to support hierarchical layers but it won’t be hard to extend.
Bests