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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
# <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__ = "Nutti <nutti.metro@gmail.com>"
__status__ = "production"
__version__ = "5.2"
__date__ = "17 Nov 2018"
import math
from math import atan2, cos, sqrt, sin, fabs
import bpy
from bpy.props import BoolProperty
import bmesh
from mathutils import Vector
from .. import common
from ..utils.bl_class_registry import BlClassRegistry
from ..utils.property_class_registry import PropertyClassRegistry
from ..utils import compatibility as compat
def _get_vco(verts_orig, loop):
"""
Get vertex original coordinate from loop
"""
for vo in verts_orig:
if vo["vidx"] == loop.vert.index and vo["moved"] is False:
return vo["vco"]
return loop.vert.co
def _get_link_loops(vert):
"""
Get loop linked to vertex
"""
link_loops = []
for f in vert.link_faces:
adj_loops = []
for loop in f.loops:
# self loop
if loop.vert == vert:
l = loop
# linked loop
else:
for e in loop.vert.link_edges:
if e.other_vert(loop.vert) == vert:
adj_loops.append(loop)
if len(adj_loops) < 2:
return None
link_loops.append({"l": l, "l0": adj_loops[0], "l1": adj_loops[1]})
return link_loops
def _get_ini_geom(link_loop, uv_layer, verts_orig, v_orig):
"""
Get initial geometory
(Get interior angle of face in vertex/UV space)
"""
u = link_loop["l"][uv_layer].uv
v0 = _get_vco(verts_orig, link_loop["l0"])
u0 = link_loop["l0"][uv_layer].uv
v1 = _get_vco(verts_orig, link_loop["l1"])
u1 = link_loop["l1"][uv_layer].uv
# get interior angle of face in vertex space
v0v1 = v1 - v0
v0v = v_orig["vco"] - v0
v1v = v_orig["vco"] - v1
theta0 = v0v1.angle(v0v)
theta1 = v0v1.angle(-v1v)
if (theta0 + theta1) > math.pi:
theta0 = v0v1.angle(-v0v)
theta1 = v0v1.angle(v1v)
# get interior angle of face in UV space
u0u1 = u1 - u0
u0u = u - u0
u1u = u - u1
phi0 = u0u1.angle(u0u)
phi1 = u0u1.angle(-u1u)
if (phi0 + phi1) > math.pi:
phi0 = u0u1.angle(-u0u)
phi1 = u0u1.angle(u1u)
# get direction of linked UV coordinate
# this will be used to judge whether angle is more or less than 180 degree
dir0 = u0u1.cross(u0u) > 0
dir1 = u0u1.cross(u1u) > 0
return {
"theta0": theta0,
"theta1": theta1,
"phi0": phi0,
"phi1": phi1,
"dir0": dir0,
"dir1": dir1}
def _get_target_uv(link_loop, uv_layer, verts_orig, v, ini_geom):
"""
Get target UV coordinate
"""
v0 = _get_vco(verts_orig, link_loop["l0"])
lo0 = link_loop["l0"]
v1 = _get_vco(verts_orig, link_loop["l1"])
lo1 = link_loop["l1"]
# get interior angle of face in vertex space
v0v1 = v1 - v0
v0v = v.co - v0
v1v = v.co - v1
theta0 = v0v1.angle(v0v)
theta1 = v0v1.angle(-v1v)
if (theta0 + theta1) > math.pi:
theta0 = v0v1.angle(-v0v)
theta1 = v0v1.angle(v1v)
# calculate target interior angle in UV space
phi0 = theta0 * ini_geom["phi0"] / ini_geom["theta0"]
phi1 = theta1 * ini_geom["phi1"] / ini_geom["theta1"]
uv0 = lo0[uv_layer].uv
uv1 = lo1[uv_layer].uv
# calculate target vertex coordinate from target interior angle
tuv0, tuv1 = _calc_tri_vert(uv0, uv1, phi0, phi1)
# target UV coordinate depends on direction, so judge using direction of
# linked UV coordinate
u0u1 = uv1 - uv0
u0u = tuv0 - uv0
u1u = tuv0 - uv1
dir0 = u0u1.cross(u0u) > 0
dir1 = u0u1.cross(u1u) > 0
if (ini_geom["dir0"] != dir0) or (ini_geom["dir1"] != dir1):
return tuv1
return tuv0
def _calc_tri_vert(v0, v1, angle0, angle1):
"""
Calculate rest coordinate from other coordinates and angle of end
"""
angle = math.pi - angle0 - angle1
alpha = atan2(v1.y - v0.y, v1.x - v0.x)
d = (v1.x - v0.x) / cos(alpha)
a = d * sin(angle0) / sin(angle)
b = d * sin(angle1) / sin(angle)
s = (a + b + d) / 2.0
if fabs(d) < 0.0000001:
xd = 0
yd = 0
else:
r = s * (s - a) * (s - b) * (s - d)
if r < 0:
xd = 0
yd = 0
else:
xd = (b * b - a * a + d * d) / (2 * d)
yd = 2 * sqrt(r) / d
x1 = xd * cos(alpha) - yd * sin(alpha) + v0.x
y1 = xd * sin(alpha) + yd * cos(alpha) + v0.y
x2 = xd * cos(alpha) + yd * sin(alpha) + v0.x
y2 = xd * sin(alpha) - yd * cos(alpha) + v0.y
return Vector((x1, y1)), Vector((x2, y2))
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
@PropertyClassRegistry()
class _Properties:
idname = "texture_lock"
@classmethod
def init_props(cls, scene):
class Props():
verts_orig = None
scene.muv_props.texture_lock = Props()
def get_func(_):
return MUV_OT_TextureLock_Intr.is_running(bpy.context)
def set_func(_, __):
pass
def update_func(_, __):
bpy.ops.uv.muv_texture_lock_operator_intr('INVOKE_REGION_WIN')
scene.muv_texture_lock_enabled = BoolProperty(
name="Texture Lock Enabled",
description="Texture Lock is enabled",
default=False
)
scene.muv_texture_lock_lock = BoolProperty(
name="Texture Lock Locked",
description="Texture Lock is locked",
default=False,
get=get_func,
set=set_func,
update=update_func
)
scene.muv_texture_lock_connect = BoolProperty(
name="Connect UV",
default=True
)
@classmethod
def del_props(cls, scene):
del scene.muv_props.texture_lock
del scene.muv_texture_lock_enabled
del scene.muv_texture_lock_lock
del scene.muv_texture_lock_connect
@BlClassRegistry()
class MUV_OT_TextureLock_Lock(bpy.types.Operator):
"""
Operation class: Lock Texture
"""
bl_idname = "uv.muv_texture_lock_operator_lock"
bl_label = "Lock Texture"
bl_description = "Lock Texture"
bl_options = {'REGISTER', 'UNDO'}
@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)
@classmethod
def is_ready(cls, context):
sc = context.scene
props = sc.muv_props.texture_lock
if props.verts_orig:
return True
return False
def execute(self, context):
props = context.scene.muv_props.texture_lock
obj = bpy.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()
if not bm.loops.layers.uv:
self.report({'WARNING'}, "Object must have more than one UV map")
return {'CANCELLED'}
props.verts_orig = [
{"vidx": v.index, "vco": v.co.copy(), "moved": False}
for v in bm.verts if v.select]
return {'FINISHED'}
@BlClassRegistry()
@compat.make_annotations
class MUV_OT_TextureLock_Unlock(bpy.types.Operator):
"""
Operation class: Unlock Texture
"""
bl_idname = "uv.muv_texture_lock_operator_unlock"
bl_label = "Unlock Texture"
bl_description = "Unlock Texture"
bl_options = {'REGISTER', 'UNDO'}
connect = BoolProperty(
name="Connect UV",
default=True
)
@classmethod
def poll(cls, context):
# we can not get area/space/region from console
if common.is_console_mode():
return True
sc = context.scene
props = sc.muv_props.texture_lock
if not props.verts_orig:
return False
if not MUV_OT_TextureLock_Lock.is_ready(context):
return False
if not _is_valid_context(context):
return False
return True
def execute(self, context):
sc = context.scene
props = sc.muv_props.texture_lock
obj = bpy.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()
if not bm.loops.layers.uv:
self.report({'WARNING'}, "Object must have more than one UV map")
return {'CANCELLED'}
uv_layer = bm.loops.layers.uv.verify()
verts = [v.index for v in bm.verts if v.select]
verts_orig = props.verts_orig
# move UV followed by vertex coordinate
for vidx, v_orig in zip(verts, verts_orig):
if vidx != v_orig["vidx"]:
self.report({'ERROR'}, "Internal Error")
return {"CANCELLED"}
v = bm.verts[vidx]
link_loops = _get_link_loops(v)
result = []
for ll in link_loops:
ini_geom = _get_ini_geom(ll, uv_layer, verts_orig, v_orig)
target_uv = _get_target_uv(
ll, uv_layer, verts_orig, v, ini_geom)
result.append({"l": ll["l"], "uv": target_uv})
# connect other face's UV
if self.connect:
ave = Vector((0.0, 0.0))
for r in result:
ave = ave + r["uv"]
ave = ave / len(result)
for r in result:
r["l"][uv_layer].uv = ave
else:
for r in result:
r["l"][uv_layer].uv = r["uv"]
v_orig["moved"] = True
bmesh.update_edit_mesh(obj.data)
props.verts_orig = None
return {'FINISHED'}
@BlClassRegistry()
class MUV_OT_TextureLock_Intr(bpy.types.Operator):
"""
Operation class: Texture Lock (Interactive mode)
"""
bl_idname = "uv.muv_texture_lock_operator_intr"
bl_label = "Texture Lock (Interactive mode)"
bl_description = "Internal operation for Texture Lock (Interactive mode)"
__timer = None
@classmethod
def poll(cls, context):
# we can not get area/space/region from console
if common.is_console_mode():
return False
return _is_valid_context(context)
@classmethod
def is_running(cls, _):
return 1 if cls.__timer else 0
@classmethod
def handle_add(cls, ops_obj, context):
if cls.__timer is None:
cls.__timer = context.window_manager.event_timer_add(
0.10, window=context.window)
context.window_manager.modal_handler_add(ops_obj)
@classmethod
def handle_remove(cls, context):
if cls.__timer is not None:
context.window_manager.event_timer_remove(cls.__timer)
cls.__timer = None
def __init__(self):
self.__intr_verts_orig = []
self.__intr_verts = []
def __sel_verts_changed(self, 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()
prev = set(self.__intr_verts)
now = set([v.index for v in bm.verts if v.select])
return prev != now
def __reinit_verts(self, 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()
self.__intr_verts_orig = [
{"vidx": v.index, "vco": v.co.copy(), "moved": False}
for v in bm.verts if v.select]
self.__intr_verts = [v.index for v in bm.verts if v.select]
def __update_uv(self, context):
"""
Update UV when vertex coordinates are changed
"""
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()
if not bm.loops.layers.uv:
self.report({'WARNING'}, "Object must have more than one UV map")
return
uv_layer = bm.loops.layers.uv.verify()
verts = [v.index for v in bm.verts if v.select]
verts_orig = self.__intr_verts_orig
for vidx, v_orig in zip(verts, verts_orig):
if vidx != v_orig["vidx"]:
self.report({'ERROR'}, "Internal Error")
return
v = bm.verts[vidx]
link_loops = _get_link_loops(v)
result = []
for ll in link_loops:
ini_geom = _get_ini_geom(ll, uv_layer, verts_orig, v_orig)
target_uv = _get_target_uv(
ll, uv_layer, verts_orig, v, ini_geom)
result.append({"l": ll["l"], "uv": target_uv})
# UV connect option is always true, because it raises
# unexpected behavior
ave = Vector((0.0, 0.0))
for r in result:
ave = ave + r["uv"]
ave = ave / len(result)
for r in result:
r["l"][uv_layer].uv = ave
v_orig["moved"] = True
bmesh.update_edit_mesh(obj.data)
common.redraw_all_areas()
self.__intr_verts_orig = [
{"vidx": v.index, "vco": v.co.copy(), "moved": False}
for v in bm.verts if v.select]
def modal(self, context, event):
if not _is_valid_context(context):
MUV_OT_TextureLock_Intr.handle_remove(context)
return {'FINISHED'}
if not MUV_OT_TextureLock_Intr.is_running(context):
return {'FINISHED'}
if context.area:
context.area.tag_redraw()
if event.type == 'TIMER':
if self.__sel_verts_changed(context):
self.__reinit_verts(context)
else:
self.__update_uv(context)
return {'PASS_THROUGH'}
def invoke(self, context, _):
if not _is_valid_context(context):
return {'CANCELLED'}
if not MUV_OT_TextureLock_Intr.is_running(context):
MUV_OT_TextureLock_Intr.handle_add(self, context)
return {'RUNNING_MODAL'}
else:
MUV_OT_TextureLock_Intr.handle_remove(context)
if context.area:
context.area.tag_redraw()
return {'FINISHED'}
|