I have created a QGIS plugin and I am using the following code to get my layers by name from the layer panel on QGIS.
Here is the simplified code:
layer1 = QgsMapLayerRegistry.instance().mapLayersByName("layer1")[0]
layer2 = QgsMapLayerRegistry.instance().mapLayersByName("layer2")[0]
layer3 = QgsMapLayerRegistry.instance().mapLayersByName("layer3")[0]
But if any of these three layers are missing, then my plugin crashes with the error:
out of index
because it cannot find the missing layer.
How can I create a condition to stop the code from crashing every time there is a missing layer, and instead just show me a simple message e.g.: layer1 missing
from the layer panel?
iface.messageBar().pushWarning( u'ERROR: ', 'layer(x) missing from layer panel' )
in the except block to show up a warning message if a layer doesn't match :)