Custom Printed Knobs In Just A Few Lines Of Code

While not everyone is necessarily onboard for the CAD-via-code principle behind OpenSCAD, there’s no denying the software lends itself particularly well to parametric designs. Using a few choice variables, it’s possible to make a model in OpenSCAD that can be easily tweaked by other users — even if they have zero prior experience with CAD.

Take for example this parametric-knob-maker written by [aminGhafoory]. The code clocks in at less than 100 lines, but if you’re looking to spin up your own version, all you really need to pay attention to are the clearly labeled variables up at the top. Just plug in your desired diameter and height, fiddle around a bit with the values that get fed into the grip generating function, and hit F7 to export it to an STL ready for printing.

Now admittedly, all the knobs generated with this code will look more or less the same. But that’s the beauty of open source, should you want to print out some wild looking knobs, you can at least use this code as a basis to build on. With the core functionality in place, you just need to concern yourself with writing a new function to generate a grip texture more to your liking.

Of course, if you want to make your OpenSCAD designs even easier for others to modify, you’ll want to look into its impressive customizer capability which replaces manually edited variables with friendly sliders and text input boxes. Projects like the Ultimate Box Maker we looked at back in 2018 are an excellent example of how powerful OpenSCAD can be if you give your design the proper forethought.

27 thoughts on “Custom Printed Knobs In Just A Few Lines Of Code

    1. OpenSCAD tries too hard NOT to be a programming language.
      It’s makers want it to be merely a descriptive language.

      As your designs get more parametric though, that becomes a problem.

      I’m not sure if it’s ready for prime time or not but I think the future might be in JSCAD.

      1. I think we can do a lot better than Javascript as a base for a CAD language.

        But I agree with your sentiment. Once things get complicated in OpenSCAD you’ve got to pay more attention to code structure and best practices or you’ll end up hard coded into a dark corner.

        Now if you’ll excuse me, I’m going to refactor some of my OpenSCAD code in the hopes that Systemd-oomd will stop killing X when I try to render high resolution version of my model.

        1. My personal experience is this:
          For all X it is true that
          “Once things get complicated in X you’ve got to pay more attention to code structure and best practices or you’ll end up hard coded into a dark corner.”

          1. When best practices are “that thing you want to do… we don’t do think you should want to do that, once you drop your goals you will be happy” it’s time to find a new language.

        2. I used to think OpenScad was a poor language (and dabbled with JS and python variants) but I have changed as a result of being more familiar with functional languages. It is not a perfect functional language but it is pretty good and I now much prefer it to JS etc.

          I would also like to to the opportunity to plug the BOSL2 library (look it up in the docs) , which I hope continues to thrive and improve. This now an essential part of any attempt to knock up a quick 3d print.

          1. As I learn more about functional programming I find that things I want to do are not impossible with it. But they are really awkward, difficult to re-use and force much more error prone styles.

            For example storing dimensions in position-based arrays (vectors) as opposed to named parameters, ex) bolt.shaft_diameter vs bolt[3]. Sure, one can wrap that in a function, function shaft_diameter(v) = v[3]. But those all end up in the only scope, the global one. So if you don’t want it to collide with your methods for some other kind of part that include a shaft you have to add to that name. bolt_shaft_diameter(v). But then you think, bolts are common, shaft diameter is a common aspect of a bolt to require. What if the user wants to use both my library and someone else’s? So everything looks like “function my_library_bolt_shaft_diameter(v)”. Yuck!!! And that’s just one parameter, you can end up with a function like this for every dimension of every type of part your library supports, all in the global scope!

            It doesn’t take many long names like that and you have lines of code that require left/right scrolling to read. Not ok!

            An OO approach would end up with something like “mybolt.shaft_diameter”. So much nicer!

            Another thing I would like to do is have a customizer that re-draws itself based on selections. I don’t think JSCAD directly supports that but mix in some html and I bet it could be accomplished.

            For example, there could be a dropdown for power button type with options of toggle, snap in, etc…
            Choose the type of switch and the next drop down, which previously was either hidden or disabled gets populated with a list of model numbers filtered by type. Pick a model number from the list and the correct size/shape hole is drawn on the front panel, maybe items are moved around to accommodate. But maybe the user has a switch in their junkbox and it’s model isn’t in the list. One option is “custom”. Choose this and now new controls appear based on the selected switch type. If it’s “toggle switch” it’s just a place to set the diameter. If it’s “snap in” you get hole height, hole width, recessed area height, recessed area width, clip thickness. Comment labels explain what each of these is and how to measure it. The user then takes a pair of calipers to their switch and enters the information per instructions.

            These are just things off the top of my head that OpenSCAD and functional programming makes hard or messy. Try designing with this sort of mindset using OpenSCAD and you will just run into roadblocks all along the way.

          2. Yes, +1 using OpenSCAD’s functional programming features.

            @Twisty Plastic
            Yeah, some form of name spacing support would help (or better yet, classes and names spaces). Has OpenSCAD++ development been started yet. :)

            A couple of things I’ve found that help with scoping:
            1) You can nest modules in modules, which while you can’t directly call the nested module from a higher level scope, you can reduce the amount of global scope pollution. (Kind of an approximation to private methods.)

            2) With “use ” statements, they import the symbols in the file context/scope, rather than the global scope. This is not as good a proper namespaces/classes, but does allow for some complexity management. I.e. if you nest your “use ” well, you can (mostly) avoid library symbol naming clashes.

        3. I have never been a Javascript fan. And so, as Javascript has slowly developed from a toy for making content-poor personal webpages flashy into a real and serious programming languages I have not kept up.

          And that’s why I’m not already using JSCAD. I intend to seriously buckle down in learning Javascript right after I finish my current efforts at studying the “Pro Git” book. Or maybe after Python. Kivy looks interesting. There just aren’t enough hours in a lifetime!

          But, yah. Javascript was never my first choice in languages either. But as far as I have found JSCAD seems to be the only “real” language out there for this purpose. Besides that there are options that generate OpenSCAD code from Python, but that two-step process sounds kind of hoaky to me. Then there is something based on Angelscript. But I don’t remember ever even hearing of Angelscript until I went looking for an alternative to OpenSCAD and I don’t see anyone other than the author talking about it so that kind of sounds like a dead end. And besides, Javascript can run in a browser. That means I can develop on an Android device. Yes, I know of ScorchCAD. It’s missing features including the hull() method and development seems to have stopped years ago.

          1. If you don’t mind another layer in your compilation process Typescript (javascript with types) is so much better and “compiles” down to javascript. It fixes a lot of the horrible things you can do with javascript

  1. I’m not a fan of OpenSCAD personally, but I respect it wholeheartedly. I also have respect for the people who put the time and effort into making these sorts of “plug numbers in” functions. Hours of work a day was saved thanks to a few lines of code just like this knob example.

  2. I haven’t tried OpenSCAD, but I’m thinking that it shouldn’t cause my brain the same “what axis am I placing this thing on” problem that it has with drawing based CAD programs. Or I just need to practice with other CAD software.

    1. Fusion 360, freecad, tinkercad and their ilk hurt my brain. They take ages to get started to setup , and become increasingly finicky tedious and repetitive as you go, and worst of all make increasing demands on your fine motor and mouse skills. Often in OpenScad you can start to see patterns arising in the design – just write a module for the pattern and you have a concise description, in nice copiable and editable code.

      I admit the the axis thing and attachments is a problem in Openscad. Some times it is hard to know the exact location to move or rotate to. BOSL2 helps (a lot). I often find there is a “natural” axis that is not apparent on the first attempt at a design but when you find it positioning can be a lot easier.

  3. I’M not a fan of OpenScad but I have used it and found it very useful for parametric design. For me I find looking at the code of putting and often reaching for that expensive 3d package.

  4. I’m only “dangerous” with OpenScad, but find projects like this extremely useful. I keep a “library” of scripts like this, and the include the “use” directive in my own scripts. (Some might choose to use “include” , but they are not the same.) I use my own and other libraries to make consistent screw holes, nut recesses, gears, and now knobs… Thank You!

  5. I’ve said similar before: I’d love to see a library of drag-and-drop, parameterised components in a 3D printing design tool. A box sized X by Y, with a USB C socket on one side and an IEC 12 (or whatever) on the other… Each and every component has a UUID and an x-y-z offset – so designs become (almost) trivially adjustable/modifiable/remixable/buildable-upon. If only I had the expertise…

  6. Huge fan of openscad here! For more button generators with for example fancy finishings, search for ‘Knurled knobs openscad’ (knurled is a term for the pattern on a knob to promote grip)

  7. I love OpenSCAD. I needed a couple of knobs for a project I was working on and created a simple parametric model in about 20 minutes. I used threaded brass inserts for the set screw (you just melt them into a hole), though, so I didn’t need to mess with threads in the model.

      1. @Neuron Plectrum said: “Maybe this: https://github.com/chrisspen/gears

        Thanx for the link Neuron, I’ll give it a try. BTW your “Neuron Plectrum” moniker spawns (in my brain) visions of a 60s rock group consisting of disembodied floating brains playing to a gaggle of screaming young (soon-to-be-digested) nubiles. The brains in this vision are straight out of “Fiend Without a Face”.[1] And no, I am not tripping. But then again, maybe I never stopped tripping…

        1. Fiend Without a Face [1958]

        https://www.imdb.com/title/tt0050393/

        https://en.wikipedia.org/wiki/Fiend_Without_a_Face

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.