I'm a Python beginner working with arcpy. I want to take a selected shapefile and add an area field to it, and then populate that field.
As of now, my code only works if I have the selected layer as a parameter input by the user. If I substitute that for the "selected layer" (which is commented out right now), the field is created, but it does not populate. I cannot figure out why.
What do I need to do in order to make this work?
import time, arcpy, sys, os, pythonaddins
## Set parameters
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
mxdName = os.path.basename(mxd.filePath)
directory = os.path.dirname(mxd.filePath)
layers = arcpy.mapping.ListLayers(mxd, "*", df)
arcpy.env.overwriteOutput = False
#####SelectedLayer = pythonaddins.GetSelectedTOCLayerOrDataFrame()
SelectedLayer = arcpy.GetParameterAsText(0)
f_name = "Area_ha"
f_type = "DOUBLE"
f_precision = 5
f_scale = ""
f_length = ""
f_alias = ""
f_null = "NULLABLE"
f_required = "NON_REQUIRED"
f_domain = ""
## Create Area field, calculate area in hectares
arcpy.AddField_management(SelectedLayer, f_name, f_type, f_precision,
f_scale, f_length, f_alias, f_null, f_required, f_domain)
areaexpression1 = "{0}".format("!SHAPE.area@HECTARES!")
arcpy.CalculateField_management(SelectedLayer, f_name, areaexpression1,
"PYTHON_9.3", "")
{}
button to format it as code.