ArcGIS Pro 3.2.1
If I understand correctly, a given layer in the Conents pane doesn't have a unique name in its properties.
For example, if I add two XY event layers to the map named SPECIES_RECORDS, then there isn't a unique name behind the scenes.
That causes problems when trying to reference a specific Contents layer using ArcPy. See: Refer to a specific Contents layer — even when there are layers with duplicate names.
Idea:
Could all layers in the Contents pane be automatically assigned a unique name property? I.e., in the Properties —> Source or General section, there would be a line called "Unique Name" and the values would be:
That would allow us to reference a specific Contents layer in Python tools and toolboxes, assuming that the new property is available to ArcPy.
This isn't my area of expertise, so let me know if I've misunderstood something.
@Bud have you tried grabbing the layer by its layer id?
I'm not sure what is going on in the python script you have, but if layers are being created in the script, you're also able to store the result of that function call in a variable so you can maintain the reference and use it as needed.
@SSWoodward Is the layer ID only available via CIM?
You are able to access it with the mapping module in arcpy. This thread has some good information you may find useful.
(edited)
I don't think using the Layer ID for tools like Selection to Definition Query is possible.
The CIM examples all refer to "layerid", but these are for ArcEnterprise feature services that actually do have a layer id in the map service. I cant find anything equivalent for the ArcGIS Pro TOC.
Additionally, the layer parameter in custom GP tools like our Selection to Definition Query tool disregards the index number in the code, so you can see we can have "layer" and "layer:0" in the dropdown, but when i pull that value from the dropdown, its always "layer" because the underlying parameter type is Layer.So I don't think the Layer ID helps us in this case.
The issue you are seeing with the script tool seems to be because the author is stripping out the text name of the input layer, and then finding the layer again by listing all of the layers and tables and searching for the layer in that list, using its non-unique string name.
When you pass a layer to the gp tool, you already have a reference to the layer. The linked script tool is getting the first result only because that is what it's asking for. Having duplicate names in a map won't prevent a script tool from pointing to the correct layer.
If possible, I'd advise an edit to the tool code.
Layers do have unique ids accessible through python, but I do not think this is the root of the issue in this case.
layers = arcpy.mp.ArcGISProject('current').activeMap.listLayers()
for layer in layers:
print(layer.name, layer.URI)
Thanks @SSWoodward
Reply:
I don’t think that’s quite right. For instance this code:
///
SelectionLayer = sys.argv[1]
arcpy.AddMessage(SelectionLayer.name)
///when executed yields the error:arcpy.AddMessage(SelectionLayer.name)
AttributeError: 'str' object has no attribute 'name'which indicates that the layer parameter from the tool, passed in by the dropwn: "sys.argv[1]' is a String type and not a Layer reference even though the parameter in the toolbox for the script is "Layer and Table".
Also, the code snippet that was provided doesn’t help us in this case because it returns the layers' internal url (.json) name and not its layer name in the TOC.If there was a way to get a true layer reference from the dropdown parameter, then we would be good.
Thanks for the feedback. Have your developer try using the arcpy.GetParameter methods instead of sys.argv. These are super handy, and should be the goto syntax when writing script tools.
When the parameter is fetched in this way it is a layer reference and not a string.
Thanks @SSWoodward !
That solved it. The script works the way we want it to now.
🎉 Yay! Super happy I could help.
Since the ability to pass specific layers into script tools, even when duplicate names exist, is present in the software, I'm going to close this Idea.
Thank you as always for your engagement with us and our software!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.