1
$\begingroup$

I have a numpy array which I'd like map onto a cube inside blender. Is there a way to make texture out of it, without writing to file? This array will change over time and writing to disk and reading it again seems very slow and also in future I might have many numpy arrays to texture the cube.

Bonus question:) Is it possible to do this without PIL in the middle?

$\endgroup$

2 Answers 2

1
$\begingroup$

I don't think you can change a texture applied to a mesh without saving, no. I could be wrong, but I'm pretty sure that's just not how Blender works. You could just render many textures to one very big texture as a spritesheet, and scroll through that as each new texture is needed.

If you just want to preview the texture without saving the file, however, that is possible using Sverchok. It's designed with NumPy in mind, the view is updated via OpenGL in realtime, and it doesn't save the results unless you tell it to. Most of its use is for 3D, but it has a mature texture viewer node: http://nikitron.cc.ua/sverch/html/nodes/viz/texture_viewer.html

$\endgroup$
5
$\begingroup$

Here is example TestImg to which we write numpy array (in this example it is all zeros). Thing to remember if image size is 100px width 100px height, and 4 channels then numpy array size must be 100*100*4. eg:

numpy.zeroes((100,100,4), dtype = float16)

Example

if "TestImg.png" not in bpy.data.images.keys():
    bpy.data.images.new("TestImg.png", width=128, height=128, alpha=True, float_buffer=False)
outputImg = bpy.data.images["TestImg.png"] 
input_res = int(math.sqrt(outputImg.size[0]))
np_out_img = np.zeros((input_res,input_res,4), dtype = np.float16)
outputImg.pixels = np_out_img.ravel()  #flatten the array to 1 dimension and write it to testImg pixels
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .