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.
<SPAN class="comment token">#Note that outCF is the output raster defined earlier in the script. </SPAN>
<SPAN class="comment token">#Add symbolized raster to map</SPAN>
<SPAN class="comment token">#First, define the symbology based on a lyrx file--3 classes, breaking at 0</SPAN>
Temp_Symbology <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>dirname<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>abspath<SPAN class="punctuation token">(</SPAN>__file__<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Symbology_Template.lyrx'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#Gets the path of the script file because the lyrx file is in the same folder</SPAN>
<SPAN class="comment token">#Create variables referring to active map and dataframe</SPAN>
p <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mp<SPAN class="punctuation token">.</SPAN>ArcGISProject<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Look if there's an active map. If there isn't, i.e. m == None, then just skip the symbology.</SPAN>
m <SPAN class="operator token">=</SPAN> p<SPAN class="punctuation token">.</SPAN>activeMap
<SPAN class="keyword token">if</SPAN> m <SPAN class="operator token">!=</SPAN> None<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token">#Add layer to top of active dataframe.</SPAN>
lyr <SPAN class="operator token">=</SPAN> m<SPAN class="punctuation token">.</SPAN>addDataFromPath<SPAN class="punctuation token">(</SPAN>outCF<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#arcpy.AddMessage("Layer name: %s" %lyr)</SPAN>
<SPAN class="comment token">#Apply symbology from a layer file.</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>ApplySymbologyFromLayer_management<SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">,</SPAN> Temp_Symbology<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Calculate whole process run time in minutes</SPAN>
processEnd <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>time<SPAN class="punctuation token">.</SPAN>time<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">-</SPAN> processStart<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">/</SPAN><SPAN class="number token">60</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Script took %s minutes to complete"</SPAN> <SPAN class="operator token">%</SPAN>processEnd<SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>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 <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mp<SPAN class="punctuation token">.</SPAN>LayerFile<SPAN class="punctuation token">(</SPAN>Temp_Symbology<SPAN class="punctuation token">)</SPAN>
m<SPAN class="punctuation token">.</SPAN>addLayer<SPAN class="punctuation token">(</SPAN>lf<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"TOP"</SPAN><SPAN class="punctuation token">)</SPAN>
m<SPAN class="punctuation token">.</SPAN>updateConnectionProperties<SPAN class="punctuation token">(</SPAN>Temp_Symbology<SPAN class="punctuation token">,</SPAN>outCF<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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!