-
Notifications
You must be signed in to change notification settings - Fork 40
/
io_import_pixelart.py
214 lines (172 loc) · 6.1 KB
/
io_import_pixelart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# coding: UTF-8
bl_info = {
"name": "Import Pixel Art",
"author": "Mathias Panzenböck",
"version": (1, 0, 4),
"blender": (2, 80, 0),
"location": "File > Import > Pixel Art",
"description": "Imports pixel art images, creating colored cubes for each pixel.",
"wiki_url": "https://github.com/panzi/blender-addon-import-pixelart/blob/master/README.md",
"tracker_url": "https://github.com/panzi/blender-addon-import-pixelart/issues",
"category": "Import-Export"
}
import os.path
import bpy
from bpy_extras.io_utils import ImportHelper
from bpy.props import StringProperty, BoolProperty
from bpy.types import Operator
PARENT_NAME = '{filename}'
MATERIAL_NAME = 'pixel_art_{color}'
CUBE_NAME = '{filename}_{x}_{y}'
MESH_NAME = '{filename}_{x}_{y}_mesh'
def read_pixel_art(context, filepath: str,
use_nodes:bool=True,
reuse_materials:bool=False,
material_name:str=MATERIAL_NAME,
cube_name:str=CUBE_NAME,
mesh_name:str=MESH_NAME,
parent_name:str=PARENT_NAME):
cube_verts = [
(0, 0, 0), # 0
(1, 0, 0), # 1
(1, 1, 0), # 2
(0, 1, 0), # 3
(0, 0, 1), # 4
(1, 0, 1), # 5
(1, 1, 1), # 6
(0, 1, 1), # 7
]
cube_edges = []
cube_faces = [
(0, 1, 2, 3),
(0, 1, 5, 4),
(1, 2, 6, 5),
(4, 5, 6, 7),
(2, 3, 7, 6),
(0, 3, 7, 4),
]
struse_nodes = 'nodes' if use_nodes else ''
filename = os.path.split(filepath)[1]
image = bpy.data.images.load(filepath)
try:
channels = image.channels
if channels not in (1, 3, 4):
raise IOError(f"Cannot handle image with {channels} channels!")
params = dict(filename=filename, use_nodes=struse_nodes)
parent = bpy.data.objects.new(name=parent_name.format(**params), object_data=None)
collection = bpy.context.collection
collection.objects.link(parent)
materials = {}
width, height = image.size
pixels = image.pixels
a = 1.0
for y in range(height):
offset = y * channels * width
for x in range(width):
if channels == 1:
r = g = b = image.pixels[offset + x]
elif channels == 3:
index = offset + x * channels
r = pixels[index]
g = pixels[index + 1]
b = pixels[index + 2]
else:
index = offset + x * channels
r = pixels[index]
g = pixels[index + 1]
b = pixels[index + 2]
a = pixels[index + 3]
if a == 0:
continue
color = (r, g, b, a)
strcolor = '%02X%02X%02X%02X' % (int(r * 255), int(g * 255), int(b * 255), int(a * 255))
params = dict(filename=filename, color=strcolor, x=x, y=y, use_nodes=struse_nodes)
name = material_name.format(**params)
if color in materials:
material = materials[color]
elif reuse_materials and name in bpy.data.materials:
material = bpy.data.materials[name]
else:
material = materials[color] = bpy.data.materials.new(name=name)
material.diffuse_color = color
material.use_nodes = use_nodes
if use_nodes:
tree = material.node_tree
tree.nodes.clear()
diffuse_node = tree.nodes.new('ShaderNodeBsdfDiffuse')
diffuse_node.inputs[0].default_value = color
output_node = tree.nodes.new('ShaderNodeOutputMaterial')
if a < 1:
mix_node = tree.nodes.new('ShaderNodeMixShader')
mix_node.inputs[0].default_value = a
transparent_node = tree.nodes.new('ShaderNodeBsdfTransparent')
transparent_node.inputs[0].default_value = color
tree.links.new(diffuse_node.outputs[0], mix_node.inputs[1])
tree.links.new(transparent_node.outputs[0], mix_node.inputs[2])
tree.links.new(mix_node.outputs[0], output_node.inputs[0])
else:
tree.links.new(diffuse_node.outputs[0], output_node.inputs[0])
cube_mesh_name = mesh_name.format(**params)
mesh = bpy.data.meshes.new(cube_mesh_name)
mesh.from_pydata(cube_verts, cube_edges, cube_faces)
mesh.materials.append(material)
mesh.update()
cube_object_name = cube_name.format(**params)
obj = bpy.data.objects.new(name=cube_object_name, object_data=mesh)
collection.objects.link(obj)
obj.location = (x, y, 0)
obj.parent = parent
finally:
image.user_clear()
bpy.data.images.remove(image)
return {'FINISHED'}
class ImportPixelArt(Operator, ImportHelper):
"""Imports pixel art images, creating colored cubes for each pixel."""
bl_idname = "import_image.pixel_art"
bl_label = "Import Pixel Art"
bl_options = {'REGISTER', 'UNDO'}
filter_glob: StringProperty(default="*.png;*.gif;*.bmp", options={'HIDDEN'})
use_nodes: BoolProperty(default=True, name="Use material nodes")
reuse_materials: BoolProperty(default=False, name="Reuse existing materials with matching names")
parent_name: StringProperty(default=PARENT_NAME, name="Object Name")
cube_name: StringProperty(default=CUBE_NAME, name="Pixel Names")
mesh_name: StringProperty(default=MESH_NAME, name="Mesh Names")
material_name: StringProperty(default=MATERIAL_NAME, name="Material Names")
def execute(self, context):
# validate inputs
pix_params = dict(filename='', color='AABBCCDD', x=0, y=0, use_nodes='')
for name, value, params in [
('object name', self.parent_name, dict(filename='', use_nodes='')),
('material names', self.material_name, pix_params),
('mesh names', self.mesh_name, pix_params),
('pixel names', self.cube_name, pix_params),
]:
try:
value.format(**params)
except ValueError as e:
self.report({'ERROR'}, f"Format error in {name}: {e}")
return {'CANCELLED'}
except KeyError as e:
self.report({'ERROR'}, f"Illegal key used in {name}: {e}")
return {'CANCELLED'}
return read_pixel_art(context, self.filepath,
use_nodes=self.use_nodes,
reuse_materials=self.reuse_materials,
material_name=self.material_name,
cube_name=self.cube_name,
mesh_name=self.mesh_name,
parent_name=self.parent_name)
def menu_func_import(self, context):
self.layout.operator(ImportPixelArt.bl_idname, text="Import Pixel Art (.png/.gif/.bmp)")
def register():
bpy.utils.register_class(ImportPixelArt)
bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
def unregister():
bpy.utils.unregister_class(ImportPixelArt)
bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)
if __name__ == "__main__":
try:
unregister()
except:
pass
register()