AnsweredAssumed Answered

Script Runs in interactive window in ArcMap but not when added to a tool box

Question asked by pmoulden on May 10, 2012
Latest reply on May 10, 2012 by ruthbowers
I am new to Python (been teaching myself the language only for the past 3 days and don't really have much of a programming background) and am working on a script that automates the process of creating a grid index for generating mapbooks, something I do quite often as a final deliverable at the engineering company I work for. The script uses the current environment settings to determine the output type and grabs the intersect features from the current "Layers" dataframe; the grid size is then determined from the dataframe size and position.  The idea is to create a 'one-click' process for developing an index grid.

The problem is that the code will run fine when run in the interactive python window in ArcMap but will not run when added to a script tool. Any Ideas?

It's still a bit messy and still needs to be cleaned up a good bit but here is what I have so far:

import arcpy from arcpy import env   environments = arcpy.ListEnvironments("workspace")  for environment in environments:     # As the environment is passed as a variable, use Python's getattr function      #   to evaluate the environment's value     #     envSetting = getattr(env, environment)      # Format and print each environment and its current setting     #     print "%-28s: %s" % (environment, envSetting)  desc = arcpy.Describe (envSetting) print desc.workspaceType wrkspc = desc.workspaceType print wrkspc  mxd = arcpy.mapping.MapDocument ("CURRENT") df = arcpy.mapping.ListDataFrames (mxd, "Layers") [0] fcList = arcpy.mapping.ListLayers (mxd)  if (desc.workspaceType == 'FileSystem'):     if arcpy.Exists ("gridIndexFeatures.shp"):         arcpy.Delete_management ("gridIndexFeatures.shp") else:     if arcpy.Exists ("gridIndexFeatures"):         arcpy.Delete_management ("gridIndexFeatures")  dfAsScale = df.scale dfAsRotation = df.rotation dfAsmapUnits = df.mapUnits dfAsExtent = df.extent dfXMax = dfAsExtent.XMax dfXMin = dfAsExtent.XMin dfYMax = dfAsExtent.YMax dfYMin = dfAsExtent.YMin  dfAsWidth = abs(abs(dfXMax) - abs(dfXMin)) dfAsHeight = abs(abs(dfYMin) - abs(dfYMax))  print dfAsExtent print dfXMax print dfXMin print dfYMax print dfYMin print dfAsWidth print dfAsHeight print dfAsRotation print dfAsmapUnits print fcList  # Set local variables if (desc.workspaceType == 'FileSystem'):     outFeatureClass = "gridIndexFeatures.shp"     inFeatures = fc     #inFeatures2 = "gridIndexFeatures.shp"     noIntersect = "INTERSECTFEATURE"     usePageUnit = "NO_USEPAGEUNIT"     scale = dfAsScale     polygonWidth = dfAsWidth     polygonHeight= dfAsHeight else:     outFeatureClass = "gridIndexFeatures"     inFeatures = fc     #inFeatures2 = "gridIndexFeatures"     noIntersect = "INTERSECTFEATURE"     usePageUnit = "NO_USEPAGEUNIT"     scale = dfAsScale     polygonWidth = dfAsWidth     polygonHeight= dfAsHeight      # Execute GridIndexFeatures arcpy.GridIndexFeatures_cartography(outFeatureClass, inFeatures, noIntersect, usePageUnit, scale, polygonWidth, polygonHeight)  if (desc.workspaceType == 'FileSystem'):     inFeatures = "gridIndexFeatures.shp" else:     inFeatures = "gridIndexFeatures"  fieldName1 = "Rotation" fieldPrecision = 18 fieldAlias = "Rotation" fieldName2 = "status" fieldLength = 10 expression = dfAsRotation  arcpy.AddField_management(inFeatures, fieldName1, "DOUBLE", fieldPrecision, "", "8", fieldAlias, "NULLABLE") arcpy.CalculateField_management (inFeatures,"Rotation", expression,"PYTHON") # Set dataframe rotation to rotation of grid df.rotation = expression

Outcomes