Skip to content

BoD/k2o

Repository files navigation

Maven Central Maven Snapshots

k2o

K2o or "Kotlin to OpenSCAD" is a Kotlin DSL to generate OpenSCAD code.

The idea

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:

  • All the benefits of a modern language with functions, classes, packages, etc. and a proper type system
  • Great IDE support (autocompletion, refactoring, etc.)
  • Benefit from a huge ecosystem of Kotlin (and Java!) libraries
  • The ability to share libraries on Maven Central or other repositories

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.

Usage

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.

FAQ

@Composable?

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.

Author and license

Copyright (C) 2025-present Benoit 'BoD' Lubek (BoD@JRAF.org).

Licensed under the Apache License, Version 2.0.

About

An OpenSCAD Kotlin DSL

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages