SCA2D - Static Code Analysis for SCAD
{width=50%}
This is a linter for OpenSCAD. The focus is to properly lex the .scad files so that we can test for dangerous code like redefining variable from other scopes.
SCA2D has been used for a few years now by the OpenFlexure Microscope project, but may have issue with projects that use different conventions.
It can:
- Parse any
.scadfile. We don't yet do extensive testing. Please raise an issue if you find a parsing error in SCA2D. - Parse included and used
.scadfiles and checking variable, function, and module usage in different scopes. - Report for a number of code errors, code style issues, and some whitespace issues.
SCA2D is also experimenting with creating documentation while linting.
How to install
If you have Python 3.6 or later installed on your machine (and on the PATH) you can install SCA2D from PyPi by running the following command from a terminal
pip install sca2d
to upgrade your current versions of SCA2D run:
pip install sca2d --upgrade
You can check that SCA2D is installed by running the following:
sca2d -h
you should see a help screen.
Install issues
If you have problems with installation possible problems include:
- You may need to run
pip3instead ofpip - Python may not be on the PATH, this often happens in Windows depending on how python was installed
How to run
To analyse a .scad file run:
sca2d filename.scad
This will print any code messages to the screen.
To analyse all .scad files in a directory run:
sca2d dir_path
This will also print any code messages to the screen.
To also generate documentation run.
sca2d --docs dir_path.
Additionally the --project option can be used to set a project name for the documentation.
If you wish to inspect the parse tree that SCA2D generates and then uses you can instead run:
sca2d --output-tree filename.scad
the tree will be printed into a file called filename.sca2d.
SCA2D Docstring Conventions for OpenSCAD
To enable automatic documentation parsing and example image generation, SCA2D defines a simple docstring format for OpenSCAD code. These conventions are enforced and utilised by the SCA2D documentation tooling.
- Use
/* */-style C comments for docstrings. - Do not include leading
*s on each line. - Document parameters using Python-style directives (without indentation):
:param arg1: Description of the argument:return:or:returns:to describe return values:assert condition:to describe assertions or expected conditions (in place of:raises:)
- Multiline descriptions (for
:param,:return:, or:assert) should indent subsequent lines by 4 spaces. - All other content is treated as Markdown.
- Use fenced code blocks (
```) for examples or inline code.
Docstring Example
/* Create a box with optional bevelled edges.
:param size: A 2D vector specifying the width and height
:param bevel: Optional bevel radius (default 0)
:return: A 2D shape representing the box
*/
module box(size, bevel = 0) {
// ...
}
Rendering images for examples
To include a rendered image of an example use a fenced code block that begins with ```example. This blocks trigger automatic image generation as part of the documentation build.
Customisation is currently fairly limited.
- To set the camera angle use the special scad variables (
$vpt,$vpr, and$vpd) in your example. - By default preview mode is used to use render mode add the word
renderafterexample:```example render - By images have the axes showing, add
no-axesafterexampleto suppress rendering axes:```example no-axes
Other Options for Documenting OpenSCAD
Projects like openscad_docsgen also generate OpenSCAD docs.
We did consider following the same conventions, however, because openscad_docsgen does not parse the SCAD code, their conventions include a lot of repetition of information found in the code (such as function/module signatures). The format also contains a fairly rigid structure to the documentation, prevent simply adding docs as you go to specific functions.
The SCA2D approach is to parse the SCAD source first, to automatically create module and function signatures, so documentation can focus purely on usage, behaviour, and intent. This avoids redundancy, reduces maintenance overhead, and allows for lightweight Markdown-based docstrings that feel familiar to developers coming from Python or similar languages.
At the time of writing openscad_docsgen is more mature than SCA2D's documentation tooling, and has more options for generating example images.
How to get involved with SCA2D
The best way to get involved is to find bugs and then open an issue. You could also make changes and open a merge request.
Release files for sca2d 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sca2d-0.4.0.tar.gz | 51.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sca2d-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size:110.4 kB
Release files / sca2d-0.4.0.tar.gz
| Download URL | sca2d-0.4.0.tar.gz |
|---|---|
| Size | 51.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
418a48add8e04cfa3c9903a86f476ea81e6aefe212f572e84af1070e7c95b374
|
|
BLAKE2b-256 checksum How to use checksums |
eb3f474a80aaf5882a15bd178b2cf2e1ee17ddd376779b4bd3cb6bbb5c49c690
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.2.0 CPython/3.10.20
|
Release files / sca2d-0.4.0-py3-none-any.whl
| Download URL | sca2d-0.4.0-py3-none-any.whl |
|---|---|
| Size | 58.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4ecd036823f0092cd75079bfc26cbbb22d1ee41b455f4d03544272e5665cbbeb
|
|
BLAKE2b-256 checksum How to use checksums |
2dcda4ac2e3d4bb98c4a93e6e12663477e3f27d773e3a2b39a8b5ca55352f055
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.2.0 CPython/3.10.20
|