Select to view content in your preferred language

Setting Script Tool Parameters - Python in ModelBuilder

8084
18
05-11-2015 12:35 AM
NickBauer
Deactivated User

Hi All,

I am wondering if you could help with following. Essentially it relates to the following help article "Setting Script Tool Parameters" ArcGIS Help (10.2, 10.2.1, and 10.2.2)

I have a used ModelBuilder to create a geoprocessing workflow. However for part of this I need to run a script within the model to dissolve some features within a layer (the inbuilt modelbuilder geoprocessing function for this does not give me multi-part polygons, despite this being a supposed option).

Thanks in advance (Using ArcGIS 10.2)

Nick

Essentially the problem I encounter is that when I run the model ArcGIS goes in to 'not responding mode' and does not recover. I suspect there is something wrong with wither my python script or the way I am passing variables between modelbuilder and my python script?

Python Script - Dissolve2

import arcpy

arcpy.env.workspace = "C:/Data/ArcGIS/Default.gdb/Dissolve1"

inLayer = arcpy.GetParameter(0)

outLayer = "C:/Data/ArcGIS/temp/OutDisolve.shp"

arcpy.Dissolve_management(inLayer, outLayer, "Id", "","MULTI_PART", "")

arcpy.SetParameter(1, OutLayer)

I have provided screenshots below

1. A simplified view of the model

Model.JPG

InLayer.JPGOutLayer.JPG

2. Parameter Input and Output

0 Kudos
18 Replies
FreddieGibson
Honored Contributor

Does the Model freeze up with this particular dataset?

0 Kudos
NickBauer
Deactivated User

Thanks Freddie,

No I have tried with several different and simplified datasets and this does not help.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Check the capitalization of outLayer vs. OutLayer. These are two different things according to Python.

Try:

arcpy.SetParameter(1, outLayer)
NickBauer1
Deactivated User

Thanks picking this up Darren,

I have tried this but it does not seem to be the source of my issue. I am still trying to solve this.

Cheers

Nick

0 Kudos
curtvprice
MVP Esteemed Contributor

> (the inbuilt modelbuilder geoprocessing function for this does not give me multi-part polygons, despite this being a supposed option).

I'm kind of confused by this because the Dissolve tool is the same tool, whether called from ModelBuilder, a Python script, or run from a toolbox in ArcMap interactively.

Another option to the overhead of setting up a script tool is to use the Calculate Value tool in ModelBuilder. Especially handy for situations where the logic of ModelBuilder validations and preconditions get complicated or problematic, but you don't want to bother with a python script tool.

Expression

proc(r"%InLayer%", r"%OutName%")

Code Block

import os
import arcpy
def  proc(lyr, outname):
  output = os.path.join(
    os.path.dirname(arcpy.Describe(lyr).catalogPath),
    outname)
  arcpy.Dissolve_management(lyr, output)
  return output

Data Type:

Feature Class

NickBauer1
Deactivated User

Curtis Price wrote:

> (the inbuilt modelbuilder geoprocessing function for this does not give me multi-part polygons, despite this being a supposed option).

I'm kind of confused by this because the Dissolve tool is the same tool, whether called from ModelBuilder, a Python script, or run from a toolbox in ArcMap interactively.

Hi Curtis, yes I agree that the underlying tools are (should) be the same... but when I put the dissolve action into modelbuilder I can't seem to get it make multi-polygons (only single-part), despite this being an obvious option to specify? Is this your experience?

With regards to the "Calculate Value Tool" should the expression have an r before %OutName%? This does seem like a more simple way to go. Do you know of any other examples I can follow?

Thanks

Nick

0 Kudos
curtvprice
MVP Esteemed Contributor

No I have not seen this behavior. I'm wondering if there is a logic thing going on here.

The raw strings are only needed when you are passing a model variable  that is a path (like a dataset path) into the function used by Calculate Value. The r is needed to ensure the backslashes are always interpreted as path delimeters instead of part of an escape sequence (for example so "path\temp" isn't intepretes as "path" + <tab> + "emp").

0 Kudos
JohnBrodnicki
Occasional Contributor

I'm not sure if it will help but you may want to try changing arcpy.GetParameter(0) to arcpy.GetParameterAsText(0). If that doesn't help you may want to use a

try:

     code

except arcpy.ExecuteError:

     msg = arcpy.GetMessage(0)

     arcpy.AddError(msgs)

This might show what's going wrong. If not, you could import os and traceback and write a more detailed "except" to catch other python errors.

curtvprice
MVP Esteemed Contributor

GetParameterAsText and SetParameterAsText should always be used unless you have a good reason to use the GetParameter and SetParameter functions. The GP environment is really setup to move data around as strings (think XML) and generally you'll be happier just going with that flow.

As for trapping exceptions and traceback - always a great idea. There are some nice code samples in the help here:

Error handling with Python—Help | ArcGIS for Desktop