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.
1 Answer 1
Your glyr
variable is a LayerFile
object, not a Layer
. 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:
glyr.foo= "bar"
glyr.name = "some string"
glyr.another_nonexistent_property = "nothing"
Your group layer is a Layer
in the LayerFile
, and you can access it using the .listLayers()
method, e.g.
lyr_file = arcpy.mp.LayerFile(r"path to.lyrx") # LayerFile object
group_layer = lyr_file.listLayers("Your template Group Layer name")[0] # Layer object
group_layer.name = "An Updated Name"