I have made a python addin button which removes layers that are turned off in an MXD. The strange thing is that I get different results depending on whether I run the python commands in the python window vs my actual addin button.
In my addin class, the code looks like this:
def onClick(self): mxd = arcpy.mapping.MapDocument("CURRENT") layerList = arcpy.mapping.ListLayers(mxd) for layer in layerList: if layer.visible == False: arcpy.Delete_management(layer) arcpy.RefreshTOC()
So, this works fine in the python window but doesn't do anything in the button. I assume because it's trying to delete a layer object instead of the name. I have made a workaround which changes the following line:
Now this actually works. But there is a caveat, if I have multiple layers with the same name but different visibility settings, it may delete the one that is visible. My second workaround is to change the layer name before I delete it.
My final code looks like this:
def onClick(self): mxd = arcpy.mapping.MapDocument("CURRENT") layerList = arcpy.mapping.ListLayers(mxd) for layer in layerList: if layer.visible == False: layer.name += '_' arcpy.Delete_management(layer.name) arcpy.RefreshTOC()
By changing the name of the layer, I'm ensuring that I only delete invisible layers. But I'm wondering if this is the best way to go about doing this.
My question is, is there a better way to remove layers that are not visible from an MXD? Especially when they have the same name?