I would like to store QGIS Map themes to be used later. In my workflow, I use a plugin which imports many (grouped) layers. Each layer contains several styles that I can switch between. I want to switch some of the styles of the layers, and then store these settings as a Map Theme. This works great, the problem is that next time I use the same plugin (in a new QGIS project), I don't have the Map Themes available as they are not stored.
I thought I found a solution to store the Map Themes using PyQGIS, but I can't get it to work. I found this thread: Export theme and its associated layers in xml file in PyQGIS
I add a Map Theme, and then run the following code
doc = QDomDocument()
mapThemesCollection = QgsProject.instance().mapThemeCollection()
print(mapThemesCollection.mapThemes()) # I see the names of my themes printed
mapThemesCollection.writeXml(doc)
The doc
QDomDocument still seems to be empty since doc.toString()
prints an empty string. Also, how do I save this as an XML file to be read later with mapThemesCollection.readXml(doc)
If there are other solutions to re-use themes within different projects I am also glad to hear about them.