Kukan Kogei -空間工芸-

Notes about my 3d printing artcrafts

Reaction Diffusion using C code with Sverchok SNLite node in Blender

f:id:asahidari:20210806213423p:plain

Overview

This entry describes Reaction Diffusion (RD) simulation in Blender, with C code using Sverchok ScriptNodeLite(SNLite) node.

Description

This aim is to enable RD simulation on Blender's node editor, not just to get RD's 3D models.

If you want to make RD 3D models, you can use Ready or Blender's Tissue add-on. And Jimmy Gunawan have useful tutorials like this or this.

When you try to write script for RD simulation and run in Blender, it needs huge amount of calculation and much amount of time. So it is not realistic to implement RD with only plain python. Sverchok add-on has tried to solve this problem with some techniques, such as using python's numba module with JIT compile.
Mesh growth & Reaction Diffusion in SV · Issue #3384 · nortikin/sverchok · GitHub

To use C library, this project is derived from my template project to run C code.
GitHub - asahidari/run_c_code_with_snlite_b3d: An example of running C code in Blender Sverchok add-on.

Using C code is not normal way for Blender's (and Sverchok's) scripting, and you need external compiler to build C library. So this technique is just experimental.

Scripts and files

Scripts and C codes are included in my GitHub repository.
GitHub - asahidari/ReactionDiffusion_SNLite_b3d: Reaction Diffusion simulation with Sverchok SNLite node and C codes in Blender.

Idea of these scripts are originated from @zeffi's gist and nortikin/sverchok#1734.

Files in this repository includes:

  • BasicRD_2d
    • BasicRD_2d.py : Drawing RD from a rectangle with C code
    • BasicRD_2d.c : C code for BasicRD_2d.py
  • BasicRD_3d
    • BasicRD_3d.py : Drawing RD from a cube with C code
    • BasicRD_3d.c : C code for BasicRD_3d.py
    • BasicRD_3d_standalone.py : Drawing RD from a cube "without" C code
  • RD_on_mesh
    • RD_on_mesh.py : Drawing RD on mesh with C code
    • RD_on_mesh.c : C code for RD_on_mesh.py
    • RD_on_mesh_standalone.py : Drawing RD on mesh "without" C code

When you use "standalone" versions, these do not depend on any C libraries, but processing speeds are very slow.

How to use

  • Compile C code and make C dynamic library.

    •   :: Windows
        cl.exe /D_USRDLL /D_WINDLL Basic_RD_xd.c /MT /link /DLL /OUT:libBasic_RD_xd.dll
      
    •   # macOS
        gcc -dynamiclib -o ./libBasic_RD_xd.dylib ./Basic_RD_xd.c
      
    •   # Linux
        gcc -c -fPIC Basic_RD_xd.c -o Basic_RD_xd.o
        gcc Basic_RD_xd.o -shared -o libBasic_RD_xd.so
      
  • Launch Blender and open Sverchok node editor.

  • Add Script Node Lite (SNLite) node.
  • Open Text editor in Blender.
  • Copy and paste one of the Python script to the text buffer.
  • Modify "load_library" arguments in the script to load your library (if you use "with C code" script).
  • Write the the text buffer name to the node and click the right button.

Node Tree Examples

Regarding Basic_RD_2d example, see nortikin/sverchok#1734 .

Image below is a Basic_RD_3d example, using Meta ball for each output vertices.
f:id:asahidari:20210806234957p:plain
Next image is an RD_on_mesh example, using the output for vertex colors.
f:id:asahidari:20210806235017p:plain

References