Fail to Save Group Layer Using ArcPy in Stand Along Script

363
3
09-25-2022 09:14 PM
ZhongyuYang
New Contributor II
It is succussed to save an empty group layer using following code in python window.
arcpy.management.SaveToLayerFile("Empty Group Layer", out_layer_path, None, "CURRENT")
 
However, in stand along script environment, the saved layer file is invalid. I changed the keyword "Empty Group Layer" to a layer object from .aprx file opened via arcpy.mp. (from aprx_map.listLayers()). Note: this empty group layer is manually generated in ArcGIS pro by right click MAP and click New Group Layer.
 
The documentation mentioned the input layer is a layer datatype that I assume this should work.
 
0 Kudos
3 Replies
JohannesLindner
MVP Frequent Contributor

Hmm, works for me.

 

Save this script as a Python file in your project folder and run it:

import arcpy
aprx_name = "Admin.aprx"
map_name = "Test"
lyr_name = "New Group Layer"

aprx = arcpy.mp.ArcGISProject(aprx_name)
m = aprx.listMaps(map_name)[0]
lyr = m.listLayers(lyr_name)[0]

arcpy.management.SaveToLayerFile(lyr, "test1.lyrx")
lyr.saveACopy("test2.lyrx")

 


Have a great day!
Johannes
0 Kudos
ZhongyuYang
New Contributor II

Hi Johannes, 

I tried your code. "test1.lyrx" is invalid. While "test2.lyrx" is valid (no problem to open or load into other project). 

Is it the same as you?

 

Thanks,

Zhongyu

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

Is it the same as you?

Nope, I can open both layers without problems. I don't know what the cause for your problem could be, but you could always just use the second method...


Have a great day!
Johannes
0 Kudos