I am working with another user, and our previous question was Formatting dependent parameters in Python Toolbox?
We gave up on trying to auto populate the drop down and instead are having the user input which band(s) they would like to explode. Everything works except when we try to break the tool by not selecting any output format type. We want an error message to occur when this happens, but we have other problems in the code preventing us from seeing the error message. But these problems don't affect the tool's running if we select the output format.
The error we get is:
UnboundLocalError: local variable 'assignment' referenced before assignment.
Here is all of the code we have tried. We tried adding global in front of our variable, but that didn't work.
import arcpy,os,sys,string class Toolbox(object): def __init__(self): self.label = "Exploding Rasters" self.alias = "ER" # List of tool classes associated with this toolbox self.tools = [ExplodeRasters] class ExplodeRasters(object): def __init__(self): self.label = "Explode Rasters" self.description = " This tool will take an input multiband raster and " +\ " extract all of its individual bands, saving these " +\ " bands as separate single band rasters.Exploding Rasters" + \ " tool is everythying you've ever hoped for and dreamed of." + \ " It's perfection." def getParameterInfo(self): # Input Features parameter in_features = arcpy.Parameter( displayName="Input Raster", name="in_features", datatype="DERasterDataset", parameterType="Required", direction="Input") # create select all bands button all_bands = arcpy.Parameter( displayName="Select all bands", name="Select_All", datatype="Boolean", parameterType="Required", direction="Input") #Raster types options TIFF = arcpy.Parameter( displayName="TIFF", name ="TIFF", datatype="Boolean", parameterType="Optional", direction="Input") BMP = arcpy.Parameter( displayName="BMP", name="bmp", datatype="Boolean", parameterType="Optional", direction="Input") PNG = arcpy.Parameter( displayName="PNG", name="png", datatype="Boolean", parameterType="Optional", direction="Input") # Range of Desired Bands parameter select_bands = arcpy.Parameter( displayName="Select Bands", name="Select_Bands", datatype="String", parameterType="Optional", direction="Input") #select bands for NDVI band parameters NDVI_red = arcpy.Parameter( displayName="Select a red band for NDVI calculation", name="NDVI_bRed", datatype="DERasterDataset", parameterType="Optional", direction="Input") NDVI_NIR = arcpy.Parameter( displayName="Select a NIR band for NDVI calculation", name="NDVI_BNIR", datatype="DERasterDataset", parameterType="Optional", direction="Input") #out directory OutDir = arcpy.Parameter( displayName="Output Workspace", name="Out_Directory", datatype="DEWorkspace", parameterType="Required", direction="Input") # Create filename output prefix prefix = arcpy.Parameter( displayName="Output filename prefix", name="output_prefix", datatype="GPString", parameterType="Required", direction="Input") parameters = [in_features, all_bands, select_bands, NDVI_red, NDVI_NIR, prefix, OutDir, TIFF, BMP, PNG] return parameters def execute(self, parameters, messages): in_raster=parameters[0].valueAsText ALL = parameters[1].valueAsText Bands = parameters[2].valueAsText Red = parameters[3].valueAsText NIR = parameters[4].valueAsText prefix = parameters[5].valueAsText Out_Dir = parameters[6].valueAsText TIFF = parameters[7].valueAsText BMP = parameters[8].valueAsText PNG = parameters[9].valueAsText messages.addMessage(Bands) messages.addMessage("INPUT RASTER=" +in_raster) messages.addMessage("\n" + "Yay, you're doing great!") #necessary encouragement for users arcpy.env.workspace=in_raster arcpy.env.overwriteOutput = True if TIFF == "true": #output tif if TIF box is checked assignment = ".tif" ending = "TIFF" if BMP == "true": #output bmp if BMP box is checked assignment = ".bmp" ending = "BMP" if PNG == "true": #output png if PNG box is checked assignment = ".png" ending = "PNG" if TIFF and BMP and PNG == "false": messages.addWarningMessage("You forgot to select an extension.") if ALL == "true": ListBandNames = arcpy.ListRasters() for band in ListBandNames: bndDesc = arcpy.Describe(band) NoData = bndDesc.noDataValue outRaster = os.path.join(Out_Dir, prefix + band + assignment) arcpy.CopyRaster_management(band, outRaster, format = ending, nodata_value = NoData) elif parameters[2].value: bands_in_input = [] ListBandNames = arcpy.ListRasters() for band in ListBandNames: bndDesc = arcpy.Describe(band) bands_in_input.append(band) NoData = bndDesc.noDataValue UserInput_Select = Bands.split(",") UpdatedInput =["Band_" + band for band in UserInput_Select] result = [] for element in bands_in_input: if element in UpdatedInput: result.append(element) outRaster = os.path.join(Out_Dir, prefix + element + assignment) arcpy.CopyRaster_management(element, outRaster, format = ending, nodata_value = NoData) else: messages.addMessage("No Output Format Selected") return
assignmentfor example to "" empty string before you call it,for example putassignment=""beforeif TIFF == "true"