I'm trying to create some layer visibility preset in QGIS 2.18 using its API. But, for some reason, when I try to add layers that are visible in legend interface, it simply doesn't work.
Here is the piece of the code that I'm refering to:
# Creating a preset
root = QgsProject.instance().layerTreeRoot()
clPreset = QgsProject.instance().visibilityPresetCollection()
clPreset.insert('MapaPrincipal', QgsVisibilityPresetCollection.PresetRecord())
# Adding visible layers to created preset
root = QgsProject.instance().layerTreeRoot()
for l in root.findLayers():
if not l.isVisible():
iface.legendInterface().setLayerVisible(root.findLayer(l).layer(), True)
clPreset.addVisibleLayersToPreset(root, clPreset.presetState('MapaPrincipal'))
When I check for the visible layers added to the visibility preset, it returns an empty list. What am I doing wrong?