0

I am currently trying to add all the feature classes contained within a gdb to a group layer in ArcGIS Pro via a script. I have however not managed to add more than one single feature class at a time.

By looking at the script below, do you have any suggestions about how to solve this?

arcpy.env.workspace = "C:\\TEST_ENV\\AGOL_output\\AGOL.gdb"
datasets = arcpy.ListDatasets(feature_type='feature')
datasets = [''] + datasets if datasets is not None else []
rootPath_aprx = 'C:\\TEST_ENV\\AGOL_output\\'
aprx = arcpy.mp.ArcGISProject(rootPath_aprx + 'WMStest1.aprx')
aprxMap = aprx.listMaps("Map")[0]
targetGroupLayer = aprxMap.listLayers('Group1')[0]
for ds in datasets:
    for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
        path = os.path.join(arcpy.env.workspace, ds, fc)
        print(path)
        for GroupLayer in targetGroupLayer:
            # print(l)
            if str(GroupLayer) == GroupName:
                try:
                    aprxMap.addLayerToGroup(aprxMap, targetGroupLayer, path, ['AUTO_ARRANGE'])
                except:
                    print("gah.")
aprx.save()

The error I am receiving when I run the script is:

for GroupLayer in targetGroupLayer: TypeError: 'Layer' object is not iterable

1 Answer 1

1

You are setting:

targetGroupLayer = aprxMap.listLayers('Group1')[0]

The index of 0 means that you are setting targetGroupLayer to the first item in the list returned by listLayers i.e. targetGroupLayer is being set to a Layer object.

You then use:

for GroupLayer in targetGroupLayer:

to try and iterate a Layer object which inevitably gives you TypeError: 'Layer' object is not iterable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.