Assuming that I have an idealized .mxd TOC structure with group layers that contain group and subgroup layers which have the same name like in the following example:
Is it possible to add data using an 'absolute' layer path with ArcPy?
For example, if I want to add a raster layer
SampleRaster.tif to the following place GroupLayer2/SubGroupLayer2 : how could I do this in ArcPy?
I know how to add a group layer and then add the specific layer (e.g. solution in this question Adding new group layer with ArcPy?) but have not found a solution how to add it if two layers have the same name.
My code that I have so far is pasted below
# add rasters to .mxd project import arcpy arcpy.env.workspace = r'D:\Data\Rasters' rasters = arcpy.ListRasters("*", "TIF") for raster in rasters: # add raster to .mxd mxd = arcpy.mapping.MapDocument(r"D:\Data\Test.mxd") df = arcpy.mapping.ListDataFrames(mxd) # here I need to list the layers in the subgroup "SubGroupLayers2", but what to do when the subgroup layers names are equal? Is there a way to indicate an absolute path (e.g. Layers/GroupLayer2/SubGroupLayer2 here?) targetGroupLayer = arcpy.mapping.ListLayers(mxd, "SubGroupLayer2", df) # I am then using another layer to apply the symbology to the raster and update the layer newlayer = arcpy.mapping.Layer(raster) newlayer.visible = False arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, newlayer, "BOTTOM") updateLayer = arcpy.mapping.ListLayers(mxd, raster, df) sourceLayer = arcpy.mapping.Layer(r"D:\Data\Colormap.lyr") arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True) mxd.save()