Two issues creating a script tool

Discussion created by bradleyglennjones on Jul 17, 2015
Latest reply on Jul 21, 2015 by LukeW

I have this script below.  When the three arguments are set as variables and I run the script in the ArcMap Python window to test it it runs perfectly.  I created a tool for this script.  In the parameters for the tool properties I have the Data Types for the 3 arguments are set as "SQL expressions" and all the data types for the feature layers created are "Feature Layers" with a "Type: Derived" and "Direction: Output".


First problem: when I run the tool with the script as is i get this error 'RuntimeError: LayerObject: Set attribute showLabels does not exist'. I remove the code block that applies the labels then I get this error 'ValueError: Unknown value for Extent argument'. Running the script in the mxd both code blocks work perfectly.


Second Problem: I remove both code blocks from the script and run the tool and nothing happens.  No errors. Just doesn't add layers.


# Import modules
import os, arcpy

# Set arguments
pws = arcpy.GetParameterAsText(0)
ass_pws = arcpy.GetParameterAsText(1)
mtr_recpws = arcpy.GetParameterAsText(2)

# Set variables  
sources = r"V:\Source Water Protection\SWAP Model\SWAP.code_newrasters\GIS\SWAP.gdb\sources" # Will need to change if used outside of SWPP staff because of directory permissions. "Sources"  feature class is in WATER_FACILITY.gdb
assessment = r"V:\Source Water Protection\SWAP Model\SWAP.code_newrasters\GIS\SWAP.gdb\assessment_areas" # Will need to change if used outside of SWPP staff because of directory permissions. "Assessments" feature class not in WATER_FACILITY.gdb..
files = ["BACTI", "DBP", "DBP_STAGE2", "OFFICE", "PRV", "PUMP_STATION", "SERVICE_AREA_COMBO", "TANK", "VALVE_MISCELLANEOUS", "WTP"] # list all names used for feature classes ans .lyr files. 
layers = ["Bacti", "DBP", "DBP Stage 2", "Office", "PRV", "Pump Station", "Service Area", "Tank","Valve", "Water Treatment Plant"]
src_layer = "Source"
ass_layer = "Assessment Area"
mtr_layer = "Master Meter"
src_symb = r"V:\Source Water Protection\PWS_ID_Tool\SOURCE.lyr"
ass_symb = r"V:\Source Water Protection\PWS_ID_Tool\ASSESSMENT.lyr"
mtr_symb = r"V:\Source Water Protection\PWS_ID_Tool\MASTER_METER.lyr"
symRoot = r"V:\Source Water Protection\PWS_ID_Tool"

# Create directory paths, make feature layers, and apply symbologies.
for i, file in enumerate(files):
    path = os.path.join(root, files[i]) # Create directory path for featureclasses 
    symPath = os.path.join(symRoot, files[i] + ".lyr") # Create directory path for .lyr files
    arcpy.MakeFeatureLayer_management(paths[i], layers[i], pws) # Make all the layers from "files" list
    arcpy.ApplySymbologyFromLayer_management(layers[i], symPaths[i]) # Apply the symbologies from "symNames" list.
arcpy.MakeFeatureLayer_management(assessment, ass_layer, ass_pws)
arcpy.ApplySymbologyFromLayer_management(ass_layer, ass_symb)
arcpy.MakeFeatureLayer_management(meter, mtr_layer, mtr_recpws)
arcpy.ApplySymbologyFromLayer_management(mtr_layer, mtr_symb)
arcpy.MakeFeatureLayer_management(sources, src_layer, pws) # Ordered to have the "Source" layer in the 0 position.
arcpy.ApplySymbologyFromLayer_management(src_layer, src_symb)

# Apply label
mxd = arcpy.mapping.MapDocument("CURRENT") # Reference to current open map document. Could insert path.
layer = arcpy.mapping.ListLayers(mxd, "")[0] # Indexing for first layer in table of contents. "Make feature layers" functions ordered so "Source" is in 0 position.
if layer.supports("LABELCLASSES"):  
     for lblclass in layer.labelClasses:  
         lblclass.showClassLabels = True         
lblclass.expression = '"%s" & [SOURCE_ID] & "%s"' % ("<CLR red='255'><FNT size = '12'>", "</FNT></CLR>") 
layer.showLabels = True  

#Apply extent from "Source" layer
mxd = arcpy.mapping.MapDocument("CURRENT") # Using current map, can also use a path to an mxd here  
df = arcpy.mapping.ListDataFrames(mxd)[0]  
lyr = arcpy.mapping.ListLayers(mxd, "", df)[0]  
ext = lyr.getExtent()  
df.extent = ext 


Message was edited by: Brad Jones