Contributed by @povmaniaco with minor changes by me. Differential Revision: https://developer.blender.org/D9263