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.
#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)
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!