Changing Layer Symbology with Python

11-13-2019 08:19 AM
Occasional Contributor

I'm having problems changing layer symbology in ArcGIS Pro using Python. I have a feature class where the symbology has changed because the data in the fields used to define the symbolgy has changed. I'm trying to use Python to create a list of layers that need to be changed and then updating the symbology using the ApplySymbologyFromLayer function.

However; in Pro I am unable to make any changes using an IDE. If I try to use the Python window in Pro then it only updates the first target layer; but it updates that first layer for as many times as the number of items in the layer list. The only way I can update the symbology correctly is to use the Python window in Pro and rename all the target layers so they have different names. 

Conversely, in Desktop I am able to create a list of layers and use the ApplySymbologyFromLayer with no problems; whether I use an IDE or the Python window. 

Does Pro treat layer objects differently for some reason? I've noticed that using the geoprocessing method for importing symbology layers with the same name have a number appended to the end. 

How can I make changing the layer symbology in Pro with Python work? 

2 Replies
Occasional Contributor II

This is currently logged as a Bug I think (Bug BUG-000108497,Running the Apply Layer Symbology tool as a script in the Python window does not update the symbology in the map. ) The workaround (closing and reopening the project) does not seem to work as far as I can tell.

0 Kudos
Occasional Contributor II

Mark Binder‌ - I finally worked out how to do it a Python Toolbox, here's an example:

In Init:

           # Spatial Data Layer
           param0 = arcpy.Parameter(
            displayName="Spatial Data Layer (SPATIALDATA_LYR)",
            name ="SPATIALDATA_LYR",
        param0.value = "SPATIALDATA_LYR"
        # SMG (line) Layer
        param1 = arcpy.Parameter(
            name ="SMG_LINE_LYR",
        param1.value = "SMG_LINE_LYR"
    def execute(self, parameters, messages):
        """The source code of the tool."""
        # Script arguments      
        SMG_LINE_LYR= parameters[1].valueAsText
        #LYRX File vars
        SMG_LYRX =os.path.join(SEVENA_LYRX_DIR, "SMG_NAME.lyrx")
        arcpy.SetParameterAsText(0, SPATIALDATA_LYR) #<--------------THIS!!!
        # SMG Line - getParameterInfo incl value, symb as os.path.join, SMG_LYRX, None, "UPDATE")
        arcpy.SetParameterAsText(1, SMG_LINE_LYR)
I have no idea why setting the parameter again makes it work, but it works.
0 Kudos