0

Every single example I see online, the code has the mapname.listLayers() in order to call the layer.

Don't we have a way to call the layer by its name instead? The reason I'm asking this is because this code does not work at all:

aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.listMaps("Map")[0]
aprxMap.addDataFromPath(outfcname)    
install_layer = aprxMap.listLayers(installname)
layer_simbology = r"C:\ArcGIS\LAYER\symbologylayer.lyrx"
arcpy.ApplySymbologyFromLayer_management(install_layer, layer_simbology)
aprxMap.removeLayer(install_layer)
aprx.save()

The code does not update the symbol, neither remove the layer from the map. But add the Feature Class (outfcname) to map.

  • I'm having trouble following the code. You bring in "layer_simbology" to change the symbology of "install_layer", but then you remove "install_layer"? You might need to refresh the map after applying, or add it back to the map? – dslamb Sep 24 at 20:11
  • The removeLayer part was just for troubleshooting. I noticed that the Applysimbology is not working. So I added the removeLayer, and saw that the layer is not being removed as well. With that i got the conclusion that the issue is how I'm calling the layer. – Gustavo Sep 24 at 20:19
3

aprxMap.listLayers() will return a list even if there is only one layer that matches the wildcard you passed. You need to index the 0 position in the list before assigning it to install_layer.

install_layer = aprxMap.listLayers(installname)[0]

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.