Two issues creating a script tool

4559
25
07-17-2015 07:35 AM
BradJones
New Contributor III

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..

meter = r"V:\WATER_FACILITY\WATER_FACILITY.gdb\MASTER_METER"

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"

root = r"V:\WATER_FACILITY\WATER_FACILITY.gdb"

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) # Create directory path for featureclasses

    symPath = os.path.join(symRoot, files + ".lyr") # Create directory path for .lyr files

    arcpy.MakeFeatureLayer_management(paths, layers, pws) # Make all the layers from "files" list

    arcpy.ApplySymbologyFromLayer_management(layers, symPaths) # 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 

arcpy.RefreshActiveView()

#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

0 Kudos
25 Replies
BradJones
New Contributor III

For some reason the script didn't post?

0 Kudos
IanMurray
Frequent Contributor

Hi Brad,

You need the advanced editor syntax formatting to make code more readable.

See: Posting Code blocks in the new GeoNet

0 Kudos
BradJones
New Contributor III

Thanks, that helped. 

0 Kudos
BradJones
New Contributor III

Now when I run the original script in the Python window in the mxd i'm getting this error: NameError: name 'paths' is not defined.  This code ran fine this morning

0 Kudos
IanMurray
Frequent Contributor

In Line 29, I think paths got changed to path, so paths is no longer defined

0 Kudos
BradJones
New Contributor III

ERROR 000732: Input Features: Dataset V does not exist or is not supported is the error I get when I change 'path' to 'paths'.

This is a serious WTF moment for me.

0 Kudos
IanMurray
Frequent Contributor

I think one of your file paths is getting truncated and just the first letter of the file is being used for a tool input, which isn't a valid dataset.  Did you get which line the error occured on?  You might want to debug using some print statements to make sure all your tool inputs are the values you think they are.

0 Kudos
BradJones
New Contributor III

Would I add print statements after each directory path?  that's what I did the error comes after they all print.

0 Kudos
IanMurray
Frequent Contributor

I would use print statements for every parameters being used in lines 29-32 to make sure they are all correct, since almost all the rest of your values are hard coded into your script.

0 Kudos