Fastest way to copy pixels between custom render engine (C++) and Blender (Python)?

Hi!

I’m looking for the fastest way to copy pixels generated on C++ side to Python. What I’m doing now is:

  • generate pixels in c++
  • copy them to boost::python::list
  • assign it in python to layer.rect

Even when I create empty list, pass it to c++ via reference, fill it, then assign it to layer.rect, assigning alone takes around 30-40ms on my pc. My first thought was to pass layer.rect by reference and fill it on c++ side, but python throws error as bpy_array_list doesnt match boost::python::list (thought it indeed is list since I’m assigning list of pixels anyway…).

Ideal solution would be getting pointer to array of pixels (array of rgba), pass to c++ and fill it, but afaik python lists are different than c++ arrays, and layer.rect isn’t list anyway. This would be ideal because copying pixels would be just one call to glReadPixels or Vulkan equivalent, done.

So, how to do it better? Is there any working C/C++ api for blender plugins? Or it could be done with Python?
Already found this: https://wiki.blender.org/index.php/Dev:2.8/Source/Viewport/EngineAPI but it’s not well documented, maybe I should check Cycles or Eevee sources.

Thanks in advance.

I’m not aware of a C++ plugin API for Blender, but I’m not very familiar with the C/C++ side of Blender’s code. Would any of the other Blender-compatible 3rd party rendering options (yafaray, renderman, v-ray, mitsuba, etc) do something like this? That’s what I would check first. If that’s a dead end, you would probably have better luck getting an answer for this on the Blender committers mailing list or the IRC dev channel then here. Not many active C/C++ people on blenderartists.

Easiest way is to save array of pixels as image file. Then import this file into blender image editor. After that python can access pixels through

bpy.data.images["image_name"].pixels

@nBurn I’m checking cycles source now, since its also standalone engine.
@Ko. saving and opening image will take way more time than copying array/list of floats