I am a little surprised to see that there seems to be no way to get the index of the active data frame in ArcPy, or did I miss something?
For example, what if you want to add a tool’s output to the active data frame? Here is the workflow I came up with to solve the problem:
- Reference the MXD
- Get the data frames
- Get the name of the active data frame
- Loop through all of the data frames, and check if the current one matches the name, if so, add the dataset
And here is the code:
mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd,"*") activeDfName = mxd.activeDataFrame.name i = 0 for d in df: if d.name == activeDfName: finalDf = arcpy.mapping.ListDataFrames(mxd,"*")[i] newlayer = arcpy.mapping.Layer(addlayer) arcpy.mapping.AddLayer(finalDf, newlayer,"BOTTOM") else: i +=1
Although this works fine I feel like this is way too much code to do such a simple thing. Is there really no out of the box way to get the index of the active data frame, or can anyone suggest other ideas, perhaps more compact, how to approach this?
PS: parts of my question can be found here, but I think that my question is more clearly about whether this is possible at all, and it asks for alternatives, as it is always interesting to see what others come up with.