I am working on a script tool to be run inside of an ArcGIS Pro project. Part of the script is meant to check/set the visibility of layers in a map by comparing to a pre-existing list of layer names which should be present and visible in the map. The code is meant to look something like this:
import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
map0 = aprx.listMaps()[0]
layers = map0.listLayers()
for lyr in layers:
if lyr.name in listBase:
lyr.visible = True
listBase is a Python list object containing strings representing the names of layers which should be present in the map. Pretty simple stuff: just check to see if a layer's name is in the list, and if so make the layer visible.
The issue that I am encountering is that the code fails on certain annotation subtype layers (annotation classes), resulting in the following error message when run in the Python window:
Traceback (most recent call last):
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py", line 90, in _get
return convertArcObjectToPythonObject(getattr(self._arc_object, attr_name))
AttributeError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 6, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\_base.py", line 96, in _get
(attr_name, self.__class__.__name__))
NameError: The attribute 'name' is not supported on this instance of Layer.
The code is able to iterate through the first few annotation classes without issue, but tells me that the 'name' attribute is not supported on others. Using layer.supports(attribute) statements, I can see that these layers also do not support "VISIBLE", "MAXTHRESHOLD" or "MINTHRESHOLD". This is really strange for a number of reasons that I will do my best to articulate now:
1. All of the layers which raise the above error are subtypes within an annotation feature class (called Transportation_MainMap). Each subtype represents a different annotation class. All of these classes appear in the TOC with their proper names, and visibility can be toggled from the TOC as well. Min/max thresholds can be set from the layer properties dialogue. The code runs fine on some, fails on others. EDIT: upon some further examination of where the loop fails, I figured out that the code works only for the first 10 iterations of the for-loop. If an annotation feature class has less than 10 subclasses no errors are raised, and every annotation layer beyond the 10th raises an error. There appears to be more than just this one issue at play here, as some annotation sublayers raise an error even if they are in the first 10 iterations.
2. If I use the Python window in Pro to print the longName attribute (i.e. <for lyr in layers: print (lyr.longName)>) the layers which do not support 'name' print a blank string rather than failing. Additionally, some of the layers which DO support 'name' have a mismatch with the layer's longName. For example the layer with 'name' == "Landing Strip" has longName == "Transportation_MainMap\County Line Road".
3. If I use the Python window in ArcMap no error occurs and there is no mismatch between the 'name' and 'longName' attributes for any layers. Seems buggy to me.
If anyone knows what it going on here I'd really appreciate some help! I'm also happy to provide more info if I haven't covered everything relevant in this post.