1

I don't know python so I often use ModelBuilder and export to python script. In this case, I have a model (that in itself is made of python scripts that have been exported from models) that works fine where if the precondition (ISG Template and VC Template) is false, it does not execute that branch and if true, it does.

When I export it to python script, the boolean does not exist and even if no parameters are input, the full script runs.

model that runs correctly

input parameters

Below is the python script that runs and produces an output regardless of whether the boolean is checked or not. How can I get the boolean precondition to work in Python? I'm using ArcGIS Desktop 10.6. All the parameters are optional in both the model and the python script.

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Parent.py
# Created on: 2019-07-14 16:21:30.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: Parent <ISG_Template> <Output_ISG_DWG_File> <VC_Template> <Output_VC_DWG_File> 
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy
import os


# Load required toolboxes
arcpy.ImportToolbox("//cadd/users/steve/CADGIS/Version2019/GIStoCAD.tbx")

# Script arguments
ISG_Template = arcpy.GetParameterAsText(0)

Output_ISG_DWG_File = arcpy.GetParameterAsText(1)

VC_Template = arcpy.GetParameterAsText(2)

Output_VC_DWG_File = arcpy.GetParameterAsText(3)

# Local variables:

# Process: VC
arcpy.VC_GIStoCAD(Output_VC_DWG_File)

# Process: ISG
arcpy.ISG_GIStoCAD(Output_ISG_DWG_File)

Furthermore, below is the script with my modifications. I still doesn't work. The script runs regardless of whether boolean is checked but does not produce an output. It runs in about a second so its really not doing anything. It should take a minute.

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Parent.py
# Created on: 2019-07-14 18:31:23.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: Parent <ISG_Template> <Output_ISG_DWG_File> <VC_Template> <Output_VC_DWG_File> 
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Load required toolboxes
arcpy.ImportToolbox("//cadd/users/steve/CADGIS/Version2019/GIStoCAD.tbx")

# Script arguments
ISG_Template = arcpy.GetParameterAsText(0)

if ISG_Template == True:
    arcpy.ISG_GIStoCAD(Output_ISG_DWG_File)

Output_ISG_DWG_File = arcpy.GetParameterAsText(1)

VC_Template = arcpy.GetParameterAsText(2)

if VC_Template == True:
    arcpy.VC_GIStoCAD(Output_VC_DWG_File)

Output_VC_DWG_File = arcpy.GetParameterAsText(3)
  • 1
    There is no guarantee that Python code output from ModelBuilder will always run. The way I recommend to learn to use ArcPy in Python is by using Copy As Python Snippet from the Geoprocessing | Results window after running each tool from its tool dialog. – PolyGeo Jul 14 at 23:14
  • 2
    The ISG_Template and VC_Template values are not Boolean values anymore, since you brought those parameters in as text using the arcpy.GetParameterAsText() function. So your conditions should most likely need to be If ISG_Template == 'True': and If VC_Template == 'True':. Add a print method after assigning the text to the variable to determine if it is storing 'True', '1', or some other text value. – Richard Fairhurst Jul 14 at 23:19
  • 2
    Also you need to move the Output_ISG_DWG_File = arcpy.GetParameterAsText(1) above the if ISG_Template == 'True': and move the Output_VC_DWG_File = arcpy.GetParameterAsText(3) above the if VC_Template == 'True': – Richard Fairhurst Jul 14 at 23:22
  • 1
    PolyGeo and Richard, thank you! Between your responses, I've got it working! Poly, I did not know of the copy as python snippet option. When I did that, I saw the output was "true", not True as I had it in the script. After changing this and rearranging the parameters as Richard suggested, the script is now working. Thanks again! – Steve Jul 15 at 0:12
  • @RichardFairhurst I recommend writing up your comments in the form of an answer so OP can accept it. – Fezter Jul 15 at 1:31
2

The ISG_Template and VC_Template values are not Boolean values anymore, since you brought those parameters in as text using the arcpy.GetParameterAsText() function. So your conditions should most likely need to be If ISG_Template == 'true': and If VC_Template == 'true':. Add a print method after assigning the text to the variable to determine if it is storing 'true', 'True', '1', or some other text value. It looks like the values are 'true' based on your comment about using the Python snippet option, so I have made that the value in the script below.

Also you need to move the Output_ISG_DWG_File = arcpy.GetParameterAsText(1) above the if ISG_Template == 'true': and move the Output_VC_DWG_File = arcpy.GetParameterAsText(3) above the if VC_Template == 'true': Variables have to be assigned a value before they can be used in a method or function in Python.

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Parent.py
# Created on: 2019-07-14 18:31:23.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: Parent <ISG_Template> <Output_ISG_DWG_File> <VC_Template> <Output_VC_DWG_File> 
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Load required toolboxes
arcpy.ImportToolbox("//cadd/users/steve/CADGIS/Version2019/GIStoCAD.tbx")

# Script arguments
ISG_Template = arcpy.GetParameterAsText(0)

Output_ISG_DWG_File = arcpy.GetParameterAsText(1)

if ISG_Template == 'true':
    arcpy.ISG_GIStoCAD(Output_ISG_DWG_File)

VC_Template = arcpy.GetParameterAsText(2)

Output_VC_DWG_File = arcpy.GetParameterAsText(3)

if VC_Template == 'true':
    arcpy.VC_GIStoCAD(Output_VC_DWG_File)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.