AnsweredAssumed Answered

layer object vs. layer name

Question asked by griner on Oct 5, 2017
Latest reply on Feb 7, 2018 by griner

I don't understand when an input argument to an arcpy function that says it takes a layer as an input but it really only works when I give it the name of the layer. Specifically: I use the arcpy.mapping.ListLayers() function to get a list of all the 'layers' in my mxd. I then search through this list to find the layer for which the .name value of the layer I'm interested in. I then copy this layer object out of the list returned by ListLayers(). I assume that what I have is a layer in the sense of what's documented on this page: Layer—Help | ArcGIS Desktop 

 

I then read the documentation for the SelectLayerByAttribute_management() at: Select Layer By Attribute—Help | ArcGIS Desktop 

and it looks like it says that it takes a 'Layer' as it's first argument.  So I call arcpy.SelectLayerByAttribute_management() handing it my layer object (from above) and the second argument of "CLEAR_SELECTIONS".  The error message I get back is "ERROR 000732: Layer Name or Table View: Dataset GPL0 does not exist or is not supported". 

 

so, it seems I just called a function that wants a layer as its first argument and I hand it a layer object that arcpy handed to me, and the function barks.  So I try using the string value of the layer name as the first argument and now the function works like a charm! So, if a string value of the layer's name that I want to clear selections for is what the function wants, then why doesn't the documentation say the the first argument is a string value, instead of a layer object?

 

Python snippet:

mxd = arcpy.mapping.MapDocument("CURRENT")

main_frames = arcpy.mapping.ListDataFrames(mxd,"Layers")

main_frame = main_frames[0]

main_layers = layers = arcpy.mapping.ListLayers(mxd,"*",main_frame)

for lay in main_layers:

   if lay.name == 'Report Polygons':

      rpolys = lay

rpolys.isFeatureLayer         # this is 'True'

rpolys.isBroken               # this is 'False'

rpolys.isGroupLayer           # this is 'False'

rpolys.serviceProperties['Is_Geodatabase']         # this has the value 'true'

arcpy.SelectLayerByAttribute_management(rpolys,"CLEAR_SELECTION")       # this fails

arcpy.SelectLayerByAttribute_management(rpolys.name,"CLEAR_SELECTION")  # this gives me a <Result>

 

This seems to be the case for many of the arcpy functions that claim to take a 'Layer' as an input argument - all they really want is a string that contains the name of the layer.

Is it a fools errand to search my mxd for the layer I want to use when it appears that functions like SelectLayerByAttribute_management() will do it for me?

Outcomes