0

I'm using a python script to create an empty group layer in the current map document, add some layers to this group and update their symbology from an existing .lyr file.

I also want to to turn on these layers' labels using layer.showLabels = True and it works fine for normal layers, but for some reason it doesn't work on layers inside a group layer. However, when I manually drag these layers outside of the group, the labels are suddenly displayed, and they are still displayed when I drag the layers back into the group.

Why does it happen and how can I programmatically display the labels even for layers inside a group?

Here is the relevant part (of a simplified version) of my ode:

mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd)[0]
groupLyr = arcpy.mapping.Layer(r'C:\Path\to\Group.lyr') # this is an empty group lyr file

arcpy.mapping.AddLayer(df,groupLyr)
currentGroup = arcpy.mapping.ListLayers(mxd,"Group")[0]

source_fc = arcpy.mapping.Layer(r'C:\Path\to\my.gdb\feature_class')
arcpy.mapping.AddLayerToGroup(df, currentGroup, source_fc, "BOTTOM")

lyr = arcpy.mapping.ListLayers(mxd,"[my layer's name]")[0]
lyr.showLabels = True # this line doesn't raise an error, but it does not turns the labels on
5
  • Could you let us know the version you are using too - I have a vague recollection of a problem in this vicinity so knowing your version may help my searching.
    – PolyGeo
    Oct 22, 2014 at 13:14
  • I am using ArcGIS 10.1 Basic with SP1.
    – isshp
    Oct 22, 2014 at 13:37
  • Do you actually have 'my layer's name' in brackets, or is that just for illustration? You may also want to add df to the Listlayers call.
    – recurvata
    Oct 22, 2014 at 13:50
  • Also, apologize if this is a dumb question, but is your group layer turned on?
    – recurvata
    Oct 22, 2014 at 14:40
  • Yes, I do have the right name in my original code, and I do know for sure that the "lyr" variable contains the right layer. The group layer is turned on. Thanks.
    – isshp
    Oct 23, 2014 at 7:16

1 Answer 1

1

Add the line arcpy.RefreshActiveView() to the end of the script. (Or choose "Refresh" from the "View" menu in ArcMap.)

ArcMap does not automatically redraw the map after each and every script command. But it does redraw the map if you move a layer using the GUI Table of Contents, as you've described moving the layers out-of/into the group.

The arcpy.RefreshActiveView() call tells ArcMap that the map should be redrawn (usually because something has changed, such as labels).

See the documentation at RefreshActiveView which says:

Summary

Refreshes the active view and table of contents of the current map document.

Discussion

RefreshActiveView is only needed if you want to see the active view of the current map document updated.

If ArcMap did automatically refresh the view each time an arcpy.mapping function was executed, it could end up refreshing the view multiple times during the script (eg, after you added the group layer, after you added the layer to the group and after you turned labels on). Each refresh takes time, and in some maps this can be a LOT of time. Therefore, it is up to you to decide when the map needs refreshing.

The code I tested with (in a script tool in ArcMap 10.7.1) is below:

import arcpy

mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd)[0]
groupLyr = arcpy.mapping.Layer(r'C:\Group.lyr')

arcpy.mapping.AddLayer(df,groupLyr)
currentGroup = arcpy.mapping.ListLayers(mxd,"Group")[0]

source_fc = arcpy.mapping.Layer(r'C:\fGDB.gdb\FeatureClass')
arcpy.mapping.AddLayerToGroup(df, currentGroup, source_fc, "BOTTOM")

lyr = arcpy.mapping.ListLayers(mxd,"FeatureClass")[0]
lyr.showLabels = True

arcpy.RefreshActiveView()
3
  • This question is quite old, but just for the record - refreshing the view does not solve the problem in case of a layer inside a group, as in the example in the code above.
    – isshp
    Dec 11, 2019 at 9:51
  • Ah, I didn't notice that this was a zombie question from years ago. Nevertheless, I have copied your code into a script tool, and it works for me (in ArcMap 10.7.1). Labels appear correctly as expected for the feature class that is within the group. I'll add my complete code to my answer (it's basically your code, with the placeholder strings replaced with actual real strings and the RefreshActiveView() at the end). Is (was) your code being run in a script tool? Dec 11, 2019 at 21:16
  • My code was running in a script tool back then (ArcMap 10.1), but I tried it directly from the Python window now (ArcMap 10.5.1) and it still didn't work. Maybe it was fixed in one of the later versions.
    – isshp
    Dec 15, 2019 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.