Hello,
I'm encountering an issue with my Python script (using the standard libraries packaged within Pro 3.0.0 and Pro 3.0.1 installs).
My goal has been to update some symbology within my Pro project. To do this, I locate all values that are not part of our symbology rendering, and simply add them into the existing renderer group. Using the .listMissingValues() functionality, I am able to locate these, add them with .addValues(), and this accomplishes the task successfully. Hooray! However, the problem lies in scenarios where there are 0 missing values. If the script does not find any missing values, the tool fails and produces this error:
Traceback (most recent call last):
File "C:\My\Python\Path\To\Script.py", line 27, in <module>
list_missing_values = layer_symbology.renderer.listMissingValues()
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_renderer.py", line 41, in listMissingValues
self._arc_object.listMissingValues(*gp_fixargs((), True)))
SystemError: <built-in method listMissingValues of MappingSymbologyObject object at 0x00000172C4BAA9B0> returned NULL without setting an error
Is the .listMissingValues() method not designed to handle scenarios with 0 missing values, or am I missing something obvious? I assumed it would just spit out an empty list if it couldn't find any missing values, but that doesn't seem to be the case. I could band-aid this with a Try/Except to ignore SystemError messages for the .listMissingValues() step, but I would prefer not to do that.
Anyway, thanks for any assistance!
Here is my script thus far (when there are 0 missing values, the error mentioned above occurs at Line 27):
import arcpy
aprx = arcpy.mp.ArcGISProject("The\\Project\\Path\\Being\\Used.aprx")
for a_map in aprx.listMaps():
if a_map.name != "Name of Map I am Needing":
continue
else:
print("Map Name: " + a_map.name)
for a_layer in a_map.listLayers():
if a_layer.name != "Layer Name I am Needing":
continue
else:
print("Layer Name: " + a_layer.name)
layer_symbology = a_layer.symbology
list_missing_values = layer_symbology.renderer.listMissingValues()
if len(layer_symbology.renderer.groups) != 1:
print("Group count error, please check symbology groups.")
else:
print("Symbology Group Header: " + str(layer_symbology.renderer.groups[0].heading))
if len(list_missing_values) == 0:
print("No missing values found.")
else:
for each_missing_value in list_missing_values:
print("Missing value count: " + str(len(each_missing_value.items)))
layer_symbology.renderer.addValues({layer_symbology.renderer.groups[0].heading: each_missing_value.items})
a_layer.symbology = layer_symbology
aprx.save()
print("Project saved.")