See for instance: How to collect the global mesh without writing a file - #4 by dokken for some inspiration for this.