openscad

Run OpenSCAD module and capture ECHO output


I have a chain of echo calls in my OpenSCAD model that generates some sort of Bill of Materials(BOM) that I would like to capture using command line.

Let's assume the model is valid and the syntax is correct. I would like to call it and get output rather than launching GUI.

How to do it?

OS is Linux
OpenSCAD version 2021.01 

Solution

  • I think I've found a workaround:

    // echo argument must look like valid JSON string
    echo(str("{\"support_plate_sqm\":", 0.37917, "}"));
    
    # grep ECHO filters out all warnings, rendering time and other statics
    #
    # sed is a combination of 3 commands:
    # (1) delete "ECHO: " prefix that we no longer need
    # (2) and (3) remove unnecessary first and last double quote " so that output becomes valid jsonl
    #
    # jq is used as an example of an app that consumes valid jsonl output
    openscad 000-000.scad -o $(mktemp).stl 2>&1 | grep ECHO | sed 's/ECHO: //;s/^.//;s/.$//' | jq -c '.support_plate_sqm'
    

    Output:

    0.37917
    

    Related OpenSCAD Github issue