I am struggling to apply symbology from an existing layer file to an output from a script tool. The below snippet is from a larger script tool that takes a DEM, clips it to the Map View, smooths it using Focal Statistics, creates a feature class of contours in an existing geodatabase, and then adds an index field. The contour feature class is automatically added to the map (seemingly regardless of my use of env.addOutputsToMap). Seems like the use of either "Add Field" or "Calculate Field" does this, which is fine, I want it there anyway.
out_gdb = r"F:\ContourTest\ContourTest.gdb"
cont_file = fr"{out_gdb}\Contours_20"
current_project = arcpy.mp.ArcGISProject("CURRENT")
current_map = current_project.listMaps()[0]
current_map.addDataFromPath(fr"{cont_file}")
cont_layer = current_map.listLayers("Contour*")[0]
symbology = r"F:\ContourTest\Contour_Index.lyrx"
arcpy.management.ApplySymbologyFromLayer(in_layer=cont_layer,
in_symbology_layer=symbology,
symbology_fields="VALUE_FIELD Index_Contour Index_Contour",
update_symbology="MAINTAIN")
The above code is the last bit of the larger script. The whole thing, including this bit, runs without exceptions, but the symbology does not apply as expected.
But, if I then take this bit of code and paste it exactly into the Python Window and run it, another copy of the contour feature class is added to the TOC, and the correct symbology is applied. Works perfectly. If I then remove both, the symbolized contours and the unsymbolized contours, and run the code above in the Python Window again, just a symbolized version is added (or an unsymbolized version is added, and then the symbology is applied, either way). Again, works perfectly.
I have tried using versions of MakeFeatureLayer, addLayer, all kinds of things--I am likely getting tripped up over the colloquial use of the term "layer," vs. the technical meaning of layer, layer file, layer object in code vs path to layer, etc. etc.
Happy to paste the entire script if that is helpful, just didn't want to clutter the question. This a script tool in a regular toolbox (.tbx) with parameters and some basic validation set up within Pro. The validation pertains to whether a contour index field should be created, checking for integer inputs, etc., so I don't think it should interfere here, but who knows. I am running ArcGIS Pro 2.5.0--it tells me that this is the current version.