2

I am trying to move a layer to a group using python code. Both the layer and the group are in the current/same map in ArcGIS Pro.

I have the following code (the layer to be moved to the group "VMS" is "Fishnet_v1_manual")

import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
mp = aprx.listMaps()[0]
lyr_to_move = mp.listLayers("Fishnet_v1_manual")
group_layer = mp.listLayers("VMS")
mp.addLayerToGroup(group_layer, lyr_to_move, "AUTO_ARRANGE")

But I keep getting this error: addLayerToGroup

I have also tried moving the layer to after the first layer in the group using moveLayer, but still get a similar error message as the above. The code I used for this is:

import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
mp = aprx.listMaps()[0]
lyr_to_move = mp.listLayers("Fishnet_v1_FeatureLayer_v3")
ref_layer = mp.listLayers(r"Data points in grid")
mp.moveLayer(ref_layer, lyr_to_move, "AFTER")

The error message is:

moveLayer

Any ideas on how to fix this error or are there other ways of moving a layer to a group in the current map?

2
  • @Hornbydd - Thanks. Before posting, I did look at the help file for ArcGIS Pro pro.arcgis.com/en/pro-app/latest/arcpy/mapping/map-class.htm. "addLayerToGroup" has three parameters - 1) target_group_layer, 2)add_layer_or_layerfile and 3){add_position}. I have all the three specified in the code I shared above. The helpfile for which you have provided a link is for ArcMap where AddLayerToGroup has 4 parameters - data_frame, target_group_layer, add_layer and {add_position}, but this is not the case in the 'addLayerToGroup' method in ArcGIS Pro. Commented May 12, 2024 at 21:52
  • 1
    Please always provide error messages as formatted text rather than only in pictures. Commented May 13, 2024 at 3:05

1 Answer 1

3

The help file states that listLayers() - Returns a Python list of Layer objects that exist within a map.

So in your code lyr_to_move is a list object.

To retrieve the layer from the list to feed into the addLayerToGroup() you would use the following code:

lyr_to_move = mp.listLayers("Fishnet_v1_manual")[0]
3
  • Hi, Thanks for this. I accordingly updated the code to: aprx = arcpy.mp.ArcGISProject("CURRENT") mp = aprx.listMaps()[0] lyr_to_move = mp.listLayers("Fishnet_v1_manual")[0] group_layer = mp.listLayers("VMS")[0] mp.addLayerToGroup(group_layer, lyr_to_move, " AUTO_ARRANGE") The above works fine. One thing to check – this code copies the "Fishnet_v1_manual" layer into the "VMS" group rather than moving it. Is there a way to move (i.e. not copy) the layer into the group? I can see "moveLayer" achieves this but then it does not work with moving a layer into a group. Commented May 13, 2024 at 12:23
  • As you have a handle on the layer object after it is copied into the group layer you could simply remove it with removeLayer()? Commented May 13, 2024 at 14:17
  • This is what I am doing. But I wanted to check if - similar to moveLayer (which does not copy but just moves the layer) - there is a one-step of moving a layer to a group instead of two steps: 1) adding the layer to the group, and 2) removing that layer from outside the group. Commented May 13, 2024 at 15:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.