WS01
Reading a Script
Inputs are variables · a loop over a list · then read what the agent wrote · Grasshopper, Rhino 8
LengthOne session, in class
ToolRhino 8 · Grasshopper
BringA terrain surface or mesh
PaperYes — steps 01 and 06
Learning Objectives
  • Predicting what a process will do before setting it running
  • Seeing that a component's inputs are a function's parameters
  • Checking an instrument before it touches your own work
AssignmentA slope-band analysis of your own terrain, produced by a Grasshopper Python component the agent wrote from your spec, with the thresholds on sliders — and three spots you measured by hand to check it.
Predict
→
Loop
→
Choose
→
Spec
→
Agent
→
Check
You already read Grasshopper definitions. A Python component is one more component — inputs on the left, outputs on the right — except that what happens in the middle is written down where you can read it. That turns out to be the whole lesson: the things you wire into the left side are the variables the code uses, and nothing else about a script is mysterious once that lands.
Every code block below has a Copy button. Paste rather than retype — the point today is reading, not typing. The order matters: predict before you run. A prediction you got wrong shows you where your model of the machine is off, and running it first robs you of that.
01 An Input Is a Variable
In Grasshopper go to the Maths tab, Script panel, and drop a Python 3 Script component on the canvas. It arrives with inputs named x and y, and outputs named out and a.
Zoom in until the Insert ⊕ and Remove ⊖ controls appear on either side of the component. Then right-click the x parameter itself to rename it rise, and y to run. Wire a number slider into each.
rise run
Python 3 Script slope = rise / run * 100
out a

The two names on the left are the two names the code uses. That is the entire idea.

Open the editor and replace the script with this:
Python 3 Script component
# Grasshopper fills rise and run before this line runs.
# You never wrote "rise = something" — the slider did that.
slope = rise / run * 100

a = slope
Write this down before you wire a panel Set rise to 1 and run to 12 — a 1:12 ramp. What number comes out of a? Write the number you expect, then look.
It reads 8.333333333333332, not 8.33. One twelfth has no exact decimal, and the machine shows you all of it rather than the tidy number you had in mind. Keep that in your pocket — it comes back in Step 03 and it is not a curiosity.
  • dir, filter and str are reserved; the component warns you if you name a parameter one of them.
02 A Loop — One Component, Many Values
So far the component handles one pair of numbers. Now give it many. Right-click the rise input, find Parameter Access, and set it to List — which tells the component to hand your script the whole branch at once instead of one item at a time. Wire a panel of three numbers into it.
rise set to List access, run left as Item
# rise now arrives as a list of numbers, not one number.
# The loop does the same arithmetic to every one of them.

slopes = []

for r in rise:
    slopes.append(r / run * 100)

a = slopes
Predict again With rise = 1.0, 2.5, 0.4 and run = 12, how many items come out of a, and what is the second one?
Three items: 8.333333333333332, 20.833333333333336, 3.3333333333333335. Add a number to the panel and the output grows by one, with no edit to the code. That is what a loop buys you — the script stops caring how many there are.
  • slopes = [] makes an empty list; .append() puts one value on the end of it. The loop runs once per item in rise, and r is whichever item it is holding at that moment.
  • Set the access back to Item and the script breaks — you cannot loop over a single number. Try it, read the error, set it back.
03 Choose — and Who Chose the Number
Add a third input, limit, with a slider on it. Now the component sorts rather than just calculates.
three inputs: rise (List), run (Item), limit (Item)
slopes = []
steep = 0

for r in rise:
    slope = r / run * 100
    slopes.append(slope)
    if slope > limit:
        steep = steep + 1

print("steep:", steep, "of", len(rise))

a = slopes
Drag the limit slider and watch the count change. The code never changed. Everything that moved was on the left side of the component.
Now the part that matters Set limit to 8.33 — the ADA ramp limit, 1:12, the same number from the Ramp on a Slope tutorial. Feed it a rise of 1 against a run of 12: a ramp built exactly to code. The component calls it too steep. Why?
Because 1 ÷ 12 × 100 is 8.333333333333332, and that is larger than 8.33. The threshold you typed is not the threshold the code applies. A legal ramp fails your test. The judgment in this script was never in the if — it was in a number a person rounded, and the rounding changed the answer.
  • Fix it by comparing ratios rather than rounded percentages: if r / run > 1 / 12: — now a 1:12 ramp passes.
  • This is the shape of most wrong answers you will get from an agent. Not a crash. A number that is slightly not the one you meant.
04 Write the Spec
You have now read every kind of line a script contains. Time to ask for one you did not write. Open a copy of a project with a terrain surface or mesh, and write the spec first — in English, before you open the agent.
the spec — adapt the thresholds and layer names to your project
Read the surface on my Existing Grade layer. Color it by slope in
four bands: under 2%, 2-5%, 5-8.33%, over 8.33%. Bake the result as
a mesh on a new layer called Slope Analysis, add a legend, then
capture the top viewport and check that the steep zones read
correctly.

Build it as a Grasshopper definition with sliders for the
thresholds, and keep the code in a Python component I can read.
  • The bands above are one common set: minimum for drainage, the accessible-route limit, the ramp limit. Use the ones your project needs and be ready to say why.
  • Name layers exactly. “The surface on my Existing Grade layer” beats “my terrain.” Precise names are most of the specification.
  • Thresholds on sliders, not buried in the code. You just spent three steps learning why that matters: the number is the design decision, so it belongs where you can reach it.
05 Read What Came Back
Paste the agent's script into a Python component. It will be longer than anything above and will use parts of Rhino you have not met. Read it anyway, hunting for four specific things rather than trying to follow every line.
  • Where do the thresholds live? They should be inputs with sliders. If they are typed into the middle of the code, say so and ask again.
  • Where is the loop? Find the line that walks the faces of the mesh. It is the same for you wrote in Step 02, over a longer list.
  • Where is the if? That is the banding. Check the comparison operators at the band edges — > against >= is the Step 03 problem all over again.
  • Are slope and limit in the same units? If the script computes a ratio (0.0833) and compares it to a percentage (8.33), every face reads as flat — a 30% slope included — and nothing errors. This is the single most likely wrong answer you will get here.
Note what you had to correct, and whether it was wrong because your spec was vague or because the agent guessed. That distinction is the one worth keeping: the first kind teaches you to write, the second teaches you where to look.
06 Check Before You Bake
Until now the definition has changed nothing. Baking is the step that writes into your document, and it is the one that cannot be undone by deleting a component. So check first, on paper.
Check Pick three spots, measure the slope yourself between two contour points, and compare with the color the agent gave them.
If all three agree, bake it onto the Slope Analysis layer and keep going. If one disagrees, you have found something — work out which of the four questions in Step 05 it belongs to before you change anything.
  • Three hand measurements take four minutes and are the only part of this session that proves anything.
  • A result that looks plausible across the whole site and is wrong at one corner is the normal failure. Plausibility is not a check.
Feeds forwardThe spec you wrote in Step 04 is the first draft of the writing you will do all semester — the site BUILDSPEC is the same move at the scale of a whole website. The four questions in Step 05 come back in every desk crit, and a slope tool with its thresholds on sliders is a plausible Part II project in Track C.