Solved! Go to Solution.
if layer.isGroupLayer: pass # skip else: #replace data path
import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd") for lyr in arcpy.mapping.ListLayers(mxd): if lyr.supports("DATASOURCE"): if lyr.dataSource == r"C:\Project\Data\Parcels.gdb\MapIndex": lyr.findAndReplaceWorkspacePath(r"Data", r"Data2") mxd.saveACopy(r"C:\Project\Project2.mxd") del mxd
if layer.isGroupLayer: pass # skip else: #replace data path
import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd") for lyr in arcpy.mapping.ListLayers(mxd): if lyr.supports("DATASOURCE"): if lyr.dataSource == r"C:\Project\Data\Parcels.gdb\MapIndex": lyr.findAndReplaceWorkspacePath(r"Data", r"Data2") mxd.saveACopy(r"C:\Project\Project2.mxd") del mxd
if lyr.supports("DATASOURCE"):
import arcpy lyr = arcpy.mapping.Layer(r"C:\Temp\Group1.lyr") #Nested group layer file for layer in lyr: print layer.dataSource
Traceback (most recent call last): File "<interactive input>", line 2, in <module> File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\_base.py", line 70, in _get return convertArcObjectToPythonObject(getattr(self._arc_object, attr_name)) AttributeError: LayerObject: Get attribute dataSource does not exist
import arcpy try: mxd = arcpy.mapping.MapDocument(r"C:\test\mymap.mxd") layers = arcpy.mapping.ListLayers(mxd) for layer in layers: if not layer.isGroupLayer: if layer.supports("DATASOURCE"): print layer.dataSource except: print "Exception ...."
I am also attempting to re-source a bunch of layer files, some of which are group layers, in a folder. I started with the same script found above, which the OP also posted to GIS Stack Exchange and someone there added a recursive function to parse through nested group layers. Seemed like the perfect solution. Except the script errors out as soon as it hits a group layer. More specifically: as soon as it tries to save the first layer within a group layer, it returns an error: "No filename set". I believe this is because the .name and .longName attributes of the layer variable are the same even though they shouldn't be if the layer is inside a group layer. Thus, the conditional statement on line 27 (if singlelayer.name != singlelayer.longName:) is never met and all layers move on to "else". Something in this script is causing .name and .longName to become identical because they are just fine if I check for them in a separate, simpler script. I am using ArcGIS 10.2, but I believe the original script was written for 10.0. Any ideas what could be causing this?
import arcpy, datetime, os, io, sys def walknestedgroups(layerlist,srcString,repString,pathfull,outputFile): if layerlist.isGroupLayer: #Cycle through grouplayer print "%s is a group layer" % layerlist for layer in layerlist: print "%s is a layer inside %s" % (layer,layerlist) print "name: " + layer.name print "long name: " + layer.longName #This will continue to cycle through and test for group layers walknestedgroups(layer,srcString,repString,pathfull,outputFile) else: print "%s is a single layer" % layerlist #Once a non-group layer is returned, head through the rest of original script singlelayer = layerlist if singlelayer.supports("DATASOURCE"): dataSourceOrig = str(singlelayer.dataSource) if dataSourceOrig.find(srcString) >= 0: dataSourceNew = dataSourceOrig.replace(srcString, repString) #This comparison test for layers in a group layer vs those not in. The .longName property includes all group layers as part of path if singlelayer.name != singlelayer.longName: outputFile.write(pathfull + "\t" + singlelayer.longName + "\t" + singlelayer.name + "\t" + dataSourceOrig + "\t" + dataSourceNew + "\t" + "GROUP LAYER SOURCE" "\n") singlelayer.findAndReplaceWorkspacePath(sourceString, replacementString, True) print "%s group source changed" % singlelayer singlelayer.save() print "%s saved" % singlelayer else: outputFile.write(pathfull + "\t" + "Non Group Layer" + "\t" + singlelayer.longName + "\t" + dataSourceOrig + "\t" + dataSourceNew + "\t" + "SINGLE LAYER SOURCE" "\n") singlelayer.findAndReplaceWorkspacePath(srcString, repString, True) print "%s source changed" % singlelayer singlelayer.save() print "%s saved" % singlelayer return layerlist print "loop exited for " + layerlist + " group layer" try: #Read input parameters from GP dialog # Prod Setting... folderPath = r"G:\JThomas\LayerFiles\Vector\REAL_PROPERTY" output = r"G:\JThomas\LayerFiles\logfile.log" sourceString = "" replacementString = r"P:\LE\Connections\LE_CAC @ CLJN.sde" #Create an output file outFile = open(output, "w") #Loop through each "LYR" file count = 0 for path, dires, files in os.walk(folderPath): # for filename in os.listdir(folderPath): for filename in files: # fullpath = os.path.join(folderPath, filename) fullpath = os.path.join(path, filename) if os.path.isfile(fullpath): if filename.lower().endswith(".lyr"): groupLayerName = "" #Reference LYR layerFile = arcpy.mapping.Layer(fullpath) #Iterate through layer list and all group layers. Return single layer for totallayer in arcpy.mapping.ListLayers(layerFile): #This function will take each layer in the .lyr file, cycle through any group layers then apply path changes to individual layers singlelayer = walknestedgroups(totallayer,sourceString,replacementString,fullpath,outFile) del layerFile outFile.close() #Open the resulting text file os.startfile(output) #Delete variables that reference data on disk del outFile except Exception, e: import traceback map(arcpy.AddError, traceback.format_exc().split("\n")) arcpy.AddError(str(e)) print "Error: %s" % e
Edit: not sure what the deal is with the code block. It looked fine in the preview and now it's all one line. I've attached it as a separate file. Sorry for any inconvenience viewing it!
I'm having the exact same issues and cannot figure out why I keep getting the error 'no filename set'. It is discouraging that ESRI would allow you to create group .lyr files but not allow you to modify them the same way.
I am having the same issue. Is this a new bug that a previously working script now fails?