I'm relatively new to Python coding. I'd like to write a script tool for turning 'label features' on within an ArcGIS model. However, the script I've written won't recieve input data.
Other example scripts for labelling in this forum have specified layer file paths or used 'listLayers' within an mxd to source the input. However, I need this to take an input connection within model builder (so it can be re-used with different data without modifying the code). So I'm using 'GetParameterAsText'.
import arcpy inputlayer = arcpy.GetParameterAsText(0) label_field = arcpy.ListFields(inputlayer) lyr = arcpy.mapping.Layer(inputlayer) lyr.supports("LABELCLASSES") lyr.labelClasses.expression = "Abt + label_field" lyr.showLabels = True arcpy.RefreshActiveView()
I've been attempting to use this on polygon features and also lines. Specifically, the problems are:
- When I run it as a standalone script tool, I cannot drag a layer (feature classes) into the input from the TOC
- When I put the script into the model builder, it refuses to connect feature classes or feature layers as inputs.
Is there a better option than GetParameterAsText?
Or is the code incorrect or incomplete?
Is it perhaps an issue with how I've set up the script tool within ArcMap?