Error trying to save layer from MXD to lyrx.

777
6
Jump to solution
10-09-2019 09:46 AM
SteveScott
New Contributor III

I am trying to load a layer from an mxd and back up the layer using ArcGIS Pro and Python.

aprx.importDocument(current_document_name)
mlist = aprx.listMaps()
infra_layers = mlist[0].listLayers()
new_file_name = backup_geodatabase_path + 'ActiveInfraProjects.lyrx'
arcpy.SaveToLayerFile_management(infra_layers[0].name, new_file_name, 'ABSOLUTE')
#I get the following error:

ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Layer: Dataset active_infrastructure_projects does not exist or is not supported
Failed to execute (SaveToLayerFile).

#It says it doesn't exist but I was able to get the name from the file (infra_layers[0]) so it must exist. 
#Why is it not supported?

0 Kudos
1 Solution

Accepted Solutions
SteveScott
New Contributor III
I needed to convert to a feature layer first.

arcpy.MakeFeatureLayer_management(infra_layers[0], "fl")
arcpy.CopyFeatures_management('fl', workspace_name)
arcpy.SaveToLayerFile_management(fl, new_file_name, 'ABSOLUTE')

View solution in original post

0 Kudos
6 Replies
DuncanHornby
MVP Notable Contributor

I've just tried your code and it worked for me. So this leads me to wonder if the problem is in this line:

aprx.importDocument(current_document_name)

Is this code in a loop and you are only showing us the salient commands? If so may be the loop is loading up a project with say a broken layer in it? Or you are have some sort of network connection issue?

0 Kudos
SteveScott
New Contributor III

You mention a loop. I am importing a single .mxd so why would it be in a loop?

0 Kudos
DuncanHornby
MVP Notable Contributor

You are missing all the import modules part of your script which would suggest to me that you are showing a cut down version hence me speculating how you are running this code. You don't state how you are running it. So I'm suggesting you are not showing us the bigger picture and the problem lies elsewhere as I was able to run your code without error.

0 Kudos
DanPatterson_Retired
MVP Emeritus

watch your path construction

# ---- not good

backup_geodatabase_path = r"c:\somefolder\some.gdb"

new_file_name = backup_geodatabase_path + 'ActiveInfraProjects.lyrx'

new_file_name
'c:\\somefolder\\some.gdbActiveInfraProjects.lyrx'

# ---- concatenate the bits simply
args = [backup_geodatabase_path, 'ActiveInfraProjects.lyrx']

new_file_name = "\\".join([a for a in args])

new_file_name
'c:\\somefolder\\some.gdb\\ActiveInfraProjects.lyrx'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
NeilAyres
MVP Alum

Or use os.path.join(var1, var2, varetc) to construct paths to things

0 Kudos
SteveScott
New Contributor III
I needed to convert to a feature layer first.

arcpy.MakeFeatureLayer_management(infra_layers[0], "fl")
arcpy.CopyFeatures_management('fl', workspace_name)
arcpy.SaveToLayerFile_management(fl, new_file_name, 'ABSOLUTE')

0 Kudos