Specifying Clojure function schemas with Malli

Clojure is a dynamically typed language which means that functions can be called with arbitrary values. If the function cannot operate on the values (or if it calls a function which cannot operate on a value or part of a value) a runtime exception is thrown. Even worse, a function could exhibit undefined behaviour for the values provided.

Statically typed languages are often considered safer because the types of values are known at compile time so that the validity of values can be checked. However statically typed implementations tend to become inflexible. Type checking at compile time requires the types of the values held by variables to be defined at compile time. When trying to keep the system both modular and extensible, the developer is forced to come up with complicated type hierarchies and possibly even generic types.

There is a promising solution to this and this is using pre- and post-conditions (contracts) for functions. In particular there is Malli which allows you to specify schemas for each function using a concise syntax. Malli schemas can be defined, reused, and composed like types. Note that a value can fulfill multiple schemas (similar to Rust traits), however implementations and schemas exist independently from each other. This allows schemas to be more specific than static types will ever be (e.g. you can have a schema for lists with three or more elements).

Malli is optimized for good runtime performance. If performance is still a concern, one can leave schema validation disabled in production for some or all namespaces.

Here follows a short introduction to using Malli for function pre- and post-conditions:

You can use the Malli provider module to suggest possible schemas matching a list of values.

(require '[malli.provider :as mp])
(mp/provide [1])
; :int
(mp/provide [1 nil])
; [:maybe :int]
(mp/provide [[1 2 3]])
; [:vector :int]

The resulting schemas can be used to validate values.

(require '[malli.core :as m])
(m/validate [:vector :int] [1 2 3])
; true
(m/validate [:vector :int] [1 2 nil])
; false

Schemas are values which can be reused and composed.

(def N (m/schema [:int {:min 1}]))
(m/validate [:vector N] [0 1 2])
; false
(m/validate [:vector N] [1 2 3])
; true

It is possible to add predicate functions to schemas.

(def ordered-vector (m/schema [:and [:vector :int] [:fn (fn [v] (apply < v))]]))
(m/validate ordered-vector [2 5 3])
; false
(m/validate ordered-vector [2 3 5])
; true

Most importantly one can add pre- and post-conditions (function schemas) to functions.

(defn sqr
  {:malli/schema [:=> [:cat :double] :double]}
  [x]
  (* x x))

By default function schemas are ignored until the schemas are collected and the functions are instrumented. Note that here I am replacing the default exception thrower with a more human-readable one.

(require '[malli.instrument :as mi])
(require '[malli.dev.pretty :as pretty])
(mi/collect!)
(mi/instrument! {:report (pretty/thrower)})
(sqr 3.0)
; 9.0
(sqr "text")
; -- Schema Error ---------------------------------------------------------------- NO_SOURCE_FILE:1 --
;
; Invalid function arguments:
;
;   ["text"]
;
; Function Var:
;
;   user/sqr
;
; Input Schema:
;
;   [:cat :double]
;
; Errors:
;
;   {:in [0], :message "should be a double", :path [0], :schema :double, :value "text"}
;
; More information:
;
;   https://cljdoc.org/d/metosin/malli/CURRENT/doc/function-schemas
;
; ----------------------------------------------------------------------------------------------------

If you want to collect the schemas from a module (e.g. for testing purposes), you can specify the namespace as follows.

(mi/collect! {:ns ['the.module]})

You can also parse code to infer a general schema for a function header.

(require '[malli.destructure :as md])
(def infer (comp :schema md/parse))
(infer '[x])
; [:cat :any]

If you have a multiple arity function, you can use :function to specify the different alternatives.

(defn f
  {:malli/schema [:function [:=> [:cat :double] :double]
                            [:=> [:cat :double :double] :double]]}
  ([x] x)
  ([x y] (+ x y)))
(mi/collect!)
(mi/instrument! {:report (pretty/thrower)})
(f 2.0 3.0)
; 5.0
(f "test")
; ... error ...

There is also support for sequence schemas which allows a more compact schema in this case.

(defn f
  {:malli/schema [:=> [:cat :double [:? :double]] :double]}
  ([x] x)
  ([x y] (+ x y)))

Finally here is an example for a method accepting keyword arguments which took me some time to figure out.

(defn g
  {:malli/schema [:=> [:cat [:= :x] :double [:= :y] :double] :double]}
  [& {:keys [x y]}]
  (+ x y))
(mi/collect!)
(mi/instrument! {:report (pretty/thrower)})
(g :x 2.0 :y 3.0)
; 5.0
(g :x 2.0 :y "test")
; ... error ...

The only problem I encountered so far is that instrumentation clashes with Clojure type hints. I guess the solution is to instead provide the type hints at the call site instead where necessary for performance reasons.

Enjoy!

Updates:

You can also define recursive schemas by using a local registry:

(require '[malli.core :as m])
(def nested-vector
  (m/schema [:schema {:registry {::node [:or :int [:vector [:ref ::node]]]}}
                     [:ref ::node]]))
(m/validate nested-vector [[1 2] 3])
; true
(m/validate nested-vector [[1 :k] 3])
; false

One can use m/=> to add a Malli schema to a function. This comes in handy when you need to add a schema to a Clojure multimethod.

(require '[malli.core :as m])
(require '[malli.instrument :as mi])
(require '[malli.dev.pretty :as pretty])
(defmulti factorial identity)
(m/=> factorial [:=> [:cat integer?] integer?])
(defmethod factorial 0 [_]  1N)
(defmethod factorial :default [num]
   (* num (factorial (dec num))))
(mi/instrument! {:report (pretty/thrower)})
(factorial 10)
; 3628800N
(factorial 1.5)
; ... error ...

Create Blender bones and animate and import with Assimp

This article is about

  • creating objects and armatures consisting of bones
  • adding inverse kinematics
  • creating animations
  • exporting to glTF
  • importing the animations with Assimp

Here is the Youtube video showing the steps involved:

Here is a corresponding step-by-step procedure to create a proof-of-concept kinematic chain in Blender

  • create armature
    • first clear scene (in layout mode)
    • in Object mode add an armature which is going to be named “Armature”
    • rotate the armature around the y-axis by 90 degrees so that the bone points in the direction of the x-axis
    • in Edit mode move the tip of the bone to enlarge it
    • press E and extrude the bone to add a second bone to the chain
    • in Edit mode add an additional bone to the armature by pressing Shift-A and move it to the end of the previous chain
    • in the tree view rename the bones to “upper bone”, “lower bone”, and “stay IK”
  • set up inverse kinematic constraint
    • in Pose mode select the stay IK bone first (!) and then shift-click on the lower bone.
    • go to bone constraints (not constraints) and add an Inverse Kinematics constraint
    • set target to “Armature” and bone to “stay IK”
    • set the chain length to 2
    • note that the bone constraint should appear under the lower bone in the tree view
    • moving the stay IK bone in Pose mode should now update the kinematic chain of upper and lower bone
  • add objects and connect them to armature
    • in Object mode select the armature, go to Object properties -> Viewport display, and check name and in front
    • in Object mode create a cuboid and move it to the position of the upper bone
    • click on the cube and shift-click on the armature
    • switch to Pose mode, click on the cube, and shift-click on the upper bone
    • press Ctrl-P and set the parent to bone
    • moving the stay IK bone should now move the cuboid
    • add another cube and set the parent to the lower bone (similar to the last 5 steps but for the lower bone)
    • add a small sphere and move it to the stay IK bone position
    • select the armature in Object mode
    • switch to Pose mode and select the stay IK bone
    • add the “Child Of” bone constraint and set the target to the sphere
    • moving the sphere in Object mode should move the cuboids now
  • add top-level armatures
    • add another armature in Object mode and set the name to “Control” in the tree view
    • click the upper arm bone and shift click the control armature, use Ctrl-P and set it as parent object
    • set the parent object of the sphere to the control armature as well (object, keep transform)
    • moving the control bone now should move everything else
    • create a root armature
    • shift drag and drop control armature under root armature (an extra node in the tree is useful if you want to overwrite the top-level transform programmatically)
  • create animations and export to glTF
    • go to the animation editor
    • add two keyframes for 90 degree rotation of the control bone around the y-axis
    • switch from Dope sheet to Nonlinear animation editor and push down the action
    • select the control bone in the tree view
    • go to Edit -> Bake action and bake only the selected bones and Object data
    • delete the original action, rename the current action to “ControlAction”, and push it down
    • uncheck the control action
    • add two keyframes moving the sphere
    • select the armature and the sphere and bake another action
    • push down the generated actions and the sphere action and rename to the same name (e.g. “CubeAction”) so that they will later become one animation in glTF
    • check the control action back on so that all actions are checked now
    • export the glTF with the animations (no need to check bake all actions)

You can use the LWJGL Assimp Java library to import the animations into Clojure as follows:

(import '[org.lwjgl.assimp Assimp AIAnimation AINodeAnim])
(def scene (Assimp/aiImportFile "test.gltf" (bit-or Assimp/aiProcess_Triangulate Assimp/aiProcess_CalcTangentSpace)))
(.mNumAnimations scene)
; 2
(def animation (AIAnimation/create ^long (.get (.mAnimations scene) 1)))
(/ (.mDuration animation) (.mTicksPerSecond animation))
; 10.4166669921875
(.mNumChannels animation)
; 5
(map #(.dataString (.mNodeName (AINodeAnim/create ^long (.get (.mChannels animation) %)))) (range (.mNumChannels animation)))
; ("Sphere" "Armature" "Stay IK" "Lowe Bone" "Upper Bone")
(def node-anim (AINodeAnim/create ^long (.get (.mChannels animation) 4)))
(.dataString (.mNodeName node-anim))
; "Upper Bone"
(.mNumPositionKeys node-anim)
; 2
(.mNumRotationKeys node-anim)
; 250
(.mNumScalingKeys node-anim)
; 2
(def quaternion (.mValue (.get (.mRotationKeys node-anim) 249)))
[(.w quaternion) (.x quaternion) (.y quaternion) (.z quaternion)]
; [0.99631643 -1.20924955E-17 -1.5678631E-17 0.08575298]
(/ (.mTime (.get (.mRotationKeys node-anim) 249)) (.mTicksPerSecond animation))
; 10.4166669921875

I used tmux, Clojure with rebel-readline, and vim with vim-slime to do the coding.

For an in-depth introduction to rigging I can recommend Mark Alloway’s video on how to rig and animate a landing gear.

Enjoy!

Mechanical Rigging using Ryan King Art's tutorial

After finishing the Blender Guru donut tutorial I wanted to look into articulated objects. Initially I looked at Blender skeletons but then I found Ryan King Art’s tutorial on mechanical rigging.

Basically for each rigid object the following steps are executed:

  • The 3D cursor is set to a point on the center of the joint axis
  • The origin of the rigid object is set to the 3D cursor
  • The parent of the rigid object is set to the other object it is connected to via the joint (while maintaining the transformation)
  • All forbidden translation axes and rotation axes are locked

The resulting object then can be animated by defining keyframes for joint angles as shown in Blender Guru donut tutorial part 11. Rendering the video using the Cycles engine on my 8 core CPU took 3.5 hours. If you have a GPU supported by Blender this would take much less time.

See below for the resulting video:

Doing the Blender Guru donut tutorial

I finally did the Blender Guru donut tutorial for beginners. Blender is a powerful 3D editor to create models and animate them. Here is a series of images made while progressing through the tutorial.

Initially one creates a torus and distorts it. Furthermore the upper half of the torus is copied, solidified, and sculpted to create the icing of the donut.

image

One can set the material properties of the donut to make the icing more reflective and add subsurface scattering.

image

Here is another example using white icing.

image

One can use texture painting to add a bright ring at the middle height of the torus.

image

Using geometry nodes one can distribute cylindrical sprinkles on the surface and rotate them randomly around the surface normal. By doing weight painting one can ensure that the sprinkles are less frequent on the sides of the donut.

image

Furthermore one can select from a set of differently shaped sprinkles and color them randomly using a color map with a few distinct colors.

image

Using key, fill, and rim lighting, one can illuminate the donut.

image

Compositing is used to add a background with a blurred white circle creating a color gradient. Also one can add lens distortion and color dispersion to make the image look more like a photo.

image

See below for the resulting video:

I really enjoyed the tutorial and I hope I will remember the right tool at the right moment when creating models in the future.

All the time there are people finishing the tutorial and posting it on r/BlenderDoughnuts.

OpenGL example in Clojure using LWJGL version 3

Some time ago I published a minimal OpenGL example in Clojure using LWJGL version 2. I used LWJGL 2 because it is available as a Debian package unlike version 3. However one can get LWJGL 3.3.2 with native bindings from the Maven Central Repository.

Note that LWJGL version 3 consists of several packages. Furthermore most packages have native bindings for different platforms. See LWJGL customization page for more information. There is a Leiningen LWJGL project example by Roger Allen which shows how to install all LWJGL libraries using Leiningen. However here we are going to use the more modern approach with a deps.edn file and we are going to use more efficient OpenGL indirect rendering.

The deps.edn file uses symbols of the form <groupID>/<artifactId>$<classifier> to specify libraries. For LWJGL version 3 the classifier is used to install native extensions. The deps.edn file for installing LWJGL with OpenGL and GLFW under GNU/Linux is as follows:

There are more native packages for lwjgl-opengl such as natives-windows and natives-macos.

Displaying a window with GLFW instead of LWJGL 2 is a bit different. The updated code to use lwjgl-glfw and lwjgl-opengl to display a triangle with a small texture is shown below:

If you download the two files, you can run the code as follows:

clj -M raw-opengl-lwjgl3.clj

It should show a triangle like this:

image

Any feedback, comments, and suggestions are welcome.

Enjoy!