I am attempting to build a model to test whether a layer exists in the current map, and if true, run the command ApplySymbologyFromLayer on that particular layer as follows:
The 'If Data Exists' doesn't seem to be the right tool to test whether a layer exists and the tool runs with an error:
Failed to execute. Parameters are not valid.
ERROR 000735: Input Values: Value is required
Failed to execute (13UGBLYR).
What would be the correct tool to use to check whether a layer exists in the current map?
Hi Leandra,
You can either do this by developing a custom script tool that checks the layer for you and then returns an output that you can evaluate....or by using the Calculate Value tool with a custom Python function and then check the output value of that tool with the If Value Is tool to create the split branch you had in your model above:
Here's how to configure the Calculate Values tool.
Expression Field:
CheckLayer("%Test Layer%")
Note that the "Test layer" is referring to my layer variable in my model that I am checking for existence. You would need to change in your model to:
CheckLayer("%UGB_Layer%")
Code Block field:
def CheckLayer(layer):
try:
import arcpy
# current ArcGIS Pro project
aprx = arcpy.mp.ArcGISProject("CURRENT")
# current open map
map = aprx.activeMap
#check to see if the layer is listed in map
if layer in map.listLayers(layer):
return "PRESENT"
else:
return "NOTPRESENT"
except:
return "NOTPRESENT"
Make sure to set the Data type to String
Set the output of the Calculate Value to be the input Value to check.
Keep the default value for the Value Test "Is equal to at least one value"
Set the Comparison Type to "Case Insensitive string"
Values - you only need to type in PRESENT. This is important because you only want to test for the "True" branch condition. Any other value (i.e. NOTPRESENT) would trigger the false branch.
This should do what you need.
Thanks Andrew, I'll give that a go. I'm trying to get around a Python bug with Apply Symbology so it would either be run the model in the Python Script or run some Python in the Model 😄
In the end I used the iterate Layers and set the wildcard to the layer name itself.
Slightly clumsy but it will suffice until the Apply Symbology from Layer bug is fixed in Python Toolboxes (and Jupyter Python as well!)
<rant>As an aside it seems that anything I try and do in Python to manipulate layers (moving, showing/hiding popups, changing symbology) just doesn't work in scripts so I'm going to try to do all that in model building becuase at least it seems to do 'what it says on the box'</rant>