BrokenLegMike

Looping Dataframe in Python doesn't Return Expected Results

Discussion created by BrokenLegMike on Sep 10, 2012
Latest reply on Sep 13, 2012 by tteaf
I'm creating a python addin script to be used on some mxd's I have created as templates. Each map contains 2 dataframes. I need to loop through each dataframe and do some processing on each. I set my loop as follows:

for df in arcpy.mapping.ListDataFrames(mxd, "DFMAIN"):
                        for lyr in arcpy.mapping.ListLayers(mxd, "", df):
                               # do some processing here
for df in arcpy.mapping.ListDataFrames(mxd, "DFINSET"):
                        for lyr in arcpy.mapping.ListLayers(mxd, "", df):
                               # do some processing here



In this scenario, if the second dataframe is the 'active' dataframe, it gets chosen first and then some weird stuff happens. I get layers from my first dataframe copied into the second which baffles me and I am not even using any functionality that would copy layers (i.e. arcpy.mapping.AddLayer or anything similar) I just update some definition queries.

If I change the template so that the first dataframe is the active one, then everything works fine.

I tried indexing the dataframe loops:

for df in arcpy.mapping.ListDataFrames(mxd, "DFMAIN")[0]:


but I get the same weirdness if the second dataframe is active.

I guess the easy answer is, make sure the template has the first dataframe as the active one and save it that way, but I have users who may change this unknowingly. I want the geoprocessing to take care of the active dataframe issue. I can't seen anything in the help menu to set the dataframe as the active one.

Outcomes