Last active
December 15, 2015 08:19
-
-
Save enzyme69/5229993 to your computer and use it in GitHub Desktop.
Blender Sushi script that imports small size Pixel Art (under 128 x 128 pixel size) and convert it into a grid of 3D cubes with Vertex Color applied based on the pixel color.
This file contains 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
##### PIXEL ART IMPORT ##### | |
''' | |
Import Pixel Art image and turn it into pixel 3D cubes, each colored based on the pixel sample grid | |
''' | |
import bpy | |
D = bpy.data | |
# Specify your image here, open it inside Blender Image Editor panel | |
image_file = 'suzanne_pixel.png' | |
img = D.images[image_file] | |
pixels = list(img.pixels) | |
grouped_list = [pixels[ipx:ipx+4] for ipx in range(0, len(pixels), 4)] | |
# Get width and height of image (in pixel) | |
w = width = img.size[0] | |
h = height = img.size[1] | |
print("------LENGTH------") | |
print( len(img.pixels)//4) | |
print("------PIXEL------") | |
print(pixels) | |
print("---GROUPED-PIXEL---") | |
print(len(grouped_list)) | |
# Create LIST of GRID XY value | |
rowColList=[] | |
for i in range(height): | |
for j in range(width): | |
rowColList.append(i) | |
rowColList.append(j) | |
# SPLIT the LIST of XY | |
origList = rowColList | |
splitList = [origList[i:i+2] for i in range(0,len(origList),2)] | |
# Create a single material that respect Vertex Color | |
mat = bpy.data.materials.new('PixelVertexColorMat') | |
mat.use_vertex_color_paint = True | |
mat.use_vertex_color_light = True | |
for number in range(len(grouped_list)): | |
# separate RGBA into each own variables | |
r = grouped_list[number][0] | |
g = grouped_list[number][1] | |
b = grouped_list[number][2] | |
a = grouped_list[number][3] | |
# separate XY coordinate | |
x=splitList[number][0] | |
y=splitList[number][1] | |
# create cube at location XY | |
bpy.ops.mesh.primitive_cube_add(location=(x-width/2, y-width/2, 0)) | |
selectedObject = bpy.context.selected_objects | |
selectedObject[0].name = 'pixelcube.%s' % number | |
selectedObject[0].scale = 0.45, 0.45, 0.45 | |
mesh = selectedObject[0] | |
bpy.context.object.data.materials.append(mat) | |
# ADD VERTEX COLOR SNIPPET | |
# Create Vertex Color Layer for each selected meshes | |
# bpy.context.selected_objects[0].data.vertex_colors | |
mesh.data.vertex_colors.new() | |
# Get Total Length of Vertex Colors for each selected meshes | |
# len(bpy.context.selected_objects[0].data.vertex_colors[0].data) | |
totalVertCol = len(mesh.data.vertex_colors[0].data) | |
# Iterate over every mesh vertex color and give it a single colour | |
for i in range(totalVertCol): | |
mesh.data.vertex_colors[0].data[i].color = r, g, b | |
# Print the progress | |
print ('PixelCube {number} created at:'.format(number=number), x, y, ', with RGB color', r, g, b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment