3
$\begingroup$

Given a .SCAD file which contains some modules, how can I execute one of those modules from the command line?

example.scad

module One() { ... }
module Two() { ... }

render.sh

openscad -q -o one.stl --module One example.scad

Note that there is no --module option, but that is what I'm attempting to do. The workaround would be to make another .SCAD file e.g. one.scad which includes example.scad, and simply calls One(); within and render that file from the shell file. But this is not ideal.

$\endgroup$

3 Answers 3

1
$\begingroup$

The openscad -D option can actually include arbitrary code, not just variable definitions, so you can include a call to the module. You can even use /dev/null on Linux or NUL on Windows as the input file and have 100 % of your code in -D statements.

$\endgroup$
1
  • 1
    $\begingroup$ Maybe showing how exporting the STL works would benefit the answer. $\endgroup$
    – 0scar
    Commented Apr 28, 2021 at 5:08
1
$\begingroup$

OpenSCAD doesn't have such an option on the command line, but the general idiom I believe you want to use is have .scad source files which are modules include invocations of the module(s) at the top-level controllable by variables you can set on the command line or GUI customizer interface. Any such invocations will be ignored if the file is used (via use directive) in another file so they don't hurt its status as a library and make it easier to preview/test. So for example you could have:

wantOne = false;
if (wantOne) One();

and then set wantOne to true from the command line.

$\endgroup$
1
  • $\begingroup$ Thanks. I think that is better than having a separate .scad just to call each module. $\endgroup$
    – Josh M.
    Commented Nov 30, 2020 at 13:30
1
$\begingroup$

elaborating on the answer from @r-github-stop-helping-ice, I find the following pattern useful:

example.scad
module a() { .. }
module b() { .. }
module c() { .. }

module print(item=0) {
  if (item==1) a();
  if (item==2) b();
  if (item==3) c();
}

item = 0;
print(item);
render.sh
#!/bin/bash
for i in {1..3}; do
  openscad -D item=${i} -o item_${i}.stl example.scad
done
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .