{"id":20004,"date":"2025-12-20T12:25:51","date_gmt":"2025-12-20T17:25:51","guid":{"rendered":"https:\/\/nuxx.net\/blog\/?p=20004"},"modified":"2025-12-20T12:55:13","modified_gmt":"2025-12-20T17:55:13","slug":"openscad-is-kinda-neat","status":"publish","type":"post","link":"https:\/\/nuxx.net\/blog\/2025\/12\/20\/openscad-is-kinda-neat\/","title":{"rendered":"OpenSCAD Is Kinda Neat"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/nuxx.net\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-20-at-12.11.35-PM-scaled.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"502\" src=\"https:\/\/nuxx.net\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-20-at-12.11.35-PM-1024x502.png\" alt=\"\" class=\"wp-image-20005\" srcset=\"https:\/\/nuxx.net\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-20-at-12.11.35-PM-1024x502.png 1024w, https:\/\/nuxx.net\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-20-at-12.11.35-PM-300x147.png 300w, https:\/\/nuxx.net\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-20-at-12.11.35-PM-768x376.png 768w, https:\/\/nuxx.net\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-20-at-12.11.35-PM-1536x752.png 1536w, https:\/\/nuxx.net\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-20-at-12.11.35-PM-2048x1003.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Designing a simple battery holder in OpenSCAD.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Earlier this year I designed a very basic box\/organizer for <a href=\"https:\/\/en.wikipedia.org\/wiki\/AA_battery\">AA<\/a> and <a href=\"https:\/\/en.wikipedia.org\/wiki\/AAA_battery\">AAA<\/a> batteries in <a href=\"https:\/\/www.autodesk.com\/products\/fusion-360\/\">Autodesk Fusion<\/a>, making it parameterized so that by changing a few variables one could adjust the battery type\/size, rows\/columns, etc. This worked well, and after <a href=\"https:\/\/www.printables.com\/model\/1522509-simple-battery-holder-aa-and-aaa-w-parameterized-f\">uploading it to Printables earlier today<\/a> I realized that reimplementing it would probably be a good way to learn the basics of <a href=\"https:\/\/openscad.org\/\">OpenSCAD<\/a>.<\/p>\n\n\n\n<p>OpenSCAD is a rather different type of CAD tool, one in which you write code to generate objects. Because my battery holder is very simple (just a box with a pattern of cutouts) and uses input parameters, I figured it&#8217;d be a good intro to a new language \/ tool. And in the future might even be better than firing up Fusion for such simple designs.<\/p>\n\n\n\n<p>After going through part of <a href=\"https:\/\/en.wikibooks.org\/wiki\/OpenSCAD_Tutorial\">the tutorial<\/a> and an hour or so of poking, here&#8217;s the result: <a href=\"https:\/\/nuxx.net\/files\/3d_printing\/battery_holder_generator.scad\"><code>battery_holder_generator.scad<\/code><\/a><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"alignright size-medium\"><a href=\"https:\/\/nuxx.net\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-20-at-12.30.03-PM-scaled.png\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"166\" src=\"https:\/\/nuxx.net\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-20-at-12.30.03-PM-300x166.png\" alt=\"\" class=\"wp-image-20008\" srcset=\"https:\/\/nuxx.net\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-20-at-12.30.03-PM-300x166.png 300w, https:\/\/nuxx.net\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-20-at-12.30.03-PM-1024x568.png 1024w, https:\/\/nuxx.net\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-20-at-12.30.03-PM-768x426.png 768w, https:\/\/nuxx.net\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-20-at-12.30.03-PM-1536x852.png 1536w, https:\/\/nuxx.net\/blog\/wp-content\/uploads\/2025\/12\/Screenshot-2025-12-20-at-12.30.03-PM-2048x1136.png 2048w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption class=\"wp-element-caption\">Slicer showing the Fusion model on top and OpenSCAD on bottom.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>By changing just a few variables &#8212; <code>numRows<\/code> and <code>numColumns<\/code> and <code>batteryType<\/code> &#8212; one can render a customized battery holder which can then be plopped into a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Slicer_(3D_printing)\">slicer<\/a> and printed. No heavy\/expensive CAD software needed and the output is effectively the same.<\/p>\n\n\n\n<p>Without comments or informative output, this is the meat of the code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>AA = 15;\nAAA = 11;\nheightCompartment = 19;\nthicknessWall = 1;\nnumRows = 4;\nnumColumns = 10;\nbatteryType = AA;\n\nwidthBox = (numRows * batteryType) + ((numRows + 1) * thicknessWall);\nlengthBox = (numColumns * batteryType) + ((numColumns + 1) * thicknessWall);\ndepthBox = heightCompartment + thicknessWall;\n\ndifference() {\n    cube(&#91;lengthBox, widthBox, depthBox]);\n    for (c = &#91; 1 : numColumns ])\n        for (r = &#91; 1 : numRows ])\n            let (\n                startColumn = ((c * thicknessWall) + ((c - 1) * batteryType)),\n                startRow = ((r * thicknessWall) + ((r - 1) * batteryType))\n            )\n            {\n                translate(&#91;startColumn, startRow, thicknessWall])\n                cube(&#91;batteryType, batteryType, heightCompartment + 1]);\n            }\n};<\/code><\/pre>\n\n\n\n<p>Simply, it draws a box and cuts out the holes. (The first <code>cube()<\/code> draws the main box, then <code>difference()<\/code> subtracts the battery holes via the second <code>cube()<\/code> as their quantity and location (via <code>translate()<\/code>) is iterated.<\/p>\n\n\n\n<p>That&#8217;s it. Pretty neat, eh?<\/p>\n\n\n\n<p>(One part that confused me is how I needed to use <code><a href=\"https:\/\/en.wikibooks.org\/wiki\/OpenSCAD_User_Manual\/List_Comprehensions#let\">let()<\/a><\/code> to define <code>startColumn<\/code> and <code>startRow<\/code> inside the loop. I don&#8217;t understand this&#8230;)<\/p>\n\n\n\n<p>While this probably won&#8217;t be very helpful for more complicated designs, I can see this being super useful for bearing drifts, spacers, and other similar simple (yet incredibly useful in real life) geometric shapes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Earlier this year I designed a very basic box\/organizer for AA and AAA batteries in Autodesk Fusion, making it parameterized so that by changing a&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/nuxx.net\/blog\/2025\/12\/20\/openscad-is-kinda-neat\/\">Continue reading<span class=\"screen-reader-text\">OpenSCAD Is Kinda Neat<\/span><\/a><\/div>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,11],"tags":[],"class_list":["post-20004","post","type-post","status-publish","format-standard","hentry","category-computers","category-making-things","entry"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/posts\/20004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/comments?post=20004"}],"version-history":[{"count":6,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/posts\/20004\/revisions"}],"predecessor-version":[{"id":20015,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/posts\/20004\/revisions\/20015"}],"wp:attachment":[{"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/media?parent=20004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/categories?post=20004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/tags?post=20004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}