AnsweredAssumed Answered


Question asked by jpedder on Aug 23, 2012
Latest reply on Aug 23, 2012 by BrokenLegMike
Morning all, this is a followup post to the thread I have going here

I'm still having a tough time understanding the ListDataFrames functionality and results. In my mind what I think ListDataFrames would give me is exactly that, a listing of data frames with the mxd. However when I execute this command I get back a listing of layers. All the examples I see in the help use [0] such as

df = arcpy.mapping.ListDataFrames(mxd, "Transportation")[0]

When I execute a line such as the above I get back a list of layers within my first layergroup.

This is probably just me not understanding python or coding very well, it's driving me a little nuts.

The objective of my script below is to save a layer of base_data from the current mxd to disk. Then look at the target mxd and see if it has a layer names Base_Data, if so remove it, then import the previously saved layer and add to the bottom of the target mxd. Then of course save.

import arcpy   from arcpy import env   # Set overwrite option arcpy.env.overwriteOutput = True   # Gather user input parameters  TargetFile = arcpy.GetParameterAsText(0)  if TargetFile > "":         arcpy.AddMessage("Target File is True")                  # from current open mxd, save base data layer file to disk         LayerFile = "./Base_Layer"         arcpy.SaveToLayerFile_management("Base_Data_Group",LayerFile,"RELATIVE")         arcpy.AddMessage("Base Data Saved as "+ LayerFile)          mxd = arcpy.mapping.MapDocument(TargetFile)         df = arcpy.mapping.ListDataFrames(mxd)         lyr_list = arcpy.mapping.ListLayers(mxd)                 for lyr in lyr_list:                 if 'Base_Data' in                         arcpy.mapping.RemoveLayer(df,lyr)                         arcpy.AddMessage(lyr)          path = LayerFile + '.lyr'         arcpy.AddMessage("adding layer")         addLayer = arcpy.mapping.Layer(path)         arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")