Why does symbology applied with Python Script tool in Pro revert to default symbology when the script tool finishes?

1549
5
Jump to solution
11-07-2019 06:48 AM
JakeSlyder1
New Contributor II

I'm trying to put together a custom version of a cut-fill tool that ends with adding the symbolized raster to a map in pro, if there is an active map.  If I watch the table of contents while the script is running, the raster is added to the map and the symbology (classified, three classes based on VOLUME field) is successfully applied.  However, between when the script finishes and the tool overall finishes, the symbology reverts to what would be the default symbology for the raster (stretch based on Value field).   I tried setting arcpy.env.addOutputsToMap = False thinking that a tool's default behavior to add the output back to the map may be messing up the symbology, but that didn't change anything.  

#Note that outCF is the output raster defined earlier in the script.  

#Add symbolized raster to map
#First, define the symbology based on a lyrx file--3 classes, breaking at 0
Temp_Symbology = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'Symbology_Template.lyrx') #Gets the path of the script file because the lyrx file is in the same folder
#Create variables referring to active map and dataframe
p = arcpy.mp.ArcGISProject("CURRENT")
#Look if there's an active map.  If there isn't, i.e. m == None, then just skip the symbology.
m = p.activeMap
if m != None:
    #Add layer to top of active dataframe.
    lyr = m.addDataFromPath(outCF)
    #arcpy.AddMessage("Layer name: %s" %lyr)
    #Apply symbology from a layer file.
    arcpy.ApplySymbologyFromLayer_management(lyr, Temp_Symbology)


#Calculate whole process run time in minutes
processEnd = (time.time() - processStart)/60
arcpy.AddMessage("Script took %s minutes to complete" %processEnd)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I've seen some other discussion here about a bug with the apply symbology from layer gp tool not working in python scripts in, but I don't think that's the case here because it is applied then reverts. I also tried a different approach for adding and symbolize the raster in case BUG-000108497 is still affecting things in version 2.4.1 which I'm running.  

lf = arcpy.mp.LayerFile(Temp_Symbology)
m.addLayer(lf,"TOP")
m.updateConnectionProperties(Temp_Symbology,outCF)

The result looks as follows.  When the last line of my script--the printing message with a total run time--executes the symbology from the layer has been applied.  

Moments later, when the tool dialog prints that it has successfully completed, the symbology of the output has changed back to a stretch.  

Thanks for your help!  

0 Kudos
1 Solution

Accepted Solutions
JakeSlyder1
New Contributor II

Hi all, thank you for the helpful suggestions. Interestingly, setting the symbology in the parameter dialog of the tool properties did not solve the problem.  However, I found that "SetParamter" was able to solve the problem.  In the toolbox properties, the 5th parameter (so index 4) was set as a "Raster Dataset."  At the end of the script, I just added the quick line below to set this parameter equal to my "lyr" variable after the symbology was applied to "lyr."  

arcpy.SetParameter(4,lyr)

Thanks again.  

View solution in original post

5 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Jacob,

I tried with the following script using ArcGIS Pro 2.4.2, and the symbology did not revert back to stretch:

import time

processStart = time.time()

outCF = r"C:\DATA\RASTER\DEM\int_example1"
Temp_Symbology = r"C:\DATA\RASTER\DEM\dem_classify.lyrx"

p = arcpy.mp.ArcGISProject("CURRENT")
m = p.activeMap
if m != None:
    lyr = m.addDataFromPath(outCF)
    arcpy.ApplySymbologyFromLayer_management(lyr, Temp_Symbology)


processEnd = (time.time() - processStart)/60
arcpy.AddMessage("Script took %s minutes to complete" %processEnd)

You may want to try upgrade to 2.4.2 and see if the issue still occurs.

JakeSlyder1
New Contributor II

Hey Jake, thanks for the response.  Good idea to check it through the console, didn't think of that.  Unfortunately, it doesn't appear to be a version issue.  I ran that on 2.4.1 through the console and the symbology did not revert.  So it would seem to me the issue is not with the script itself, but with the behavior of how the output is handled when the script is imported into a toolbox and run that way.  I will try it on 2.4.2 when it is approved by our IT for installation.  

0 Kudos
JakeSkinner
Esri Esteemed Contributor

I tried as a Script tool as well (i.e. right-click on Toolbox > New > Script).  Note, I did not choose the option Import Script when doing this.  Running as a Script tool was also successful.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Jacob, of you are using a custom toolbox, you can set the output symbology in the parameter dialog, (python toolboxes, through the validation code)

JakeSlyder1
New Contributor II

Hi all, thank you for the helpful suggestions. Interestingly, setting the symbology in the parameter dialog of the tool properties did not solve the problem.  However, I found that "SetParamter" was able to solve the problem.  In the toolbox properties, the 5th parameter (so index 4) was set as a "Raster Dataset."  At the end of the script, I just added the quick line below to set this parameter equal to my "lyr" variable after the symbology was applied to "lyr."  

arcpy.SetParameter(4,lyr)

Thanks again.