Houdini & Procedural Workflows / Houdini / Python
Day 7 - Conditions in Houdini with Python | Houdini 10 Day Introduction to Python
Written guide

Playing loads YouTube content. YouTube processes data after activation.
Watch on YouTubeIf the player is blocked or unavailable, use Watch on YouTube.
Make a Houdini Python tool choose what to do from the values it receives. Day 7 introduces comparisons, membership tests and branching, then uses them to distinguish a useful node selection from an empty one. Its most valuable debugging example is a misplaced if that makes a negative number print a message claiming it is zero.
Use a Python shelf tool and the Python Shell to follow the output. Apply edits to the tool before running it again. The examples below normalize the spoken code into Python 3; they are not a transcription of every character visible in the recording. The exact recorded Houdini and embedded Python versions are unverified.
Make an action conditional on a comparison
At 00:00, the simplest demonstration runs an indented print statement under if True. Changing the condition to False produces no new output. A false condition is not an error: Python simply skips that body.
At 02:01, the lesson introduces x = 5 and y = 10 and compares them with other numbers. Use the following as a reading guide to the operators demonstrated:
| Test | Meaning | Result for those starting values |
|---|---|---|
x > 0 |
Greater than | True |
y < 0 |
Less than | False |
y <= 10 |
Less than or equal to | True |
x >= 5 |
Greater than or equal to | True |
x == 5 |
Equal in value | True |
y != 5 |
Not equal in value | True |
At 04:00, the instructor emphasizes the difference between assignment and comparison. x = 5 gives the variable a value. x == 5 asks whether its value equals five. Keep the colon at the end of the if line and indent the action underneath it.
x = 5
y = 10
if x > 0:
print("X is positive")
if y < 0:
print("Y is negative")
Only the first message prints. The two conditions are separate tests, so a successful first test does not prevent Python from evaluating the second.
Combine tests and inspect membership
At 05:01, and combines two comparisons. For the lesson’s Boolean comparisons, both must be true for the body to run. With or, introduced at 06:01, at least one true comparison is sufficient. Changing one sign and then both signs reveals the difference.
For example, x > 0 and y > 0 describes two positive numbers. x > 0 or y > 0 also accepts a pair with only one positive number. Read the entire condition before interpreting its message; a message mentioning “X or Y” does not tell you which value passed.
At 07:01, membership replaces numeric ordering:
numbers = [1, 2, 3, 4, 5]
if 3 in numbers:
print("3 is in the list")
if 6 not in numbers:
print("6 is not in the list")
text = "this test check"
if "test" in text:
print("test is in the string")
All three messages print. For a list, the example looks for a matching element. For a string, it looks for a substring, not a separate word or a list entry. Python’s membership reference clarifies that distinction. The lesson briefly calls the string a list while speaking; the actual data roles should remain separate.
Use one chain for mutually exclusive outcomes
At 09:02, the lesson builds an if/elif/else chain that classifies an ordinary number by sign:
x = 0
if x > 0:
print("X is positive")
elif x < 0:
print("X is negative")
else:
print("X is zero")
With zero, neither comparison succeeds, so the fallback prints. Change x to 1 and then −1 to check the other two paths. Python’s control-flow tutorial describes the same optional elif and else structure.
Within this chain, the first matching branch wins. At 11:00, adding a later elif x == 10 does not make that branch reachable when an earlier x > 0 already accepts ten. If a special case should replace the general positive message, its test must come before the broader positive test. If both messages are wanted, use a separate test after the completed sign-classification chain.
The zero message is appropriate for the lesson’s ordinary numeric examples. This small exercise does not validate arbitrary input types or handle every special numeric value.
Understand why the misplaced else prints the wrong message
At 12:00, the instructor changes the ten-specific branch into an independent if. This makes ten print both the positive and ten-specific messages. But placing the old else immediately after that new test changes which condition the fallback belongs to.
For −10, the earlier sign test prints “X is negative.” The separate x == 10 test is false, so its own else prints “X is zero.” Python is following the written structure; the message is wrong because the fallback is attached to the wrong decision.
At 13:00, the correction is to finish the sign-classification chain first, then place the independent ten test after it:
x = 10
if x > 0:
print("X is positive")
elif x < 0:
print("X is negative")
else:
print("X is zero")
if x == 10:
print("X is 10")
Ten now prints the two intended messages. Negative ten prints only the negative message. Zero prints only the zero message. This three-value check is more revealing than repeatedly testing the one value that originally seemed to work.
Give the Houdini selection an explicit empty case
At 14:02, create a geometry network containing several nulls and retrieve the selected nodes. The instructor converts the returned tuple to a list as a preference. Conversion is not required merely to count or print it.
At 16:01, the condition checks whether the selection contains any entries. A normalized version of the demonstrated tool is:
import hou
selected_nodes = list(hou.selectedNodes())
if len(selected_nodes) > 0:
print("Selected nodes:", selected_nodes)
else:
print("No nodes are selected")
Run it with nothing selected, with one null selected and with several selected. The first case should explain the absence of a selection; the others should print the selected node objects. This is a diagnostic message, not a node-editing operation.
The script retrieves the selection each time the shelf tool runs. It does not keep a live connection to future selection changes. Its condition establishes only that some nodes were selected: it does not establish a required node count, type or shared network for a more demanding tool.
The plain-Python examples were checked for syntax and their stated outputs during editorial preparation. The HOM example requires Houdini and was not executed there. The instructional result is a clear branching pattern that can later guard actual work: inspect the input, perform the appropriate action and give the empty case a meaningful explanation.