I need a 2nd set of eyes to do a sanity check, as I am not been able to figure out why a script that runs fine in pywin, does not run properly as a tool in a toolbox. Below is the script that runs in pywin without an issue. It walks through a tree of folders and adds any shapefiles to an empty existing mxd (The mxd is not open in arcmap). Each shape is added with the coordsys name append to the layer name.
import arcpy, os
# Set the source path
srcPath = r'C:\shpfiles'
# get the map document
mxd = arcpy.mapping.MapDocument(r'C\TEST.mxd')
# get the data frame
dataFrame = arcpy.mapping.ListDataFrames(mxd)[0]
dirList = os.walk(srcPath)
try:
for root, dirs, files in dirList:
for dir in dirs:
#Set the path to the shapefile folder...
path = os.path.join(root,dir)
#Set the workspace
arcpy.env.workspace = path
#Get a list of shapefiles in each dir...
featureclasses = arcpy.ListFeatureClasses()
#Cycle through the list of shpfiles...
for shp in featureclasses:
shpPath = os.path.join(path,shp)
coordsys = arcpy.Describe(shp).spatialReference
#Create a new layer name
newLayer = shp[:-4] + "_" + coordsys.name
#add a new layer in the map document
layer = arcpy.mapping.Layer(shpPath)
#add the layer to the map at the bottom of the TOC
arcpy.mapping.AddLayer(dataFrame, layer, "BOTTOM")
layer.name = newLayer
del layer, newLayer, coordsys
except:
print arcpy.GetMessages(2)
#save the mxd file
mxd.save()
del mxd
This works perfectly. The script loops through and loads all the shape files into TEST.mxd, with their projection name appended to the layer name.
Below is the same code, modified to run as a tool in toolbox.
import arcpy, os
# Set the source path
srcPath = arcpy.GetParameterAsText(0)
# get the map document
mxd = arcpy.mapping.MapDocument(arcpy.GetParameterAsText(1))
# Set the workspace
arcpy.env.workspace = srcPath
# get the data frame
dataFrame = arcpy.mapping.ListDataFrames(mxd)
dirList = os.walk(srcPath)
try:
for root, dirs, files in dirList:
for dir in dirs:
#Set the path to the shapefile...
path = os.path.join(root,dir)
#Set the workspace
arcpy.env.workspace = path
#Get a list of shapefiles in each dir...
featureclasses = arcpy.ListFeatureClasses()
#Cycle through the list of shpfiles...
for shp in featureclasses:
shpPath = os.path.join(path,shp)
coordsys = arcpy.Describe(shp).spatialReference
newLayer = shp[:-4] + "_" + coordsys.name
#add a new layer in the map document
layer = arcpy.mapping.Layer(shpPath)
print arcpy.AddMessage(layer)
#add the layer to the map at the bottom of the TOC
arcpy.mapping.AddLayer(dataFrame, layer, "BOTTOM")
layer.name = newLayer
del layer, newLayer
except:
print arcpy.GetMessages(2)
#save the mxd file
mxd.save()
del mxd
Running this tool in ArcCatalog does not do anything; none of the shapefiles are loaded into the mxd. Only one of the shapefiles is looped through and the entire process finishes in < 1sec, and TEST.mxd is empty.
Adding print statements and reming out the code, the problem is isolated to:
arcpy.mapping.AddLayer(dataFrame, layer, "BOTTOM")
When this line of code is commented out, the tool script loops through and prints out every shapefile it encounters in the tree. Why would AddLayer stop the loop?
Am I missing some extra code to enable this to run as an arcToolbox tool script?