-
Notifications
You must be signed in to change notification settings - Fork 1
/
image_as_mesh.py
132 lines (95 loc) · 3.11 KB
/
image_as_mesh.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
import bpy
import bmesh
from itertools import *
from io_image_as_mesh.rdp import rdp
from io_image_as_mesh.marching_squares import create_polygon
def create_mesh_from_image(img):
pixels = img.pixels
data = []
# creates an array with info, if pixels are transparent
# loop takes every 4th value from pixels, which is the
# alpha value
for alpha in islice(pixels, 3, None, 4):
if alpha == 0:
data.append(0)
else:
data.append(1)
# split array into rows
w = img.size[0]
data = [data[n:n+w] for n in range(0, len(data), w)]
# add border of empty pixels
for row in data:
row.insert(0, 0)
row.append(0)
empty_row = [0] * (w + 2)
data.insert(0, empty_row)
data.append(empty_row)
poly = rdp(create_polygon(data), 1.5)
obj = create_sprite(poly, img)
return obj
def create_sprite(poly, img):
w = img.size[0]
h = img.size[1]
scene = bpy.context.scene
points = []
edges = []
for i, p in enumerate(poly):
points.append([p[0] / w - 0.5, 0, p[1] / h - 0.5])
if i < len(poly) - 2:
edges.append((i, i + 1))
else:
edges.append((i, 0))
mesh_data = bpy.data.meshes.new("sprite_mesh")
mesh_data.from_pydata(points, edges, [])
mesh_data.update()
obj = bpy.data.objects.new(img.name, mesh_data)
scene.collection.objects.link(obj)
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bm = bmesh.from_edit_mesh(obj.data)
bm.verts.index_update()
bmesh.ops.triangle_fill(
bm, edges=bm.edges[:], use_dissolve=False, use_beauty=True)
if hasattr(bm.verts, "ensure_lookup_table"):
bm.verts.ensure_lookup_table()
uv_lay = bm.loops.layers.uv.new("UVMap")
for face in bm.faces:
for loop in face.loops:
uv = loop[uv_lay].uv
index = loop.vert.index
v = bm.verts[index].co
uv.x = v.x + 0.5
uv.y = v.z + 0.5
# scale image to size in inches
dpi_x = img.resolution[0] / 39.3701
dpi_y = img.resolution[1] / 39.3701
for vert in bm.verts:
v = vert.co
v.x *= w / dpi_x
v.z *= h / dpi_y
vert.co = v
bmesh.update_edit_mesh(obj.data)
bpy.ops.object.mode_set(mode='OBJECT')
mat = create_blender_material(obj, img)
create_cycles_material(mat, img)
return obj
def create_blender_material(obj, img):
tex = bpy.data.textures.new('ColorTex', type='IMAGE')
tex.image = img
mat = bpy.data.materials.new(name="Material")
obj.data.materials.append(mat)
return mat
def create_cycles_material(mat, img):
mat.use_nodes = True
nodes = mat.node_tree.nodes
links = mat.node_tree.links
for n in nodes:
nodes.remove(n)
tex = nodes.new("ShaderNodeTexImage")
tex.image = img
diff = nodes.new("ShaderNodeBsdfDiffuse")
out = nodes.new('ShaderNodeOutputMaterial')
links.new(tex.outputs[0], diff.inputs[0])
links.new(diff.outputs[0], out.inputs[0])