I'm currently using "mapLayersByName" successfully to set one active layer at a time as follows...

vl = QgsMapLayerRegistry.instance().mapLayersByName('My_Layer')[0]

Can an instance of multiple layers be set using the "mapLayersByName" method?

I have tried...

vl = QgsMapLayerRegistry.instance().mapLayersByName('My_First_Layer', 'My_Second_Layer')[0]

As you can see my end goal in this case would be to set the layers within that instance of MULTIPLE layers (vl) as the active layers using the "iface.setActiveLayer()" method. I notice this method is not plural (i.e. not "setActiveLayers()"). Significant?

  • y que otro metodo me permitiria seleccionar una capa por nombre o como podria terminar la instancia registro para en otro metodo volver a instanciar y buscar una capa diferente translated: > and that another method would allow me to select a layer by name or > how the registration instance could end up in another method to > re-instantiate and search for a different layer Apr 26, 2018 at 17:04

1 Answer 1


No you can't. mapLayersByName returns a list of layers matching a single name (you can have many layers with the same name)

setActiveLayer takes a single active layer because there is only one active layer at a time in QGIS.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.