-
Notifications
You must be signed in to change notification settings - Fork 1
/
precise_uv_export.py
239 lines (176 loc) · 7.99 KB
/
precise_uv_export.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import bpy
from bpy.props import StringProperty, BoolProperty, IntVectorProperty
from bpy_extras.mesh_utils import mesh_linked_uv_islands
from mathutils.geometry import tessellate_polygon
from math import ceil, sqrt, isclose
bl_info = {
'name': 'Precise UV Export',
'description': 'Export pixel-perfect UV layouts as images',
'author': 'majik',
'version': (1, 4, 0),
'blender': (3, 0, 0),
'category': 'Import-Export'
}
# Precise UV export operator.
class ExportLayout(bpy.types.Operator):
"""Export pixel-perfect UV layouts as images"""
bl_idname = 'uv.export_precise_layout'
bl_label = 'Export Precise Layout'
bl_options = {'REGISTER', 'UNDO'}
filepath: StringProperty(subtype='FILE_PATH')
check_existing: BoolProperty(default=True, options={'HIDDEN'})
size: IntVectorProperty(size=2, min=2, max=8192, default=(16, 16), name='Image Size',
description='Dimensions of the exported layout image')
shade_islands: BoolProperty(default=True, name='Shade Islands',
description='Shade separate UV islands differently')
grid_overlay: BoolProperty(default=True, name='Grid Overlay',
description='Overlay a grid on the exported image')
outline_islands: BoolProperty(default=True, name='Outline Islands',
description='Draw an outline around every island')
@classmethod
def poll(cls, context):
mesh = context.active_object
return mesh is not None and mesh.type == 'MESH' and mesh.data.uv_layers
def invoke(self, context, event):
self.size = self.get_image_size(context, self.size)
self.filepath = context.active_object.name.replace('.', '_') + '.png'
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def check(self, context):
self.filepath = bpy.path.ensure_ext(self.filepath, '.png')
return True
def execute(self, context):
mesh = context.active_object
editing = (mesh.mode == 'EDIT')
if editing:
bpy.ops.object.mode_set(mode='OBJECT')
path = bpy.path.ensure_ext(self.filepath, '.png')
meshes = list(self.get_meshes(context))
triangles = list(self.get_triangles(meshes))
self.draw_layout(triangles)
self.save_layout(path)
if editing:
bpy.ops.object.mode_set(mode='EDIT')
return {'FINISHED'}
def draw_layout(self, triangles):
self.width, self.height = self.size
self.indices = [-1] * (self.width * self.height)
for triangle in triangles:
index = triangle.pop()
v1, v2, v3 = [(x * self.width, y * self.height) for x, y in triangle]
x_min, x_max = max(min(v1[0], v2[0], v3[0]), 0), min(max(v1[0], v2[0], v3[0]), self.width)
y_min, y_max = max(min(v1[1], v2[1], v3[1]), 0), min(max(v1[1], v2[1], v3[1]), self.height)
x_min = ceil(x_min) if isclose(x_min, ceil(x_min), rel_tol=1e-4) else int(x_min)
y_min = ceil(y_min) if isclose(y_min, ceil(y_min), rel_tol=1e-4) else int(y_min)
x_max = int(x_max) if isclose(x_max, int(x_max), rel_tol=1e-4) else ceil(x_max)
y_max = int(y_max) if isclose(y_max, int(y_max), rel_tol=1e-4) else ceil(y_max)
self.draw_triangle(*v1, *v2, *v3, x_min, x_max, y_min, y_max, index)
self.draw_line(*v1, *v2, x_min, x_max, y_min, y_max, index)
self.draw_line(*v2, *v3, x_min, x_max, y_min, y_max, index)
self.draw_line(*v3, *v1, x_min, x_max, y_min, y_max, index)
def save_layout(self, path):
pixels = [i for y in range(self.height) for x in range(self.width) for i in self.get_colour(x, y)]
try:
image = bpy.data.images.new('temp', self.width, self.height, alpha=True)
image.filepath = path
image.pixels = pixels
image.save()
bpy.data.images.remove(image)
except:
pass
def draw_triangle(self, x1, y1, x2, y2, x3, y3, x_min, x_max, y_min, y_max, index):
for x in range(x_min, x_max):
for y in range(y_min, y_max):
dist_a = (x - x2) * (y1 - y2) - (x1 - x2) * (y - y2)
dist_b = (x - x3) * (y2 - y3) - (x2 - x3) * (y - y3)
dist_c = (x - x1) * (y3 - y1) - (x3 - x1) * (y - y1)
negative = dist_a < 0 or dist_b < 0 or dist_c < 0
positive = dist_a > 0 or dist_b > 0 or dist_c > 0
if not (negative and positive):
self.indices[y * self.width + x] = index
def draw_line(self, x1, y1, x2, y2, x_min, x_max, y_min, y_max, index):
length = sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
x_dir, y_dir = (x2 - x1) / length, (y2 - y1) / length
x, y, dist = int(x1), int(y1), 0
x_delta = 1e6 if x_dir == 0 else abs(1 / x_dir)
y_delta = 1e6 if y_dir == 0 else abs(1 / y_dir)
if x_dir < 0:
x_step = -1
x_dist = (x1 - x) * x_delta
else:
x_step = 1
x_dist = (x - x1 + 1) * x_delta
if y_dir < 0:
y_step = -1
y_dist = (y1 - y) * y_delta
else:
y_step = 1
y_dist = (y - y1 + 1) * y_delta
while dist < length:
if x_min <= x < x_max and y_min <= y < y_max:
self.indices[y * self.width + x] = index
if x_dist < y_dist:
x_dist += x_delta
x += x_step
dist = x_dist - x_delta
else:
y_dist += y_delta
y += y_step
dist = y_dist - y_delta
def get_colour(self, x, y):
index = self.indices[y * self.width + x]
if index != -1:
value = 1
if self.shade_islands:
value -= (index % 6) * 0.1
if self.grid_overlay and (x + y) % 2 == 1:
value -= 0.04
return value, value, value, 1
if self.outline_islands:
for y_neighbour in range(max(y - 1, 0), min(y + 2, self.height)):
for x_neighbour in range(max(x - 1, 0), min(x + 2, self.width)):
if self.indices[y_neighbour * self.width + x_neighbour] != -1:
return 0, 0, 0, 1
return 0, 0, 0, 0
@staticmethod
def get_image_size(context, default):
width, height = default
if isinstance(context.space_data, bpy.types.SpaceImageEditor):
image = context.space_data.image
if image is not None:
image_w, image_h = image.size
if image_w and image_h:
width, height = image_w, image_h
return width, height
@staticmethod
def get_meshes(context):
for mesh in {*context.selected_objects, context.active_object}:
if mesh.type != 'MESH':
continue
if mesh.data.uv_layers.active is None:
continue
yield mesh.data
@staticmethod
def get_triangles(meshes):
for mesh in meshes:
layer = mesh.uv_layers.active.data
islands = mesh_linked_uv_islands(mesh)
for index, island in enumerate(islands):
for poly_index in island:
polygon = mesh.polygons[poly_index]
start = polygon.loop_start
end = start + polygon.loop_total
uvs = tuple(uv.uv for uv in layer[start:end])
for triangle in tessellate_polygon([uvs]):
yield [tuple(uvs[i]) for i in triangle] + [index]
# Register and unregister the add-on.
def menu_entry(self, context):
self.layout.operator(ExportLayout.bl_idname)
def register():
bpy.utils.register_class(ExportLayout)
bpy.types.IMAGE_MT_uvs.append(menu_entry)
def unregister():
bpy.utils.unregister_class(ExportLayout)
bpy.types.IMAGE_MT_uvs.remove(menu_entry)
if __name__ == '__main__':
register()