Standard Lego rack gears only come in one size: 1 stud wide by 4 studs long.
This thing allows you to generate Lego rack gears of any length and width.
LEGO tolerances are very strict - while the teeth will most likely mesh perfectly with LEGO gears, you might have a harder time getting the rack to grip LEGO studs properly if your first layer has too much spreading. A lot of slicers will alter the first layer so that it's thicker than subsequent layers to increase the adhesion between the first layer and the print bed. If you can avoid altering the first layer and still get a good print your printed part will be more likely to work well with LEGO.
I've had the most success with ABS printed on a heated bed with no alterations to the first layer. Even when the gear is 12+ studs long, when printed on a heated bed using ABS it will usually grip studs perfectly.
Adjust the values of width_in_studs and length_in_studs as needed and print with the teeth up.
Even stud lengths work best. You can use odd lengths, but the script won't add partial teeth so you'll be left with a small toothless section on the end of a part.