Hi all. I'm in trouble with the SaveToLayerFile_management function in a python script for arcgis pro 2.1.2. The script is setting a symbology that works but happens to not beeing saved on disk. I mean the lyrx file is done but doesn't contain the symbology. I've tried many changes in vain. The code below works properly on the python console however. For info, I changed here somes values initially gotten with GetParameterAsText.
Ws=arcpy.env.workspace Mxd = arcpy.mp.ArcGISProject("Current") MapPath = Mxd.filePath ActMap = Mxd.activeMap LyrName = 'Benzene' SeuilsNb = 2 Seuils = "10;580" #string vers list fileName = os.path.basename(MapPath) fileNameLen = len(fileName)+1 Ws = MapPath[0:-fileNameLen] in_layer = ActMap.listLayers(LyrName) LayerSource = in_layer.dataSource TargetFolder = "J:\\00-Template-job_Oct2017\\900_CAD_GIS\\Lyr" Labels =  for Seuil in Seuils: if Seuil == Seuils: Labels.append("Inférieur à " + Seuil) BorneInf = Seuil else: Labels.append(BorneInf + " à " + Seuil) BorneInf = Seuil Symbologie = in_layer.symbology Symbologie.updateRenderer('GraduatedColorsRenderer') Symbologie.renderer.classificationField = 'result_value_atof' Symbologie.renderer.breakCount = SeuilsNb i = 0 for bc in Symbologie.renderer.classBreaks: bc.upperBound = float(Seuils[i]) bc.label = Labels[i] i =+1 in_layer.symbology = Symbologie arcpy.SaveToLayerFile_management(LyrName,os.path.join(TargetFolder, LyrName + ".lyrx"))
Thanks in advance for the help!
Without dissecting your paths, are they in the same location? if not, you might want to explore the absolute/relative option to see if that is an issue or has an effect
Hello Dan. Thanks for helping. Unfortunatly, adding an "absolute" or "relative" parameter doesn't help. However I can say one thing more I can't explain eather, If I run the code twice, the symbology disappear on arcgis but the lyrx file is this time complete :-). Does that make sens for you?
I kept trying to solve the issue in many ways. Result still consists on a file without my symbology. If running the program twice helps writing a complete lyrx file , may be the layer cache could be responsible. Is there a direct way to programaticly clear a layer cache? (since the updateConnectionProperties function is not exactly a success here)..
you would have to search help using 'cache' for what can have a cache, and how to set or get rid of them.
This is the help topic for layer properties, where they talk about caching ...
you might try the approach, but whether there is arcpy access I haven't looked