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

CC BY-SA 4.0
2021-04-28T05:08:36.927Z
Maybe showing how exporting the STL works would benefit the answer.
0scar
CC BY-SA 4.0
2021-04-28T00:14:16.143Z
2021-04-28T04:49:50.300Z
2021-04-28T04:49:50.300Z
whitis
<p>elaborating on the answer from @r-github-stop-helping-ice, I find the following pattern useful:</p>
<h6>example.scad</h6>
<pre><code>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);
</code></pre>
<h6>render.sh</h6>
<pre><code>#!/bin/bash
for i in {1..3}; do
  openscad -D item=${i} -o item_${i}.stl example.scad
done
</code></pre>

CC BY-SA 4.0
2022-11-12T17:54:33.583Z
2022-11-12T17:54:33.583Z
ellemenno
<p>OpenSCAD doesn't have such an option on the command line, but the general idiom I believe you want to use is have <code>.scad</code> 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 <code>use</code> 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:</p>
<pre><code>wantOne = false;
if (wantOne) One();
</code></pre>
<p>and then set <code>wantOne</code> to <code>true</code> from the command line.</p>

CC BY-SA 4.0
2020-11-30T13:30:38.250Z
Thanks. I think that is better than having a separate .scad just to call each module.
Josh M.
CC BY-SA 4.0
2020-11-30T04:35:45.003Z
2020-11-30T04:35:45.003Z
R.. GitHub STOP HELPING ICE
<p>Given a .SCAD file which contains some modules, how can I execute one of those modules from the command line?</p>
<p><strong>example.scad</strong></p>
<pre><code>module One() { ... }
module Two() { ... }
</code></pre>
<p><strong>render.sh</strong></p>
<pre><code>openscad -q -o one.stl --module One example.scad
</code></pre>
<p>Note that there is no <code>--module</code> option, but that is what I'm attempting to do. The workaround would be to make another .SCAD file e.g. <code>one.scad</code> which includes <code>example.scad</code>, and simply calls <code>One();</code> within and render that file from the shell file. But this is not ideal.</p>

<openscad>
Export STL from OpenSCAD command line, calling module within script?
Josh M.
# Export STL from OpenSCAD command line, calling module within script?

(Asked by: username_0 on 2020-11-29)

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.

**Tags:** openscad

---

## 3 answers

### Answer by username_1 on 2021-04-28. Score: 1 | Accepted answer

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.

> **Comments:**
> **username_2**: Maybe showing how exporting the STL works would benefit the answer.  

---

### Answer by username_3 on 2022-11-12. Score: 2

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


---

### Answer by username_4 on 2020-11-30. Score: 1

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.

> **Comments:**
> **username_0**: Thanks. I think that is better than having a separate .scad just to call each module.  

---

