Add Catmull-Clark subdivision to Sverchok
Created by: GeneralPancakeMSTR
Problem statement
Catmull-Clark Subdivision (usually as OpenSubdiv) is an important tool standard to most (if not all) poly-based 3D modeling programs, and a core component of many users' modeling pipeline (including mine).
However, in Sverchok, there is no way to apply Catmull-Clark subdivision directly to mesh data, which is a significant drawback of an otherwise very powerful and complete tool.
Solution
A potential solution to this problem would be to somehow wrap the OpenSubdiv subdivision algorithm in Python, to make it compatible with Sverchok.
I am pleased to say that I have achieved this, specifically using ctypes for wrapping the C++ code from OpenSubdiv. I have also forked Sverchok, written a node that implements my subdivision wrapper, and tested its functionality directly in Blender:
Implementation
-
The code I wrote for wrapping OpenSubdiv's subdivision algorithm is available at my pyOpenSubdivision repository, which includes instructions on how to build and test it.
-
My fork of Sverchok that includes the
OpenSubdiv
node is available here. The modifications are as follows:-
Added
nodes/modifier_change/opensubdivide.py
: TheOpenSubdiv
node. -
Added
utils/modules/ctypes_pyOpenSubdiv.py
: Python code which communicates with the C++ code (using ctypes) that implements the OpenSubdiv Subdivision function and exposes it in a "friendly" way (i.e. as an easily useable function) to theOpenSubdiv
node (opensubdivide.py
). -
Added
utils/modules/ctypes_OpenSubdiv.dll
: C++ code compiled into a Dynamic Linked Library (.dll
) to expose the OpenSubdiv Subdivision function to ctypes (i.e. toctypes_pyOpenSubdiv.py
), on Windows x64 platforms. -
Added
utils/modules/ctypes_OpenSubdiv.so
: C++ code compiled into a Dynamic Library to expose the OpenSubdiv Subdivision function to ctypes (i.e. toctypes_pyOpenSubdiv.py
), on Linux x64 platforms. -
modified: .gitignore
: +!/utils/modules/ctypes_OpenSubdiv.so
, to include the Linux dynamic library file in version control. -
modified: index.md
: To make theOpenSubdiv
node available from the Sverchok menu.
... ## Modifier Make LineConnectNodeMK2 --- SvOpenSubdivideNode <- Add the OpenSubdiv node to Sverchok menu SvSubdivideNodeMK2 SvSubdivideToQuadsNode SvOffsetLineNode SvContourNode --- ... ```
-
Advantages
- Speed: The OpenSubdiv library is extremely fast, capable of performing simultaneous subdivision on many meshes in roughly real time, the below example shows 3 levels of subdivision applied to 100 randomly variable cubes:
-
Low User Overhead: No extra or complicated steps must be taken to activate the
OpenSubdiv
node, and because ctypes is built into python, the linked libraries (.dll
and.so
files) should not be python version-sensitive.
Disadvantages
- Platform Dependency: A dynamic library file containing the C++ code in binary format must be compiled separately for every platform Blender is available on, i.e. Windows, Linux, and OS X Intel and ARM. I have compiled libraries for Linux and Windows, but have yet to compile for OS X (this should not be too complicated, though).