Select to view content in your preferred language

How to move a layer to a group?

504
2
Jump to solution
05-12-2024 02:08 PM
HKN
by
Emerging Contributor

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

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:

ValueError                                Traceback (most recent call last)
In  [37]:
Line 8:     mp.addLayerToGroup(group_layer, lyr_to_move, "AUTO_ARRANGE")

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py, in fn_:
Line 191:   return fn(*args, **kw)

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in addLayerToGroup:
Line 3559:  return convertArcObjectToPythonObject(self._arc_object.addLayerToGroup(*gp_fixargs((target_group_layer, add_layer_or_layerfile, add_position), True)))

ValueError: []

 

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:

ValueError                                Traceback (most recent call last)
In  [38]:
Line 9:     mp.moveLayer(ref_layer, lyr_to_move, "AFTER")

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\utils.py, in fn_:
Line 191:   return fn(*args, **kw)

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in moveLayer:
Line 3772:  return convertArcObjectToPythonObject(self._arc_object.moveLayer(*gp_fixargs((reference_layer, move_layer, insert_position), True)))

ValueError: []

Any ideas on how to fix this error or are there other ways of moving a layer to a group in the current map? Thanks for your help and ideas.

0 Kudos
1 Solution

Accepted Solutions
Clubdebambos
Frequent Contributor

Hi @HKN 

listLayers always returns a list. When using the wildcard with a layer name or group layer name you are more than likely returning a list that contains one layer so you need to put [0] at the end like below. 

 

lyr_to_move = mp.listLayers("Fishnet_v1_manual")[0]
group_layer = mp.listLayers("VMS")[0]

 

~ learn.finaldraftmapping.com

View solution in original post

0 Kudos
2 Replies
Clubdebambos
Frequent Contributor

Hi @HKN 

listLayers always returns a list. When using the wildcard with a layer name or group layer name you are more than likely returning a list that contains one layer so you need to put [0] at the end like below. 

 

lyr_to_move = mp.listLayers("Fishnet_v1_manual")[0]
group_layer = mp.listLayers("VMS")[0]

 

~ learn.finaldraftmapping.com
0 Kudos
HKN
by
Emerging Contributor

Hi @Clubdebambos , thanks for the above. It works fine now with this change.