AnsweredAssumed Answered

Improve my script so it toggle on/off layers after updating them

Question asked by AntoineCantinR on Apr 14, 2016
Latest reply on Apr 14, 2016 by AntoineCantinR


The following script works perfectly to update all mxds contained in one folder using a unique .lyr file as a reference to all layers (in groups and subgroups). All mxds have the same TOC composition, but the visibility of each layer depend of the mxd. My problem is that when I run the "update script", the .lyr file apply his parameter to the layers so it turns them OFF. The .lyr file contains only OFF layers because, as I said, the visibility of every layer is different in each map project. 


So I would love to find a way to integrate a few more lines of code in the script so it would list the layers visibility parameter (for every layers in the mxd), then do the update, finally toggle back ON the layers that were ON initially using the list.


I found something, but I can't find a way to integrate it in the "update script" (


Here the "update script":

import arcpy
import os

MxdFolderPath = arcpy.GetParameterAsText(0)
if MxdFolderPath == '#' or not MxdFolderPath:
    MxdFolderPath = "//"
MxdCount = 0

sourcelypath = arcpy.GetParameterAsText(1)
if sourcelypath == '#' or not sourcelypath:
    sourcelypath = "// .lyr/Autres formats/Couches.lyr"  # reference .lyr file
sourcely = arcpy.mapping.Layer(sourcelypath)

arcpy.env.workspace = MxdFolderPath
arcpy.AddMessage("   MXD PROCESSING")
mxdList = arcpy.ListFiles("*.mxd")
Mxdlen = len(mxdList)
if Mxdlen == 0:
    arcpy.AddMessage("   There are no mxd in: " + str(MxdFolderPath))
    arcpy.AddMessage("   There are " + str(Mxdlen) + " mxd in: " + str(MxdFolderPath))

    for mxd in mxdList:
        MxdCount = (MxdCount + 1)
        MxdPPath = os.path.join(MxdFolderPath, mxd)
        processingmxd = arcpy.mapping.MapDocument(MxdPPath)
        arcpy.AddMessage(" Mxd: " + str(MxdCount) + " :" + str(mxd))
        dflist = arcpy.mapping.ListDataFrames(processingmxd, "Couches_DF")  # Dataframe
        for df in dflist:
            arcpy.AddMessage(" df: " + str(
            layers = arcpy.mapping.ListLayers(processingmxd, "Couches", df)  # layers called Couches
            for layer in layers:
                arcpy.AddMessage("      Layer: " + str(layer))
                arcpy.mapping.UpdateLayer(df, layer, sourcely, False)
                arcpy.AddMessage("      Layer Updated")
        arcpy.AddMessage(" ")
arcpy.AddMessage(" End")


Can anyone help? Thanks in advance!