Updating group layer symbology in arcpy

3830
7
06-20-2016 12:58 PM
AndrewGehlot
New Contributor II


I'm new to python, but I'm trying to apply the symbology from one layer to a group of others without manually importing each one. I could build a model using the ApplySymbologyFromLayer_management tool, but it does not work if there are features (e.g. polygons) missing in the layer; but I want these feature classes present across all layers because I am drawing them in. I was able to use the following python script to apply symbology to a single layer from another:

>>> import arcpy

>>> mxd = arcpy.mapping.MapDocument("Current")

>>> df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

>>> updateLayer = arcpy.mapping.ListLayers(mxd, "T33001700", df)[0]

>>> sourceLayer = arcpy.mapping.Layer(r"For Review\T23001510")[0]

Runtime error

Traceback (most recent call last):

  File "<string>", line 1, in <module>

TypeError: 'Layer' object does not support indexing

>>> sourceLayer = arcpy.mapping.Layer(r"T23001510")

>>> arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True)

#But if I try to change the update layer to:

>>> updateLayer = arcpy.mapping.ListLayer(mxd, "In Progress", df) #where "In Progress" is the group of layers I want to change the symbology of

#then I get the error:

Runtime error

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\utils.py", line 182, in fn_

    return fn(*args, **kw)

  File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\mapping.py", line 1888, in UpdateLayer

    assert isinstance(update_layer, Layer)

AssertionError

0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus

sourceLayer = arcpy.mapping.Layer(r"For Review\T23001510")[0]

did you try just removing [0] from the line?

0 Kudos
AndrewGehlot
New Contributor II

Thanks for the reply.

Yes I did and I get the error:

Runtime error

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\utils.py", line 182, in fn_

    return fn(*args, **kw)

  File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\mapping.py", line 1888, in UpdateLayer

    assert isinstance(update_layer, Layer)

AssertionError

I'm not sure if there's some way to identify the group layer and then iterate through each one?

0 Kudos
DanPatterson_Retired
MVP Emeritus

From here ListLayers—Help | ArcGIS for Desktop group layers are supposed to be treated just like layers

So have a read... expecially this

When working with layer files, the data_frame parameter should not be used because layer files don't support data frames; if it is, it will be ignored. The Layer() function is for referencing layer (.lyr) files stored on disk.

Group layers are treated just like layers. The index values are simply generated from top to bottom as they appear in the table of contents or the way they would appear in a layer file. The same applies if a group layer is within another group layer. A map document with a single group layer with three layers within it will return a Python list of four layer objects, the group layer being the first. One way of determining if a layer is inside a group layer is to interrogate the longName property. A layer's longName will include the group layer name as part of the name.

0 Kudos
AndrewGehlot
New Contributor II

I had just read that earlier--it says group layers are treated just like layers, which is strange because it will identify and update my single layer within the group if I set a path to it (mxd, "T33001680", df), but it will not if I identify the entire group that the layer (as well as several other layers of the same type) are contained in (mxd, "In Progress", df)

0 Kudos
DanPatterson_Retired
MVP Emeritus

its the dataframe property should not be used ... reference

0 Kudos
AndrewGehlot
New Contributor II

But when I run the arcpy function for UpdateLayer, it asks for a dataframe. Also, when I use a single layer and identify a dataframe ("df") it works. I've tried leaving that field blank for the updateLayer, but it still returns the error.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I think you might want to throw some print statements in and loops if necessary, to see what you are actually getting, so referencing the mxd and examining the coding options here should help.  It is hard to debug remotely without fuller information as to what is being returned other than the error when what is returned isn't right

ListLayers—Help | ArcGIS for Desktop

0 Kudos