layer name and longName are identical when they shouldn't be

المشرف العام

Administrator
طاقم الإدارة
I am attempting to use a script to re-source a bunch of layer files in a folder (adapted from Unable to change data source path for group layer file):

import arcpy, datetime, os, io, sysdef 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 = arcpy.GetParameterAsText(0) output = arcpy.GetParameterAsText(1) sourceString = r"P:\Temp" # replacmentString = r"Z:\Temp" # #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 outFileexcept Exception, e: import traceback map(arcpy.AddError, traceback.format_exc().split("\n")) arcpy.AddError(str(e)) print "Error: %s" % eThe script works just fine until it hits a group layer. 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?



أكثر...
 
أعلى