I can get the following code to run in an mxd with a single dataframe. I need it, however, to run and loop through an mxd that has multiple dataframes with identically named files and data sources to those in my lyrlist.
EDIT Additional Info - The intent of the last if statement was to provide a workaround for the inability of arcpy to set the Display Expression in the layer properties dialog by re-adding a file with a name field. So, the crux of the question is how best to update a layers Display Expression in an automated a way as possible (preferably with python as I have no familiarity with ArcObjects).
import arcpy lyrlist = ['Inset Bridges','Bridges', 'Transportation Points', 'Transportation Lines'] mxd = arcpy.mapping.MapDocument("Current") dflist = arcpy.mapping.ListDataFrames(mxd) for df in dflist: if df.scale <= 126720: for lyr in (arcpy.mapping.ListLayers(mxd, "", df)): if lyr.supports("DEFINITIONQUERY"): if lyr.name in lyrlist: NewFile = arcpy.mapping.Layer(lyr.dataSource) OldFN = lyr.name NewFile.name ="NewFi" lyr.name = "OldFi" arcpy.mapping.InsertLayer(df, lyr, NewFile, "AFTER") #the following line results in the ValueError when run in an #mxd that has multiple dataframes with filenames from lyrlist updateLyr = arcpy.mapping.Layer("NewFi") sourceLyr = arcpy.mapping.Layer("OldFi") arcpy.mapping.UpdateLayer(df, updateLyr, sourceLyr, True) updateLyr.name = OldFN arcpy.mapping.RemoveLayer(df, lyr) arcpy.RefreshTOC() arcpy.RefreshActiveView print "script complete" del mxd
The code works well until it gets to the where I assign the updateLyr variable. That gives me the following error:
Runtime error Traceback (most recent call last):
File "", line 44, in
File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 389, in init super(LayerMixin, self).init(lyrfile)
File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects_base.py", line 47, in init for arg in args))
ValueError: Object: CreateObject Layer invalid data source