Monday 25 September 2017

modelbuilder - arcpy Tool works in debug mode but not in normal model


When I try to run the following simple script from arcmap it fails but when run using right--click debug and then F9 (debug) in python scripter it works fine. I don't understand why???


enter image description here


# Modified by: Georgec Corea (ATGIS) coreagc@gmail.com

# Author: ESRI
# Date: July 5, 2010
# Version: ArcGIS 10.0
# Purpose: This script will print one or more map documents to a local printer.
# The script is intended to run within a script tool. There are two

# parameters:
# 1) Select Map Documents to Print,
# 2) Select Output Printer (auto populated using a validation script)
#
#Notes: The print order of the MXDs is based on how they are entered. The MXD
# at the top of the list is first followed by those below it.

import arcpy, string, datetime, shutil, os
import arcpy.mapping as MAP


#Read input parameters from script tool
MXDList = string.split(arcpy.GetParameterAsText(0), ";")
#printer = arcpy.GetParameterAsText(1)

count=0
#Loop through each MXD and print
for MXDPath in MXDList:
count=count+1
arcpy.AddMessage(str(count)+'. Working on '+str(MXDPath)+' @ '+str(datetime.datetime.now())+"\n")
## try:

mxd=arcpy.mapping.MapDocument(MXDPath)
outPDFpath = mxd.filePath[:mxd.filePath.rfind('.')]+'.pdf'
ddp = mxd.dataDrivenPages
#MXD = MAP.MapDocument(mxd)
#arcpy.AddMessage(ddp, MXD)
if str(os.path.isfile(outPDFpath))=='False':
outputPDF = arcpy.mapping.PDFDocumentCreate(outPDFpath)
mxd.dataDrivenPages.exportToPDF(outPDFpath)
#arcpy.mapping.ExportToPDF(mxd, outPDFpath)
name='c:\\temp\\'+outPDFpath[outPDFpath.rfind('\\'):]

shutil.copy2(outPDFpath,name)
arcpy.AddMessage('Printed '+str(outPDFpath)+' @ '+str(datetime.datetime.now())+"\n")
else:
arcpy.AddMessage('File Exists '+str(outPDFpath)+' skipped pdf creation @ '+str(datetime.datetime.now())+"\n")
## except:
## errorm=arcpy.GetMessages()
## arcpy.AddMessage(' Error creating file...' +str(outPDFpath)+'\n'+str(errorm)+'\n'+' continuing...')

#MAP.PrintMap(MXD, printer)
#Remove variable reference to file

del mxd

When run in debug


enter image description here



Answer



I changed the tool to run only in the foreground and after that it works fine. It would be nice to understand what part of the script can not be run in background mode in a tool.


Thanks Get Spatial and Nicksan for your input.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...