How do I rename a group layer in ArcGIS Pro using ArcPy?
In ArcMap I can do so thus:
groupLayer = arcpy.mapping.Layer(r"Path to grouplayer.lyr")# empty group layer as template
groupLayer.name = some string
arcpy.mapping.AddLayer(dataframe, groupLayer, add_position='TOP')
I can't find an analogous way to do this in ArcGIS Pro:
glyr = arcpy.mp.LayerFile(r"Path to GroupLayer.lyrx")# empty group layer as template
glyr.name = some string
theMap.addLayer(glyr, add_position='TOP')
While the above code adds the group layer, it does not rename it, and it does not flag an error so I'm not really sure what is going on.
Best Answer
Your
glyr
variable is aLayerFile
object, not aLayer
. It represents the file your group layer is stored in, not the group layer itself. Note that a layer file can contain multiple layers, eg:The
LayerFile
object doesn't have a.name
property and doesn't raise an error when setting a non-existent property. You could do this and nothing would happen:Your group layer is a
Layer
in theLayerFile
, and you can access it using the.listLayers()
method, e.g.