My stand-alone arcpy script below produces .lyrx files in the right location, with the right name.
If I drag any of the .lyrx files onto a map in an ArcGIS Pro project, it fails with the error: "The selected layer file is not valid". The contencts of the .lyrx file (viewed in a text editor) is:
{
"type" : "CIMLayerDocument",
"version" : "3.5.0",
"build" : 57366
}This is clearly not a valid .lyrx file!
This issue occurs if I run the script against a wide variety of ArcGIS Pro projects, using ArcGIS Pro version 3.5.2.
If I right-click on the same layer(s) in the same ArcGIS Pro project and save to a layer file that way, then the .lyrx can be used without any problems.
Similarly, this same code works fine if running from an ArcGIS Pro python window. It only produces the invalid .lyrx files when running from a stand-alone Python script.
What is going wrong here?
Here is the very simple script:
for lyr in mp.listLayers():
lyrx_path = os.path.join(layers_dir, lyr.name) + ".lyrx"
arcpy.SaveToLayerFile_management(lyr, lyrx_path)UPDATE:
ESRI have been able to reproduce this issue and they have suggested that I could try using arcpy.management.MakeFeatureLayer() to create a feature layer from the Layer object and then saving that to a layer file instead. However, this suggestion is unsuitable for my situation, so I haven't even tried it. I need this to work for raster layers, online tile layers, and most especially, I need this to work for group layers (which include multiple other layers). These cannot be converted to feature layers!