I work with several MXD'S and i try to update a group layer named "allHa" containing several layers with this code:

import arcpy,os,sys,string
import arcpy.mapping
from arcpy import env

env.workspace = r"D:\PROJECTS\road_20\gis"
sourceLayer = arcpy.mapping.Layer(r"D:\PROJECTS\road_20\gis\layers\16_12_15\allHa.lyr")
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname 
    mxd = arcpy.mapping.MapDocument(r"D:\PROJECTS\road_20\gis\\" + mxdname)
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
    updateLayer = arcpy.mapping.ListLayers(mxd, "allHa", df)[0]
    dfList = arcpy.mapping.ListDataFrames(mxd, "*")
    for df in dfList:
        for lyr in arcpy.mapping.ListLayers(mxd, "", df):                                   
            if lyr.name == u"allHa":
                 arcpy.mapping.UpdateLayer(df, lyr, sourceLayer, True)
                 print 'UpdateLayer'     
del mxd

but i get an error:


Traceback (most recent call last):
  File "C:\Program Files\CCleaner\yaron\shonot\software\---gis---  \tools\YARON_SCRIPTS\UpdateLayer 1 df.py", line 16, in <module>
    arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\utils.py",    line 182, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\mapping.py",     line 1897, in UpdateLayer
    tl._update(rl, symbology_only)
    ValueError: LayerObject: Unexpected error
  • What is the rest of your error? Did you get it from running the precise code snippet that you have presented here? – PolyGeo Dec 23 '15 at 7:57
  • Now i added the full error and i get it from this precise code presented here. – newGIS Dec 23 '15 at 8:12
  • I think you should print lyr.name immediately before if lyr.name == u"allHa": to see what hint that gives. – PolyGeo Dec 23 '15 at 8:15
  • i got the same error – newGIS Dec 23 '15 at 9:19
  • 1
    could there be an issue with using for df in dfList, since you already declared a df variable above that? would you mind terrible marking up your code a little describing what certain lines are trying to do? i'm curious as to why you're listing all the dataframes after grabbing the first one. – CSB Dec 23 '15 at 16:35

You will get this exact error if you try to run UpdateLayer with a source layer and update layer with different geometry types or if one of the layers is a group layer without any symbology of its own.

What's probably happening is your allHa.lyr file actually contains a group layer with the layer you want to update from as a child to that layer.

run this code and tell me what you get.

sourceLayer = arcpy.mapping.Layer(r"D:\PROJECTS\road_20\gis\layers\16_12_15\allHa.lyr")
print sourceLayer.isGroupLayer

if your source layer, update layer or both are a group layer then UpdateLayer will raise an error.

EDIT: avoid and safeguard against this like so:

# if our layer file contains a group layer as the root layer then we have to
# find a non-group layer to use as the source. This code assumes there is
# any level of group layer nesting and only one layer with symbology.
# it can be avoided if you have control over the .lyr file and can simply save the leaf node(non group layer node) as its own .lyr file 

def findLeafLayer(groupLayer):
    if groupLayer.isGroupLayer:
        for layer in arcpy.mapping.ListLayers(groupLayer):
            if not layer.isGroupLayer:
                return layer
        return groupLayer

sourceLayer = findLeafLayer(arcpy.mapping.Layer(r"D:\PROJECTS\road_20\gis\layers\16_12_15\allHa.lyr"))

for df in dfList:
    lyr = findLeafLayer(arcpy.mapping.ListLayers(mxd, "allHa", df)[0])                                  
    arcpy.mapping.UpdateLayer(df, lyr, sourceLayer, True)
    print 'Updatedlayer '
| improve this answer | |
  • 1
    Ryan, i get "True" while running the two line code – newGIS Nov 28 '16 at 12:24
  • then its a group layer and my code above applies. – Ryan Oct 31 '17 at 16:22

I think Ryan is right. A group layer cannot be updated. Instead, update the layers in the group. So you need to expand your for-each loop.

if lyr.name == u"allHa":
    for <<layer>> in <<grouplayer>>:
        <<update layer>>`

Just edit the <<...>>

| improve this answer | |
  • how do i integrate those lies in my code? – newGIS Nov 27 '16 at 14:26
  • just add this line of code for <<layer>> in <<grouplayer>>: under the already existing if-statement if lyr.name == u:"allHa": – Stefan Nov 28 '16 at 9:36
  • but what to write in the for loop? i don't understand. what are the <<<>> signs? – newGIS Nov 28 '16 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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