
OpenSCAD DSL enabling declarative 3D modelling and programmatic generation of OpenSCAD code, with composable primitives, transformations, modules and reusable templates for parametric designs.
K2o or "Kotlin to OpenSCAD" is a Kotlin DSL to generate OpenSCAD code.
While OpenSCAD is truly great in general, and its language is actually quite good (and already pretty similar to Kotlin!), the more I've been using it, the more I wished I could just "do OpenSCAD in Kotlin".
This would bring a few advantages:
This library is an attempt to bring that to life by providing an OpenSCAD DSL: write Kotlin code which when run outputs OpenSCAD code. You can then open the result in OpenSCAD's UI or just run the OpenSCAD CLI to convert it to stl.
Add the dependency to your build.gradle.kts:
implementation("org.jraf.k2o:k2o:$latestVersion")Then call standard OpenSCAD functions (or your own) inside the openScad { } block:
fun main() {
openScad {
CubeMinusSphere()
}
}
@Composable
private fun CubeMinusSphere() {
difference {
Cube(size = 10)
translate(x = 2.5, y = 2.5, z = 2.5) {
Sphere(radius = 5)
}
}
}This will output:
$fa = 0.1;
$fs = 0.1;
difference() {
cube(10);
translate([2.5, 2.5, 2.5]) {
sphere(r = 5);
}
}As you can see, the DSL is very close to OpenSCAD itself so you should feel right at home.
While this doesn't really use anything from Jetpack Compose, I found the @Composable annotation is a good fit for this kind of DSL where
functions represent UI elements (3D elements in this case) that are rendered together.
It is also a nice way to have the context around without having to pass it explicitly, and to force the use of the DSL only inside the
openScad block or other composable functions.
Copyright (C) 2025-present Benoit 'BoD' Lubek (BoD@JRAF.org).
Licensed under the Apache License, Version 2.0.
K2o or "Kotlin to OpenSCAD" is a Kotlin DSL to generate OpenSCAD code.
While OpenSCAD is truly great in general, and its language is actually quite good (and already pretty similar to Kotlin!), the more I've been using it, the more I wished I could just "do OpenSCAD in Kotlin".
This would bring a few advantages:
This library is an attempt to bring that to life by providing an OpenSCAD DSL: write Kotlin code which when run outputs OpenSCAD code. You can then open the result in OpenSCAD's UI or just run the OpenSCAD CLI to convert it to stl.
Add the dependency to your build.gradle.kts:
implementation("org.jraf.k2o:k2o:$latestVersion")Then call standard OpenSCAD functions (or your own) inside the openScad { } block:
fun main() {
openScad {
CubeMinusSphere()
}
}
@Composable
private fun CubeMinusSphere() {
difference {
Cube(size = 10)
translate(x = 2.5, y = 2.5, z = 2.5) {
Sphere(radius = 5)
}
}
}This will output:
$fa = 0.1;
$fs = 0.1;
difference() {
cube(10);
translate([2.5, 2.5, 2.5]) {
sphere(r = 5);
}
}As you can see, the DSL is very close to OpenSCAD itself so you should feel right at home.
While this doesn't really use anything from Jetpack Compose, I found the @Composable annotation is a good fit for this kind of DSL where
functions represent UI elements (3D elements in this case) that are rendered together.
It is also a nice way to have the context around without having to pass it explicitly, and to force the use of the DSL only inside the
openScad block or other composable functions.
Copyright (C) 2025-present Benoit 'BoD' Lubek (BoD@JRAF.org).
Licensed under the Apache License, Version 2.0.