Find a specific layer?

893
6
Jump to solution
09-22-2022 06:33 AM
RandyMcGregor3
Occasional Contributor III

I have an arcpy (ArcGIS Pro) script to rename layers to their original name. It only has one argument - The layer to be renamed, which the user selects from the table of contents.

At first, I thought I could just enter the argument's name ("Input_Layer") then ".dataSource" but of course you get the error telling you a string has no function 'dataSource.'

Input_Layer = arcpy.GetParameterAsText(0)

So, I loop through the layers, then find the the layer that has the same name. The problem with this is that it is often the case that more than one layer has the same name, and the first one gets picked whether I want it to or not.

Is there a way to reliably find the exact layer that the user chose when he selected from the table of contents? 

Thank you,

Randy McGregor

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

I think you want GetParamter(), which will return the selected object.

View solution in original post

6 Replies
by Anonymous User
Not applicable

I think you want GetParamter(), which will return the selected object.

RandyMcGregor3
Occasional Contributor III

Thanks! Will try that.

0 Kudos
RandyMcGregor3
Occasional Contributor III

Wow. I was so used to GetParameterAsText() that I never even thought of what the 'AsText' means. Facepalm. GetParameter() returns the object and I can apply .dataSource to it no problem. 

Thank you!

0 Kudos
SamSzotkowski
New Contributor III

I don't think that catches every edge case, because you could potentially have two layers both the same name and the same data source in your Contents pane (if you make a copy of a layer within the same map and don't change anything).  Here is how I usually handle this: https://community.esri.com/t5/python-questions/how-to-access-layer-properties-from-a/m-p/1191018

If you want your tool to edit properties of Layer objects, using a GPFeatureLayer parameter in your GP tool doesn't let you edit the corresponding Layer object directly (you get a MappingLayerObject instead of a Layer).  So usually I put some code in updateMessages() to raise an error if the active map doesn't have service layer IDs enabled ("Allow assignment of unique numeric IDs..." in the map properties General tab), then my code in execute() or postExecute() grabs the layer's CIM definition to make sure its service layer ID matches the ID in the MappingLayerObject, then changes its title or whatever.

But if you don't care about the edge case where you have layers with the same name + data source (like if you're just trying to rename every layer in the map), then simply doing an example like here https://pro.arcgis.com/en/pro-app/latest/arcpy/geoprocessing_and_python/post-execution-validation-in... is fine

RandyMcGregor3
Occasional Contributor III

Yes, I've noticed some limitations. It did work for what I needed but sometimes the object returned won't let me do what I want. There are times when you need to cycle through the layers in the TOC and pick the right one and there just doesn't seem to be a straightforward way to do that .

0 Kudos
BlakeTerhune
MVP Regular Contributor

I can't find a way to see what layer is "selected" in the contents of the map. You can only build a list of layers and have the user choose it in the script tool parameter. Then, as @Anonymous User mentioned, you can get the layer object chosen in the tool parameter using GetParameter().