Skip to content
LimeHunter7 edited this page Jul 23, 2020 · 6 revisions

The manual has a (very short) explanation on how to use the command line: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Using_OpenSCAD_in_a_command_line_environment#Constants

A simple example for exporting multiple files using a batch-file/shell-script:

mode = 0;

module part1() {
     cube(20, center = true);
}

module part2() {
     cylinder(r = 5, h = 21, center = true);
}

module assembly() {
     difference() {
         part1();
         part2();
     }
}

if (mode == 1) {
     part1();
} else if (mode == 2) {
     part2();
} else {
     assembly();
}

Create a batch file (or shell script, or processing sketch, ...):

openscad -Dmode=0 -o all.stl params.scad
openscad -Dmode=1 -o part1.stl params.scad
openscad -Dmode=2 -o part2.stl params.scad 

Another example to convert all the .scad in a folder into .stl:

in a folder with .scad files, make a .bat file with text: for %%f in (*.scad) do openscad -o %%~nf.stl %%f

written in it, and i had to set the PATH using this else it didnt recognize openscad:

Guide for settings Windows PATH entries for CMD.EXE

  • Start the System Control Panel applet (Start - Settings - Control Panel - System).
  • Select the Advanced tab.
  • Click the Environment Variables button.
  • Under System Variables, select Path, then click Edit.

You'll see a list of folders separated by semicolons, as this example for my system shows:

C:\Program Files\Windows Resource Kits\Tools\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Support Tools\;C:\Program Files\Common Files\Roxio Shared\DLLShared;C:\Program Files\Common Files\Ulead Systems\MPEG;C:\Program Files\Intel\DMIX;C:\Program Files\Executive Software\Diskeeper\;C:\Program Files\Bonjour\;C:\Program Files\QuickTime\QTSystem\;C:\Program Files\Misc

You can add additional folders that you want to include in searches. I add a "C:\program files\misc" entry into which I place my standalone utilities, instead of copying them into C:\windows. Click OK.

You'll need to restart the processes (e.g., command prompt) that use the system path to see the added folders.

A Python + OpenScad example

The following pair of scripts shows how a Python script can use OpenScad constants to generate variations on the basic OpenScad model. 'x', 'y', and 'z' are the OpenScad constants used here to control the translation of a sphere.

Note:

  • the syntax of how the OpenScad command is built and passed in Python,
  • the need for a separate '-D' in the command for each constant,
  • each variable without a value assigned in the OpenScad script needs a constant passed in the command.

multi-mesh-generator.py

 import subprocess
 tissues = ["skin", "bone", "tendon", "ligament", "muscle", "loose_ct"]
 steps = ["0","10","20","30"]
 for i, tissue in zip(steps, tissues):
      for j in steps:
          out_filename = "sphere_{}_{}_{}.off".format(i,j,tissue)
          openscad_cmd = "openscad -o{} -Dx={} -Dy={} -Dz=0 movable_sphere2.scad ".format(out_filename,i,j)   
          print(openscad_cmd.split())
          subprocess.run(openscad_cmd.split())

movable_sphere2.scad

  module movable_sphere2()
  {
      translate([x,y,z])
        sphere(20, $fs = 0.01);
  }
  movable_sphere2();
Clone this wiki locally