1

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'     
    mxd.save()
del mxd

but i get an error:

>>> 
antiquities__55-30__403.mxd

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
>>> 
9
  • What is the rest of your error? Did you get it from running the precise code snippet that you have presented here? Commented Dec 23, 2015 at 7:57
  • Now i added the full error and i get it from this precise code presented here. Commented Dec 23, 2015 at 8:12
  • I think you should print lyr.name immediately before if lyr.name == u"allHa": to see what hint that gives. Commented Dec 23, 2015 at 8:15
  • i got the same error Commented Dec 23, 2015 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. Commented Dec 23, 2015 at 16:35

2 Answers 2

2

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
    else:
        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 '
2
  • 1
    Ryan, i get "True" while running the two line code Commented Nov 28, 2016 at 12:24
  • then its a group layer and my code above applies. Commented Oct 31, 2017 at 16:22
2

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 <<...>>

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.