Search code examples
command-linebatch-processingopenscad

How should I pass values to openSCAD in command line?


I want to apply a certain modification on a bunch of stl files. So I wrote an openSCAD program and wanted to script from the command line, but it doesn't work and I can't figure out why.

According to the man page, I thought the following command would work but it doesn't (Ah, also I use an appimage version of openSCAD (for in case it would be important):

OpenSCAD-2021.01-x86_64.AppImage -o [OUTPUT FILE NAME] 'myscript.scad' -D filename=[INPUT FILE NAME] 

However, it appears, the -D options doesn't do what I expect. So I made a little test script:

echo ("START");

filename1="cube.stl";

echo(filename1=filename1, filename2=filename2);

minkowski() {
  import(filename1);
  sphere(r=1, $fn=24);
}

translate([30, 0, 0])
  minkowski() {
    import(filename2);
    sphere(r=1, $fn=24);
  }

If I test myscript.scad with the gui version of openSCAD, it gives the following result (which is expected):

Compiling design (CSG Tree generation)...
ECHO: "START"
WARNING: Ignoring unknown variable 'filename2' in file test.scad, line 5 
ECHO: filename1 = "cube.stl", filename2 = undef
WARNING: Ignoring unknown variable 'filename2' in file test.scad, line 14 
Compiling design (CSG Products generation)...
ERROR: Unsupported file format while trying to import file '""', import() at line 14 
Geometries in cache: 5
Geometry cache size in bytes: 128056
CGAL Polyhedrons in cache: 0
CGAL cache size in bytes: 0
Compiling design (CSG Products normalization)...
Normalized tree has 2 elements!
Compile and preview finished.
Total rendering time: 0:00:00.177

However, if I test it in command line, no value is initialized, and by the way, I have no idea what are both those warning on line 19 and 20 while my scad file only has 16 lines:

MyPrompt> ./OpenSCAD-2021.01-x86_64.AppImage -o 'test.stl' 'test.scad' -D filename1=cube.stl -D filename2=cube.stl 
WARNING: Ignoring unknown variable 'cube' in file test.scad, line 19
WARNING: Ignoring unknown variable 'cube' in file test.scad, line 20
ECHO: "START"
ECHO: filename1 = undef, filename2 = undef
ERROR: Unsupported file format while trying to import file '""', import() at line 8
Geometries in cache: 4
Geometry cache size in bytes: 118112
CGAL Polyhedrons in cache: 1
CGAL cache size in bytes: 1362752
Total rendering time: 0:00:00.854
   Top level object is a 3D object:
   Simple:        yes
   Vertices:      576
   Halfedges:    3168
   Edges:        1584
   Halffacets:   2024
   Facets:       1012
   Volumes:         3

For reference the cube.stl was made with the cube(); command and contains:

00000000  4f 70 65 6e 53 43 41 44  20 4d 6f 64 65 6c 0a 00  |OpenSCAD Model..|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000050  0c 00 00 00 00 00 00 80  00 00 00 00 00 00 80 3f  |...............?|
00000060  00 00 00 00 00 00 80 3f  00 00 80 3f 00 00 80 3f  |.......?...?...?|
*
00000080  00 00 80 3f 00 00 00 00  00 00 00 00 00 00 00 00  |...?............|
00000090  80 3f 00 00 80 3f 00 00  00 00 00 00 80 3f 00 00  |.?...?.......?..|
000000a0  00 00 00 00 80 3f 00 00  80 3f 00 00 00 00 00 00  |.....?...?......|
000000b0  00 00 00 00 80 3f 00 00  00 00 00 00 00 00 00 00  |.....?..........|
000000c0  00 00 80 bf 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000d0  00 00 80 3f 00 00 80 3f  00 00 00 00 00 00 80 3f  |...?...?.......?|
000000e0  00 00 00 00 00 00 00 00  00 00 00 00 00 80 00 00  |................|
000000f0  00 00 00 00 80 bf 00 00  80 3f 00 00 80 3f 00 00  |.........?...?..|
00000100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000110  00 00 00 00 80 3f 00 00  00 00 00 00 00 00 00 00  |.....?..........|
00000120  00 00 80 bf 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000130  00 00 00 00 00 00 80 3f  00 00 00 00 00 00 80 3f  |.......?.......?|
00000140  00 00 00 00 00 00 00 00  00 00 80 3f 00 00 00 00  |...........?....|
00000150  00 00 00 00 80 bf 00 00  00 80 00 00 80 3f 00 00  |.............?..|
00000160  00 00 00 00 80 3f 00 00  00 00 00 00 00 00 00 00  |.....?..........|
*
00000180  00 00 80 3f 00 00 00 80  00 00 00 00 00 00 80 3f  |...?...........?|
00000190  00 00 00 00 00 00 80 3f  00 00 80 3f 00 00 80 3f  |.......?...?...?|
*
000001b0  00 00 00 00 80 3f 00 00  00 00 00 00 00 00 00 00  |.....?..........|
000001c0  80 3f 00 00 80 3f 00 00  00 00 00 00 80 3f 00 00  |.?...?.......?..|
000001d0  00 00 00 00 80 3f 00 00  80 3f 00 00 00 00 00 00  |.....?...?......|
000001e0  00 00 00 00 00 00 00 00  00 00 80 3f 00 00 00 80  |...........?....|
000001f0  00 00 80 3f 00 00 80 3f  00 00 00 00 00 00 00 00  |...?...?........|
00000200  00 00 80 3f 00 00 80 3f  00 00 80 3f 00 00 80 3f  |...?...?...?...?|
00000210  00 00 80 3f 00 00 00 00  00 00 00 00 80 3f 00 00  |...?.........?..|
00000220  00 00 00 00 00 00 00 00  80 3f 00 00 80 3f 00 00  |.........?...?..|
00000230  80 3f 00 00 80 3f 00 00  00 00 00 00 00 00 00 00  |.?...?..........|
00000240  80 3f 00 00 00 00 00 00  00 00 80 bf 00 00 00 00  |.?..............|
00000250  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000260  00 00 00 00 00 00 80 3f  00 00 80 3f 00 00 00 00  |.......?...?....|
00000270  00 00 80 3f 00 00 00 00  00 00 00 00 80 bf 00 00  |...?............|
00000280  00 80 00 00 00 00 00 00  00 00 00 00 80 3f 00 00  |.............?..|
00000290  80 3f 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |.?..............|
000002a0  00 00 00 00 00 00 00 00  80 3f 00 00              |.........?..|

Solution

  • Ok, Got it :

    The problem comes from the fact that openSCAD wants to receive string with the quotation marks. However, the shell will try to eat them. So, the correct calling syntax will be :

    MyPrompt> ./Téléchargements/OpenSCAD-2021.01-x86_64.AppImage -o 'test.stl' 'test.scad' -D filename1='"cube.stl"' -D filename2='"cube.stl"'
    ECHO: "START"
    ECHO: filename1 = "cube.stl", filename2 = "cube.stl"
    Geometries in cache: 4
    Geometry cache size in bytes: 136832
    CGAL Polyhedrons in cache: 1
    CGAL cache size in bytes: 1524416
    Total rendering time: 0:00:01.021
       Top level object is a 3D object:
       Simple:        yes
       Vertices:      672
       Halfedges:    3536
       Edges:        1768
       Halffacets:   2200
       Facets:       1100
       Volumes:         3
    

    This also explains the warnings, because it wasn't try to interpret cube.stl as a string, but as a variable name in dot notation (so the main variable name was indeed cube).