Select to view content in your preferred language

How to implement derived output parameter for ApplySymbologyFromLayer()

619
1
12-18-2023 09:54 AM
cweirpatrickco
Occasional Contributor

I'm trying to set feature symbology's from .lyrx files in a python script tool but am running into issues. The code runs with no errors but the .lyrx symbologies aren't applied (ie the symbologies don't change after script completion). I've opened a case with ESRI support, but it hasn't been much help. On the tool documentation page it says...

"
To see the symbology created in a script tool, the tool must include the layer as a derived output parameter. Similarly, the Updated Input Layer parameter value must be added as a derived model parameter model tool to see the symbology changes.”
"

How can I successfully implement that into this code? I tried in Line 32 but had no luck.

 

 

 

def apply_lyrx(map_object, lyrx_folder):
    """
    Applys .lyrx files to layers in the TOC according to layer name

    Parameters:
    - map_object (arcpy.mp.ArcGISProject("").activeMap): Map object
    - lyrx_folder (path): path to .lyrx source folder

    Returns:
    None
    """
    # Loop through layers in the TOC
    for lyr in map_object.listLayers():
        if "primary" in lyr.name.lower():
            lyrx_file = "Primary.lyrx"
        elif "secondary" in lyr.name.lower():
            lyrx_file = "Secondary.lyrx"
        elif "pole" in lyr.name.lower() and "anno" not in lyr.name.lower():
            lyrx_file = "Pole.lyrx"
        elif "anno" in lyr.name.lower():
            lyrx_file = "Pole_Anno.lyrx"
        else:
            lyrx_file = "clip_poly.lyrx"

        symb_path = os.path.join(lyrx_folder, lyrx_file)

        # apply .lyrx file
        if lyr.isFeatureLayer:
            arcpy.ApplySymbologyFromLayer_management(lyr, symb_path)

            # Set layer as derived output parameter
            arcpy.SetParameterAsText(0, lyr)

            arcpy.AddMessage(f"'{lyrx_file}' applied to '{lyr}'")

        else:
            arcpy.AddMessage(f"'{lyr.name}' is not a feature layer...")

    arcpy.AddMessage("All .lyrx files applied...")

    aprx.save()

 

 

 


PS: see attached .txt file for a stand-alone version of this code that works when run in the python window (symbologies get updated)... so I know it's something when running ApplySymbologyFromLayer() in a script tool.

0 Kudos
1 Reply
cpehlke
New Contributor

Hi,

I've run into this same problem before.

What worked for me was not using:

ApplySymbololgyFromLayer()

but instead using:

SetParameterSymbology()

https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/setparametersymbology.htm

0 Kudos