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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
# <pep8-80 compliant>
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
__author__ = "McBuff, Nutti <nutti.metro@gmail.com>"
__status__ = "production"
__version__ = "5.2"
__date__ = "17 Nov 2018"
from math import sqrt
import bmesh
from mathutils import Vector
from .. import common
def _is_valid_context(context):
obj = context.object
# only edit mode is allowed to execute
if obj is None:
return False
if obj.type != 'MESH':
return False
if context.object.mode != 'EDIT':
return False
# only 'VIEW_3D' space is allowed to execute
for space in context.area.spaces:
if space.type == 'VIEW_3D':
break
else:
return False
return True
def _measure_wsuv_info(obj, tex_size=None):
mesh_area = common.measure_mesh_area(obj)
if common.check_version(2, 80, 0) >= 0:
uv_area = common.measure_uv_area(obj, tex_size)
else:
uv_area = common.measure_uv_area_legacy(obj, tex_size)
if not uv_area:
return None, mesh_area, None
if mesh_area == 0.0:
density = 0.0
else:
density = sqrt(uv_area) / sqrt(mesh_area)
return uv_area, mesh_area, density
def _apply(obj, origin, factor):
bm = bmesh.from_edit_mesh(obj.data)
if common.check_version(2, 73, 0) >= 0:
bm.verts.ensure_lookup_table()
bm.edges.ensure_lookup_table()
bm.faces.ensure_lookup_table()
sel_faces = [f for f in bm.faces if f.select]
uv_layer = bm.loops.layers.uv.verify()
# calculate origin
if origin == 'CENTER':
origin = Vector((0.0, 0.0))
num = 0
for f in sel_faces:
for l in f.loops:
uv = l[uv_layer].uv
origin = origin + uv
num = num + 1
origin = origin / num
elif origin == 'LEFT_TOP':
origin = Vector((100000.0, -100000.0))
for f in sel_faces:
for l in f.loops:
uv = l[uv_layer].uv
origin.x = min(origin.x, uv.x)
origin.y = max(origin.y, uv.y)
elif origin == 'LEFT_CENTER':
origin = Vector((100000.0, 0.0))
num = 0
for f in sel_faces:
for l in f.loops:
uv = l[uv_layer].uv
origin.x = min(origin.x, uv.x)
origin.y = origin.y + uv.y
num = num + 1
origin.y = origin.y / num
elif origin == 'LEFT_BOTTOM':
origin = Vector((100000.0, 100000.0))
for f in sel_faces:
for l in f.loops:
uv = l[uv_layer].uv
origin.x = min(origin.x, uv.x)
origin.y = min(origin.y, uv.y)
elif origin == 'CENTER_TOP':
origin = Vector((0.0, -100000.0))
num = 0
for f in sel_faces:
for l in f.loops:
uv = l[uv_layer].uv
origin.x = origin.x + uv.x
origin.y = max(origin.y, uv.y)
num = num + 1
origin.x = origin.x / num
elif origin == 'CENTER_BOTTOM':
origin = Vector((0.0, 100000.0))
num = 0
for f in sel_faces:
for l in f.loops:
uv = l[uv_layer].uv
origin.x = origin.x + uv.x
origin.y = min(origin.y, uv.y)
num = num + 1
origin.x = origin.x / num
elif origin == 'RIGHT_TOP':
origin = Vector((-100000.0, -100000.0))
for f in sel_faces:
for l in f.loops:
uv = l[uv_layer].uv
origin.x = max(origin.x, uv.x)
origin.y = max(origin.y, uv.y)
elif origin == 'RIGHT_CENTER':
origin = Vector((-100000.0, 0.0))
num = 0
for f in sel_faces:
for l in f.loops:
uv = l[uv_layer].uv
origin.x = max(origin.x, uv.x)
origin.y = origin.y + uv.y
num = num + 1
origin.y = origin.y / num
elif origin == 'RIGHT_BOTTOM':
origin = Vector((-100000.0, 100000.0))
for f in sel_faces:
for l in f.loops:
uv = l[uv_layer].uv
origin.x = max(origin.x, uv.x)
origin.y = min(origin.y, uv.y)
# update UV coordinate
for f in sel_faces:
for l in f.loops:
uv = l[uv_layer].uv
diff = uv - origin
l[uv_layer].uv = origin + diff * factor
bmesh.update_edit_mesh(obj.data)
class MeasureImpl:
@classmethod
def poll(cls, context):
# we can not get area/space/region from console
if common.is_console_mode():
return True
return _is_valid_context(context)
def execute(self, ops_obj, context):
sc = context.scene
obj = context.active_object
uv_area, mesh_area, density = _measure_wsuv_info(obj)
if not uv_area:
ops_obj.report({'WARNING'},
"Object must have more than one UV map and texture")
return {'CANCELLED'}
sc.muv_world_scale_uv_src_uv_area = uv_area
sc.muv_world_scale_uv_src_mesh_area = mesh_area
sc.muv_world_scale_uv_src_density = density
ops_obj.report({'INFO'},
"UV Area: {0}, Mesh Area: {1}, Texel Density: {2}"
.format(uv_area, mesh_area, density))
return {'FINISHED'}
class ApplyManualImpl:
@classmethod
def poll(cls, context):
# we can not get area/space/region from console
if common.is_console_mode():
return True
return _is_valid_context(context)
def __apply_manual(self, ops_obj, context):
obj = context.active_object
bm = bmesh.from_edit_mesh(obj.data)
if common.check_version(2, 73, 0) >= 0:
bm.verts.ensure_lookup_table()
bm.edges.ensure_lookup_table()
bm.faces.ensure_lookup_table()
tex_size = ops_obj.tgt_texture_size
uv_area, _, density = _measure_wsuv_info(obj, tex_size)
if not uv_area:
ops_obj.report({'WARNING'},
"Object must have more than one UV map")
return {'CANCELLED'}
tgt_density = ops_obj.tgt_density
factor = tgt_density / density
_apply(context.active_object, ops_obj.origin, factor)
ops_obj.report({'INFO'}, "Scaling factor: {0}".format(factor))
return {'FINISHED'}
def draw(self, ops_obj, _):
layout = ops_obj.layout
layout.prop(ops_obj, "tgt_density")
layout.prop(ops_obj, "tgt_texture_size")
layout.prop(ops_obj, "origin")
layout.separator()
def invoke(self, ops_obj, context, _):
if ops_obj.show_dialog:
wm = context.window_manager
return wm.invoke_props_dialog(ops_obj)
return ops_obj.execute(context)
def execute(self, ops_obj, context):
return self.__apply_manual(ops_obj, context)
class ApplyScalingDensityImpl:
@classmethod
def poll(cls, context):
# we can not get area/space/region from console
if common.is_console_mode():
return True
return _is_valid_context(context)
def __apply_scaling_density(self, ops_obj, context):
obj = context.active_object
bm = bmesh.from_edit_mesh(obj.data)
if common.check_version(2, 73, 0) >= 0:
bm.verts.ensure_lookup_table()
bm.edges.ensure_lookup_table()
bm.faces.ensure_lookup_table()
uv_area, _, density = _measure_wsuv_info(obj)
if not uv_area:
ops_obj.report({'WARNING'},
"Object must have more than one UV map and texture")
return {'CANCELLED'}
tgt_density = ops_obj.src_density * ops_obj.tgt_scaling_factor
factor = tgt_density / density
_apply(context.active_object, ops_obj.origin, factor)
ops_obj.report({'INFO'}, "Scaling factor: {0}".format(factor))
return {'FINISHED'}
def draw(self, ops_obj, _):
layout = ops_obj.layout
layout.label(text="Source:")
col = layout.column()
col.prop(ops_obj, "src_density")
col.enabled = False
layout.separator()
if not ops_obj.same_density:
layout.prop(ops_obj, "tgt_scaling_factor")
layout.prop(ops_obj, "origin")
layout.separator()
def invoke(self, ops_obj, context, _):
sc = context.scene
if ops_obj.show_dialog:
wm = context.window_manager
if ops_obj.same_density:
ops_obj.tgt_scaling_factor = 1.0
else:
ops_obj.tgt_scaling_factor = \
sc.muv_world_scale_uv_tgt_scaling_factor
ops_obj.src_density = sc.muv_world_scale_uv_src_density
return wm.invoke_props_dialog(ops_obj)
return ops_obj.execute(context)
def execute(self, ops_obj, context):
if ops_obj.same_density:
ops_obj.tgt_scaling_factor = 1.0
return self.__apply_scaling_density(ops_obj, context)
class ApplyProportionalToMeshImpl:
@classmethod
def poll(cls, context):
# we can not get area/space/region from console
if common.is_console_mode():
return True
return _is_valid_context(context)
def __apply_proportional_to_mesh(self, ops_obj, context):
obj = context.active_object
bm = bmesh.from_edit_mesh(obj.data)
if common.check_version(2, 73, 0) >= 0:
bm.verts.ensure_lookup_table()
bm.edges.ensure_lookup_table()
bm.faces.ensure_lookup_table()
uv_area, mesh_area, density = _measure_wsuv_info(obj)
if not uv_area:
ops_obj.report({'WARNING'},
"Object must have more than one UV map and texture")
return {'CANCELLED'}
tgt_density = ops_obj.src_density * sqrt(mesh_area) / sqrt(
ops_obj.src_mesh_area)
factor = tgt_density / density
_apply(context.active_object, ops_obj.origin, factor)
ops_obj.report({'INFO'}, "Scaling factor: {0}".format(factor))
return {'FINISHED'}
def draw(self, ops_obj, _):
layout = ops_obj.layout
layout.label(text="Source:")
col = layout.column(align=True)
col.prop(ops_obj, "src_density")
col.prop(ops_obj, "src_uv_area")
col.prop(ops_obj, "src_mesh_area")
col.enabled = False
layout.separator()
layout.prop(ops_obj, "origin")
layout.separator()
def invoke(self, ops_obj, context, _):
if ops_obj.show_dialog:
wm = context.window_manager
sc = context.scene
ops_obj.src_density = sc.muv_world_scale_uv_src_density
ops_obj.src_mesh_area = sc.muv_world_scale_uv_src_mesh_area
return wm.invoke_props_dialog(ops_obj)
return ops_obj.execute(context)
def execute(self, ops_obj, context):
return self.__apply_proportional_to_mesh(ops_obj, context)
|