Skip to content


Level-up your OpenSCAD skills

Inspired by Joe Walnes’ tweet and post on OpenSCAD, here’s some tricks that i’ve used in OpenSCAD.

Assembly

For 3D printing, it is often helpful to design parts assembled but to be able to easily lay them flat for printing. Another Kwartzlabber Dave has been using this technique for quite a while. He uses if statements. Compared to the following technique, if statements are long winded and restrictive.

To use optional assembly, define a variable that takes on values {0, 1}.

// Assemble: 0 for printing, 1 for visualization
assemble=1;

Then, specify transforms for each, for example:

module placed_brick() {
    rotate((1-assemble)*[0,180,0])
    rotate(assemble*[90,-90,0]) {
        toybrick(2, 4);
    }
}

The assemble transformation will apply only when assemble=1. The (1-assemble) transformation will apply only when assemble=0.

The idea came from a concept presented in Andrew Ng’s Machine Learning course, applied to a different domain. In both cases, the goal was to replace a conditional function with one that is a standard (non-conditional) function. In machine learning, this technique is applied to the cost function for logistic regression. Here, we can choose one of two transforms to apply by having the other one multiply to 0.

This technique will work for both translations and rotations.

Movement

A very similar trick can be used for movement. With y animatronic eye, it is helpful to see the lids open, closed, and sometimes in between. In this case, i want to move from 0 to 45 degrees. Also note how the tricks are combined so the transformation only applies when assemble is specified.

Instead of closed range [0, 1] it would also be possible to used degrees directly, if desired.

// Lid opening: range [0, 1]
lids_open=0.5;

...

module lid_upper() {
    ...
    rotate(assemble*[0,-lids_open*45,0])
    ...
}

This isn’t animation, but does let you easily see the parts in different poses. Of course, if you do want to animate you can use $t. For details, see the Instructable on animation in OpenSCAD.

Define what matters

In quickly building models, it is sometimes easy to loose track of what matters. Sometimes you just add parts based on what is already drawn. Define what matters and work from there.

A good example occurred when building my eye. In this case, the distance from the edge to the screw hole is critical as it must align with another model. This wasn’t obvious in my initial drawing, since i wasn’t yet even sure how i would mount it or what i would connect the parts to. As the model progressed, the interfaces between the parts became critical. (Starting to sound a lot like software…) So i refactored the calculations to make the distance from the plate edge to the mounting hole a parameter rather than have it fall out of calculations (like the hole being half way from the center of the alignment block to the extent of the bracket — neither of these other dimensions are important).

Summary

Here, you’ve learned how to optionally assemble the model, create a bit of movement, and define the model in terms of what’s important. Hope this helps you in your own OpenSCAD modeling!

Posted in Make, Robotics.

Tagged with , , , , , , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

You must be logged in to post a comment.

«