1

I have a tool I'm trying to create in model builder. I have run into a problem as I have basically no experience with python. I'm trying to basically use the calculate value tool and an if then statement to determine if a model parameter (type String) has a value inputted to it by the user, if it does then go one direction in model builder and if not go another. Something to the effect of the picture below: enter image description here

The Second picture is of the model with the properties of the string and the tool (in the lower right)

enter image description here

1

Edit: I checked the help for the calculate Value tool. It says: "Variables created in ModelBuilder can be used by this tool, but variables desired for use in the expression parameter cannot be connected to the Calculate Value tool. To use them in the expression, enclose the variable name in percent signs (%). For example, if you want to divide a variable named 'Input' by 100, your expression would be %Input%/100." So, ModelBuilder parameter names have to be surrounded by percent signs to be passed into the Calculate Value tool (It has to be %String%, not String). I have changed the code to show the correct syntax.

The String parameter is never Null, it is initialized as '', so the condition has been change to only return False when values of String are greater than ''. It returns true if String == ''.

You have several problems. In Python you must set up a def function to use a code_block. I have flipped your test logic so that it now tests for actual string values and not Null values. ModelBuilder may return '' for what you consider to be a Null String parameter value. So you may need to change the test to be just values greater than '', not values greater than or equal to ''. None should used to test for real Null values if you are dealing with field values from a table or a parameter from a script tool interface. You are also missing a colon at the end of your if statement as kttii mentioned. Also, this is setting a Boolean parameter based on the Data Type value you have chosen, not a text string, so your return values are wrong and you need to change them to return real Boolean values.

So the expression must change to:

nullTest(%String%)

So the code_block code must change to:

def nullTest(String):
    print("The value of String is {0}".format(String))
    if String > '':
        return False
    else:
        return True
  • That worked beautifully with one small issue. None does not seem to equal no value. With no input, it returns it as false still. It states: Executing (Calculate Value (2)): CalculateValue nullTest(String) "def nullTest(String):\n if String == None:\n return True\n else:\n return False" Boolean Start Time: Thu Jun 23 12:21:37 2016 Value = 0 – something Jun 23 '16 at 17:24
  • Well then it is a blank string, since "Null" is not Null it is the word Null as a string. So I have changed it to if String == '': – Richard Fairhurst Jun 23 '16 at 17:56
  • I actually have flipped the logic to test for values that are not Null (i.e., they are a blank string or a real string value). If you get a return value of 1 when you consider the String parameter to be Null, it is actually a blank string. – Richard Fairhurst Jun 23 '16 at 18:05
  • I meant to say "If you get a return value of 0 with the new code when you consider the String parameter to be Null, it is actually a blank string." – Richard Fairhurst Jun 23 '16 at 18:12
  • Thanks for all the great help again! unfortunately the same error occurs with: if String == '' as well as 0 or various versions of NULL that I've tried – something Jun 23 '16 at 18:14
0

Looks like you are missing a colon at the end of the if statement.

See this page for more info: http://pro.arcgis.com/en/pro-app/tool-reference/modelbuilder-toolbox/calculate-value.htm

  • Thanks that helped part of the problem! However when I run the model builder it has an error stating: ERROR 000539: Runtime error SyntaxError: 'return' outside function (<string>, line 2) Failed to execute (Calculate Value). – something Jun 23 '16 at 16:48
  • I don't have arcgis but I think that link will help you. The error you are getting is because you did not enclose the if statement in a defined function. There are several examples in that link of how to formulate the code as a function in the Code block section. – kttii Jun 23 '16 at 16:55
0

If you don't want to use a code block, you can go for

'False' if !String! is None else 'true'

  • I don't think you can do that with Calculate Value. – something Jun 23 '16 at 17:44
  • In any event, a so called Null value in ModelBuilder does not match a Python value of None. So even if the syntax works, the test doesn't work. I think ModelBuilder is actually returning ''. In any case I think it may be better to flip the logic so that the test is for any real text values (return False), else (return True). Also, this is not a field calculation and String is a parameter, not a field. – Richard Fairhurst Jun 23 '16 at 18:17
  • @Richard Fairhurst I read your comments and I learn. I'll leave my answer up as food for thought and so your comment may profit others. I had not tested my answer in ModelBuilder. – FredCF Jun 23 '16 at 18:30

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.