Create a gist now

Instantly share code, notes, and snippets.

@knowuh /photo.py
Last active Jun 26, 2018

Embed
What would you like to do?
Blender script to turn an image data block into 3D cubes...
#----------------------------------------------------------
# File photo.py - cubify a named image in project.
# @knowuh (Noah Paessel) http://bit.ly/photoblend
# License: MIT ( http://opensource.org/licenses/MIT )
#----------------------------------------------------------
import bpy
from random import uniform
def make_material(color):
alpha = 1.0
red, green, blue, alpha = color
color_name = '%(red)03d_%(green)03d_%(blue)03d' % {"red": red, "green": green, "blue": blue}
color = bpy.data.materials.new(color_name)
color.use_nodes = True
Diffuse_BSDF = color.node_tree.nodes['Diffuse BSDF']
Diffuse_BSDF.inputs[0].default_value = [red, green, blue, alpha]
return color
def draw_pix(x, y, col):
material = make_material(col)
r, g, b, a = col
size = 16 - ((r + g + b) * 4)
z = size
bpy.ops.mesh.primitive_cube_add(location=(x, y, z))
bpy.ops.transform.resize(value=(0.9,0.9,size))
new_obj = bpy.context.active_object
new_obj.data.materials.append(material)
def cubify(image_name):
myImage = bpy.data.images[image_name]
width, height = myImage.size
for y in range(0, height):
for x in range(0, width):
block_number = (y * width) + x
color = []
for color_index in range(0, 4):
index = (block_number * 4) + color_index
color.append(myImage.pixels[index])
draw_pix(x * 2, y * 2, color)
print ("y: %(y)04d / %(height)04d" % {"y": y, "height": height})
cubify('sprite.png')
@Steveps3

This comment has been minimized.

Show comment
Hide comment
@Steveps3

Steveps3 Sep 16, 2014

Would the script run faster if it checked the material existed before creating a new material for each pixel. The script appears to get slower and slower which suggests that it is eating more and more memory. Less materials would improve this.

Would the script run faster if it checked the material existed before creating a new material for each pixel. The script appears to get slower and slower which suggests that it is eating more and more memory. Less materials would improve this.

@SDraw

This comment has been minimized.

Show comment
Hide comment
@SDraw

SDraw Mar 31, 2015

Nice trick can be used to gain performance:

  • Separate Y for equal parts: (0,32),(32,64) and etc.
  • Use script to create one part
  • Save .blend file
  • Save this .blend file as copy
  • Remove all objects
  • Create next small part.
  • Repeat
  • And then just append all created .blend files into one.

SDraw commented Mar 31, 2015

Nice trick can be used to gain performance:

  • Separate Y for equal parts: (0,32),(32,64) and etc.
  • Use script to create one part
  • Save .blend file
  • Save this .blend file as copy
  • Remove all objects
  • Create next small part.
  • Repeat
  • And then just append all created .blend files into one.
@papataci

This comment has been minimized.

Show comment
Hide comment
@papataci

papataci Nov 24, 2015

Nice script, I've added a conditional to generate the voxel only if the Alpha is not 0, i think is useful:

        #gets the value of the Alpha
        alpha = myImage.pixels[(block_number * 4) + 3]

        if alpha != 0:
            for color_index in range(0, 4):
                index = (block_number * 4) + color_index
                color.append(myImage.pixels[index])              
            draw_pix(x * 2, y * 2, color)
    print ("y: %(y)04d / %(height)04d" % {"y": y, "height": height})

cubify('image.png')

Nice script, I've added a conditional to generate the voxel only if the Alpha is not 0, i think is useful:

        #gets the value of the Alpha
        alpha = myImage.pixels[(block_number * 4) + 3]

        if alpha != 0:
            for color_index in range(0, 4):
                index = (block_number * 4) + color_index
                color.append(myImage.pixels[index])              
            draw_pix(x * 2, y * 2, color)
    print ("y: %(y)04d / %(height)04d" % {"y": y, "height": height})

cubify('image.png')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment