This document provides context for AI assistants (like Claude) working on the scad-to-png project.
## Project Overview
`scad-to-png` is a Node.js CLI tool that converts OpenSCAD files to PNG images without requiring OpenSCAD to be installed. It uses OpenSCAD's WebAssembly (WASM) port for the conversion.
## Key Technical Details
### OpenSCAD WASM Integration
The most challenging part of this project was getting OpenSCAD WASM to work in a headless Node.js environment:
1.**Use the Web Version**: The OpenSCAD WASM build from `openscad.org` has two versions:
- **Node version** (`OpenSCAD-*-WebAssembly-node.zip`) - Has GUI initialization code that fails in headless environments
- **Web version** (`OpenSCAD-*-WebAssembly-web.zip`) - Works properly with `noInitialRun: true`
2.**Critical Initialization**: The OpenSCAD module must be initialized as:
```javascript
const instance = await OpenSCAD({
noInitialRun: true, // Prevents GUI mode
wasmBinary: wasmBinary, // Provide WASM binary directly
print: (text) => console.log(text),
printErr: (text) => console.error(text)
});
```
3.**File System**: Uses Emscripten's virtual file system. Files must be written before calling `callMain()`.
4.**Parametric Support**: OpenSCAD parameters can be passed using the `-D` flag, which works identically to the native OpenSCAD CLI. Example: `-D width=30 -D text="Hello"`
1.**OpenSCAD Errors**: The error messages after "OpenSCAD Error:" are from OpenSCAD's internal logging and can usually be ignored if the conversion succeeds
2.**Checking WASM Loading**: Add console.log in `locateFile` callback to see what files are being requested
3.**STL Parsing Issues**: Write the STL buffer to a file and examine it with a text editor (for ASCII) or hex editor (for binary)
4.**Rendering Issues**: Export projected 2D coordinates to verify the projection math