arcpy: get access to layer that's in group layer?

4279
15
09-21-2018 08:16 AM
helenchu
Occasional Contributor II

Hi

I created a model builder and scripted it out to python.  My issue is it only works if the layer is outside of group layer.  How can I modify it to works in both inside and outside of group layer?  Thank you very much for your help.  

import arcpy,sys, os
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\GIS\MyGeodatabase.gdb"
mxd = arcpy.mapping.MapDocument("CURRENT")
layerName = 'street'
lyr = arcpy.mapping.ListLayers(mxd, layerName)[0]
fc = arcpy.GetParameterAsText(0)

Street = "Street"

selCount = 0
if lyr.getSelectionSet(): # If there are selected features
selCount = len(lyr.getSelectionSet())
arcpy.AddMessage("{0} has {1} features selected.".format(fc, selCount))
try:
# Process: Copy Features
arcpy.CopyFeatures_management(Street , street_CopySelected, "", "0", "0", "0")

0 Kudos
15 Replies
helenchu
Occasional Contributor II

Thanks for the tip!  When I drag my Street layer out of TopLayerGroup, the codes work just fine.  I begin to suspect if group layer is supported by ESRI for doing most of data management tools?

0 Kudos
RandyBurton
MVP Alum

You may have some issues with how you are using getSelectionSet.  My test code:

mxd = arcpy.mapping.MapDocument("CURRENT")
layerName = 'street'
lyr = arcpy.mapping.ListLayers(mxd, layerName)[0]
for l in lyr:
    print l.name, l.getSelectionSet(), len(l.getSelectionSet()) if l.getSelectionSet() is not None else 'None'

    #  and if you only want to work with layer named 'street'
    if l.name == 'street':
        # do something‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
RandyBurton
MVP Alum

You might try something like:

import arcpy,sys, os
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\GIS\MyGeodatabase.gdb"
mxd = arcpy.mapping.MapDocument("CURRENT")

layerName = 'street'

for lyr in arcpy.mapping.ListLayers(mxd, layerName)[0]:
    if lyr.getSelectionSet(): # If there are selected features
        arcpy.AddMessage("{} has {} features selected".format(lyr.name, len(lyr.getSelectionSet()))

        # Process: Copy Features
        arcpy.CopyFeatures_management(Street , street_CopySelected, "", "0", "0", "0")
helenchu
Occasional Contributor II

Randy,

I tried your codes and they did not work for me.   Thanks.

Error:

Traceback (most recent call last):
File "C:\Working\Python\python_script\copy_test_1.py", line 9, in <module>
for lyr in arcpy.mapping.ListLayers(mxd, layerName)[0]:
File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\arcobjects\mixins.py", line 401, in __iter__
self.layers
AttributeError: LayerObject: Get attribute layers does not exist

0 Kudos
RandyBurton
MVP Alum

Try this change:

layerName = 'Street'

for lyr in arcpy.mapping.ListLayers(mxd, layerName): # remove [0]
0 Kudos
helenchu
Occasional Contributor II

I called ESRI support and the solution is

arcpy.CopyFeatures_management(lyr, street_CopySelected, "", "0", "0", "0")

It can not read the longName but it could read the object lyr.

Thank you all so much for your help.