m.listLayers problematic when run in background tool?

04-22-2021 09:45 AM
Labels (2)
New Contributor II

This script creates feature layer files from features in a dataset, then uses the m.listLayers function to reference those layers and update the symbology renderer. The script works w/o issues in the foreground in Jupyter Notebook. However, when the script is called using a custom tool in the GeoProcessing window, interacting with the same aprx and map, the script runs, generates the layer files in the dflt gdb, but then does not draw the layers in the map. It makes me think the way I am referencing the layer files is problematic when run in the background? Any suggestions? Thanks, Penelope


#List features from Results feature dataset
#Make feature layer from features, add feature layers to map
m = aprx.listMaps()[0]
datasets = arcpy.ListDatasets(feature_type='feature')
for ds in datasets:
    ResultList= arcpy.ListFeatureClasses("*", "ALL", ds)
    for fc in ResultList:
        OutFile = os.path.join(wkspc, str(fc))
        arcpy.MakeFeatureLayer_management(fc, fc, None, None)
        lf = arcpy.management.SaveToLayerFile(fc, OutFile)
p = arcpy.mp.ArcGISProject("CURRENT")
for lyr in m.listLayers():
    if lyr.isFeatureLayer:
        sym = lyr.symbology
        if hasattr(sym, 'renderer'):
            if sym.renderer.type == "SimpleRenderer":
                #modify graduated symbol renderer
                fields = arcpy.ListFields(lyr)
                #fields = ListFields(dataset)
                for field in fields:
                    if field in fields:
                        if field.name=="Total_Deli":
                            sym.renderer.classificationField = "Total_Deli"
                            sym.renderer.breakCount = 6
                            sym.renderer.minimumSymbolSize = 1
                            sym.renderer.maximumSymbolSize = 8
                            sym.renderer.colorRamp = p.listColorRamps("Greens")[0]

                        lyr.symbology = sym



0 Kudos
1 Reply
MVP Esteemed Contributor

custom tools and models run in the foreground even if background is enabled.

Check the help topic for further restrictions.

Foreground and background processing—ArcMap | Documentation (arcgis.com)

... sort of retired...
0 Kudos