The naive approach
Tasked with representing some big data, let's see if blender can handle it. Here is some exploring first.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
D = bpy.data | |
# BlendDataImages.new(name, width, height, alpha=False, float_buffer=False) | |
image_object = D.images.new(name='pixeltest', width=4000, height=3000) | |
# image_object points to the newly created: bpy.data.images['pixeltest'] | |
num_pixels = len(image_object.pixels) | |
# well, turns out pixels is a little bit of a misnomer. | |
print(num_pixels) | |
# >>> 480000 | |
# A number 4 timeslarger than the expected 120000 pixels. It appears that | |
# .pixels is a list of the RGBA values of every pixel in sequence. | |
print( bpy.data.images['pixeltest'].file_format ) | |
# >>> 'TARGA' | |
# seems to be the internal default | |
print(image_object.pixels[:4]) | |
# >>> (0.0, 0.0, 0.0, 1.0) | |
# drawing a pixel, changing pixel content | |
for i in range(4): | |
image_object.pixels[0+i] = (1.0) |
adjusting the pixel on the last two lines above takes the most time. This image shows what the result is, zoomed in.
baby steps
This is relatively fast, but it's only 120 pixels in total. Try changing to 400*300 and you can expect it to take a lot longer, far too long to scale for big data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
D = bpy.data | |
image_object = D.images.new(name='pixeltest', width=40, height=30) | |
num_pixels = len(image_object.pixels) | |
# drawing a pixel, changing pixel content | |
for px in range(0, num_pixels-5, 12): | |
for i in range(4): | |
image_object.pixels[px+i] = (1.0) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
import random | |
D = bpy.data | |
image_object = D.images.new(name='pixeltest', width=40, height=30) | |
num_pixels = len(image_object.pixels) | |
# drawing a pixel, changing pixel content | |
for px in range(0, num_pixels-5, 12): | |
r = random.random() | |
g = random.random() | |
b = random.random() | |
a = (1.0) | |
cols = (r,g,b,a) | |
for i in range(4): | |
image_object.pixels[px+i] = cols[i] | |
What we know - end of naive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> bpy.data.images['pixeltest'].pixels.__class__ | |
#<class 'bpy_prop_array'> |
dm = [(1.0) for i in range(4800)] bpy.data.images['pixeltest'].pixels = dm # turns them all white, so maybe try constructing the array first, then assigning.This leads to a much faster way of pushing pixels. First create the image, then the array, then modify the array, then overwrite the image with the array data. The snippet below overwrites with a dark gray.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
import random | |
D = bpy.data | |
image_name = 'pixeltest2' | |
if not image_name in D.images: | |
image_object = D.images.new(name=image_name, width=400, height=300) | |
image_object = D.images[image_name] | |
num_pixels = len(image_object.pixels) | |
dm = [(0.2) for cp in range(num_pixels)] | |
image_object.pixels = dm | |
And it seems that the speed is now closer to acceptable, here is a version that does a 400*300 px overwrite.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
import random | |
D = bpy.data | |
image_name = 'pixeltest3' | |
if not image_name in D.images: | |
image_object = D.images.new(name=image_name, width=400, height=300) | |
image_object = D.images[image_name] | |
num_pixels = len(image_object.pixels) | |
dm = [(0.2) for cp in range(num_pixels)] | |
for i in range(0,len(dm),16): | |
for j in range(4): | |
dm[i+j] = (1.0) | |
image_object.pixels = dm | |