How to design a parametric gear in Python and 3D-print it
When you describe a gear in code, you can change a dimension by editing the number that controls it. We used CadQuery on a Small awx computer to make an involute spur gear. We then checked how it fitted with a partner and found zero overlap in the position we tested. Finally, we shared a link to the 3D view so our teammate could see the gear and download the STL for printing.
Why CAD as code
Suppose you need to change a gear from 20 teeth to 24. In a click-based CAD program, that means drawing it again. In code, the module, tooth count, width and bore are variables at the top of the file. Edit the value you need and run the file again to generate a new STL and STEP. The design itself is a text file you can put in Git, review and hand in.
CadQuery is a free Python library that uses OpenCASCADE, the same geometry kernel that FreeCAD uses. It exports your model as a standard STEP file (ISO 10303), so you can open it in SolidWorks, Fusion, Inventor or FreeCAD.
Step 1: a computer and CadQuery
Create a Small computer in awx (we called ours gear-cadquery). Open its Terminal tab and enter the commands below to install the libraries.
python3 -m venv ~/cad
~/cad/bin/pip install cadquery matplotlib
These commands installed CadQuery 2.8.0 for us. Since the libraries are in ~/cad, use ~/cad/bin/python to run the scripts. Next, use the commands below to download this post’s files into your home folder, or upload them through the Files tab. Work from your home folder for the steps that follow.
cd ~
for f in gear.py check.py pair.py viewer.html; do curl -sO https://awanbox.com/blog/files/parametric-gear/$f; done
The main script, gear.py, builds the gear. Keep check.py and pair.py in the same folder because both scripts use it. You will use viewer.html later to show the gear in a browser.
Step 2: the involute tooth
To picture why gear teeth are shaped as involutes, imagine unwinding a taut string from a circle. The end traces an involute, and we call that circle the base circle. When two involute teeth roll and slide against each other, they maintain a constant speed ratio even if the centre distance is slightly off. This is why almost every modern power-transmission gear uses them. In Python, the function for the curve takes two lines, plus its docstring.
def involute(rb, t):
"""A point on the involute of a circle of radius rb, at roll angle t."""
return rb * (math.cos(t) + t * math.sin(t)), rb * (math.sin(t) - t * math.cos(t))
With the pressure angle α set to 20°, use the module m and the number of teeth z to calculate the standard diameters below.
pitch diameter d = m z
base diameter db = d cos α
tip diameter da = d + 2m
root diameter df = d − 2.5m
gear_profile() builds the outline tooth by tooth. For each tooth, it draws the root arc, follows one involute flank up to the tip circle and comes back down along the mirrored flank. The involute ends at the base circle, so the outline continues straight down to the root. A real cutting tool leaves a rounded fillet there. For a printed gear, the straight line is fine.
Step 3: from outline to solid
MODULE = 2.0 # mm
TEETH = 20
WIDTH = 8.0 # mm
BORE = 8.0 # mm
PRESSURE_ANGLE = 20.0 # degrees
def spur_gear(m=MODULE, z=TEETH, width=WIDTH, bore=BORE, alpha=PRESSURE_ANGLE):
pts, d = gear_profile(m, z, alpha)
g = cq.Workplane("XY").polyline(pts).close().extrude(width)
g = g.faces(">Z").workplane().hole(bore)
return g, d
Run the script to build and export the gear.
~/cad/bin/python gear.py
module 2.0 mm, 20 teeth: pitch Ø 40.00, tip Ø 44.00, root Ø 35.00, base Ø 37.59 mm
bounding box 43.98 x 43.98 x 8.00 mm, volume 9.40 cm³
saved gear.stl and gear.step
Our run took three seconds. Use gear.stl (146 KB) in your 3D printer’s slicer and gear.step in any CAD program. The bounding box measures 43.98 mm because the outline approximates the ideal shape. Each flank uses 12 short straight segments, and the tip is a straight line between the flanks. That puts the solid a few hundredths of a millimetre inside the ideal shape, whose tip diameter is 44.00. Increase n in gear_profile() for a finer outline.
Step 4: check the tooth
Before you trust the model, check the tooth thickness at the pitch circle. It should be exactly half the circular pitch, which gives πm / 2 = 3.142 mm. We cut the solid with thin rings at three diameters to measure how the tooth width changes from base to tip.
~/cad/bin/python check.py
base circle Ø 37.59: tooth width 3.512 mm
pitch circle Ø 40.00: tooth width 3.140 mm
tip circle Ø 43.98: tooth width 1.402 mm
At the pitch circle, we measured 3.140 mm against the expected 3.142. The small difference comes from the straight segments. The measurements also show the tooth narrowing from base to tip, as an involute tooth should. The script check.py saves a close-up of one tooth as zoom.png.

Step 5: does it mesh?
Once you have checked the tooth on its own, check how it fits with a partner. The script pair.py makes a 40-tooth wheel and places it at the correct centre distance, a = m(z₁ + z₂) / 2 = 60 mm. It turns the wheel by half a tooth so a gap faces the pinion’s tooth, then asks CadQuery to measure the volume where the two solids overlap.
~/cad/bin/python pair.py
centre distance 60.0 mm, gear ratio 2.0 : 1, overlap 0.0000 mm³
wheel turned 0.0°: overlap 156.61 mm³
wheel turned 2.2°: overlap 84.72 mm³
saved gear_pair.png and gear_pair.step
At the correct centre distance, zero overlap tells you the teeth fit without interference in this position. The two lines below it show what happens when the wheel is turned to the wrong position. In both cases, the check catches a clash. A working mesh requires zero overlap, but gears set too far apart also pass this check, and a clash can still occur elsewhere in the mesh. To check the full mesh, turn the pinion through one tooth in small steps, turn the wheel by half as much at each step (the 2 : 1 ratio), and measure the overlap each time. You can use the same method in any assembly by intersecting the parts and measuring the resulting volume.
Step 6: share a 3D view with a link
Your teammate can view and download the gear in a browser without installing Python. The page viewer.html uses three.js to load the STL into a 3D view. Your teammate can drag to turn the gear, scroll to zoom and use the two download links to get the files. Put the page next to those files and start a small web server with the commands below.
cd ~
mkdir -p ~/gearsite && cp viewer.html ~/gearsite/index.html && cp gear.stl gear.step ~/gearsite/
cd ~/gearsite && python3 -m http.server 8000
The server keeps the terminal busy while it runs. Leave it running so your teammate can view the page, then press Ctrl+C when you are ready to stop it.
On the computer’s Links tab, enter port 8000, choose how long the link should work (one hour to seven days), and select Create link. Send the address to your teammate so they can open the view, including on their phone.

Printing it
- Module 1.5 or more is a safe choice for an ordinary FDM printer with a 0.4 mm nozzle. Below about module 1, the teeth start to lose their shape, although this depends on the printer and the material.
- Print it lying flat so that each layer contains the full outline of every tooth. That way, the load on a tooth does not try to split the layers apart.
- A little backlash helps printed gears run smoothly. You can give them that clearance by moving them about 0.1 to 0.2 mm further apart than the ideal centre distance.
- A tight bore is normal because printed holes come out slightly small. To get the size you need, drill the hole to size or set
BORE0.2 mm larger.
Common questions
What is the module of a gear?
Divide the pitch diameter by the number of teeth to get the module in millimetres. It sets the tooth size. For two gears to mesh, they must have the same module and pressure angle.
Can I open the STEP file in SolidWorks or Fusion?
Yes. CadQuery writes standard STEP files, so you can open them in SolidWorks, Fusion, Inventor, Onshape and FreeCAD.
Why does the tooth go straight down below the base circle?
The involute exists only outside the base circle. A gear-cutting tool leaves a rounded fillet below it. Here, the code uses a straight line to reach the root, which is fine for printed and prototype gears.
Is the link safe to share?
Anyone with the link can open the page until it expires. Share it only with your team, choose a short lifetime and revoke it on the Links tab when you are done.
Keep reading
- How to do a finite element analysis for free with CalculiX
- How to find natural frequency from vibration data in Python
- How to send your lecturer a link to your Flask assignment
Getting started
To try the walk-through yourself, sign in with your Google account and ask to join. Once you are approved, create a Small computer and follow the steps above.
Awi, signing off.