Python Stairs
Contents
- 1 Introduction
- 2 Setting up the Python component
- 3 Add description to inputs and outputs
- 4 Initialize implicit variables
- 5 Generate the 2D staircase outline points
- 6 Generate the 2D staircase outline curves
- 7 Extrude the risers and treads
- 8 Output the result
- 9 Error Messages
- 10 Final Python script
- 11 Publish/re-use the script
Introduction
Expected Time: 90 min
During this tutorial, we will create a staircase using Python programming in Grasshopper step by step. It is assumed that you already have a basic understanding of how the Grasshopper Python component works, and some basic knowledge about Python programming. If not, it is recommended to first take a look at How to use GhPython? and Python Algorithm.
To create the staircase we will make use of the Rhino.geometry library. If you are unfamiliar with this library, take a look at the RhinoCommon API documentation. When we are using functions that start with "rg." you can be sure the RhinoCommon API was used to find out how the function works.
Setting up the Python component
Add a GhPython component to the canvas.
- Drag a GhPython component to the canvas
Change and add the necessary inputs for the staircase script. Make sure you follow the spelling of the names exactly, according to the right image
- Add some inputs and outputs to the canvas
- Rename the inputs and outputs
- Connect Number Sliders to the inputs
Inputs: height, length, steps, landings, landing_length, nose_depth, width, riser_thickness, tread_thickness
Outputs: risers, treads, risers_outline, treads_outline
Change the Type hint of the inputs to integers and floats. Check the right image to find the right type.
- Set the correct data types
Double-click on the GhPython icon to open the Python Script Editor.
- Open the Python Script Editor
Add description to inputs and outputs
- In line 1 to 20, set a correct description for the scripts purpose, the inputs and outputs. It can be helpful to also note the data type behind the description of the inputs and outputs.
- Import the Rhino.geometry library and the math library.
Initialize implicit variables
Before we start creating our algorithm, we need to initialize several variables that will contain important data during the execution of the script. These variables will be lists that contain the outlines, endpoints and final solids of the risers and treads. For the naming of the variables we will follow the PEP8 style guide and use standard characters, with words separated by an underscore.
- Initialize the required variables according to the following image:
Next, we will need to calculate some implicit variables. In line 42 we will calculate the horizontal length that is available for the staircase steps, without the length of all the landings. In line 45 we need to calculate the depth of an individual step tread. Similarly, in line 48 we calculate the riser height of one step. Furthermore, we will need to know at which step interval, a normal step should be replaced by a landing. Therefore, we use the formula in line 51.
- Calculate the implicit variables needed for the staircase according to the following image:
If you want to make sure you used the correct formula, it could by wise to print the values of the implicit variables you used calculated.
- Print the variables to check if they are correct
- Click on Test
- Remove the print lines
Next, we need to create some indicators for thickness and width for the script in a later stage. Using the Rhino.Geometry library, in line 54 to 55 we create two lines that indicate the direction of which the risers and treads should be extruded. For the width of the staircase, we create a 3D vector in line 54. The fact we are using different types of indicators, is caused by the functions we will use later, which need different inputs.
- Create two lines that indicate the thickness direction for risers and treads
- Create a 3d vector direction indicator for the width
Note that we are using the Grasshopper Python node inputs to create the internal Python objects.
Generate the 2D staircase outline points
In this chapter, we will use the previously initialized variables, to create a 2D outline for the staircase.
Using a for-loop, we iterate over the amount of steps. In line 63, based on the step count, we check how many landings should be passed the staircase. To accomplish this, we use a floor rounding from the math library. Next, in line 66, we check if the amount of passed landings is greater than the amount that are allowed. If that is the case, the landings_passed variable is set to the amount of landings (line 67). Thus, no extra landing is added to the step. Finally, we need to check how much length should be added to the step start location, to match the locations of the landing (line 70).
- Continue the script according the following image:
If you print the landing_add variable, you will see that for each step Python calculates how far the step should be moved horizontally, to allow the landings to be placed in between.
Next, we use some more complex function to create 3D points that indicate the start/end for the risers and treads. The Point3D function creates a points based on an x, y and z value. Using the previously calculated variables, we can tell exactly what these values should be. Note, that in line 76, we also substract the nose depth. This will make sure the start point of the treads is moved a bit in the negative y direction. Furthermore, you may notice that line 77 seems to be wrong. The tread_end should not be the same point as the riser_begin. This is a problem that we will solve later.
- Create the start and end points of the risers and treads in line 79 to 83
Finally, we append the created points (line 80 to 83) to the corresponding lists, we created at the start of the script.
- Append the points to their corresponding lists
As mentioned before, the tread endpoints are not correct yet. However, if we remove the first point, and add the top right corner of the staircase to the tread endpoints, we are okay.
- Remove the first endpoint in line 86
- Add the top right corner point of the staircase to the tread endpoints in line 89
Make sure you do these steps outside the for-loop.
If we temporarily output the points to the outline outputs, you can see in the Rhino viewport that the points for the risers and treads are now visible. Make sure you remove the statements after checking.
Generate the 2D staircase outline curves
Based on the points we generated in the previous chapter, we will now convert these points into an outline.
Using a for-loop, we iterate over the steps count in line 92. Based on the start- and endpoints of the risers and treads, we generate a line between them (line 93 and 94). Considering some functions we need in a later stage, we convert the lines to curves in line 97 an 98. Finally, we append the curves to the corresponding lists.
- Generate the lines between the start and end points
- Convert the lines to curves
- Append the curves to the corresponding lists
By temporarily outputting the lines, we can check if they are correct in the Rhino viewport. Remove the statements afterwards.
Extrude the risers and treads
In this chapter we will extrude the outlines to a real staircase.
Using a zip-loop in line 104, we can iterate over both the riser and tread curves. First, we extrude the outlines using the width direction in line 106 and 107.
- Zip loop over the riser and tread outlines
- Extrude the riser and tread outlines using the width direction
To further extrude the surfaces in perpendicular direction, to give the risers and treads thickness, we will need to convert them to brep faces in line 110 and 111. After that, we extrude the brep faces and capping them using the boolean True statement at the end of the function (line 114 and 115). Finally the solid risers and treads are appended to their corresponding lists (line 118 and 119).
- Convert the surfaces to brep faces
- Extrude the brep faces to give the risers/treads thickness
- Append the solid risers and treads to their corresponding lists
Since we do not need the final tread, we can remove it from the tread outline list (line 122) and the tread solid list (line 123).
Output the result
As final step, we output the result. Using the output names we created for the Grasshopper Python component, we can display the result in Rhino.
- Output the results in line 126 to 129
Error Messages
If you would like to use this script in the future, it may be wise to add some error messages to your script. At the start, you can code some conditional statements that check if the Number Sliders value inputs are correct. If not, the Out parameter will send you a message.
Final Python script
Publish/re-use the script
You can add the Python Script to your Grasshopper toolbar by converting it to a cluster. Take a look at Clusters and Hops to find out how to do this.