forked from leukbaars/DreamUV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DUV_UVTransfer.py
147 lines (113 loc) · 5.34 KB
/
DUV_UVTransfer.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
import bpy
import bmesh
from bpy.types import Menu
from bpy.props import EnumProperty, BoolProperty
class DREAMUV_OT_uv_transfer(bpy.types.Operator):
"""Transfer to selection"""
bl_idname = "view3d.dreamuv_uvtransfer"
bl_label = "UV Transfer"
bl_options = {"UNDO"}
def execute(self, context):
bpy.ops.uv.select_split()
obj = bpy.context.view_layer.objects.active
bm = bmesh.from_edit_mesh(obj.data)
uv_layer = bm.loops.layers.uv.verify()
faces = list()
xmin,xmax,ymin,ymax=0,0,0,0
selected_uv_loops = list()
for i,face in enumerate(bm.faces):
if face.select:
for o,vert in enumerate(face.loops):
if vert[bm.loops.layers.uv.active].select:
selected_uv_loops.append(vert)
#if nothing is selected, match UV selection to mesh selection
if len(selected_uv_loops) == 0:
for i,face in enumerate(bm.faces):
if face.select:
selected_uv_loops.extend(face.loops)
first = True
for vert in selected_uv_loops:
if first:
xmin=vert[bm.loops.layers.uv.active].uv.x
xmax=vert[bm.loops.layers.uv.active].uv.x
ymin=vert[bm.loops.layers.uv.active].uv.y
ymax=vert[bm.loops.layers.uv.active].uv.y
first=False
else:
if vert[bm.loops.layers.uv.active].uv.x < xmin:
xmin=vert[bm.loops.layers.uv.active].uv.x
elif vert[bm.loops.layers.uv.active].uv.x > xmax:
xmax=vert[bm.loops.layers.uv.active].uv.x
if vert[bm.loops.layers.uv.active].uv.y < ymin:
ymin=vert[bm.loops.layers.uv.active].uv.y
elif vert[bm.loops.layers.uv.active].uv.y > ymax:
ymax=vert[bm.loops.layers.uv.active].uv.y
aspect = (xmax-xmin)/(ymax-ymin)
aspecttarget = (context.scene.uvtransferxmax-context.scene.uvtransferxmin)/(context.scene.uvtransferymax-context.scene.uvtransferymin)
print("aspects")
print(aspect)
print(aspecttarget)
#move to 0,1
for vert in selected_uv_loops:
vert[bm.loops.layers.uv.active].uv.x -= xmin
vert[bm.loops.layers.uv.active].uv.y -= ymin
vert[bm.loops.layers.uv.active].uv.x /= (xmax-xmin)
vert[bm.loops.layers.uv.active].uv.y /= (ymax-ymin)
xmin2 = .5
ymin2 = .5
xmax2 = 1
ymax2 = 1
#move to new rect
for vert in selected_uv_loops:
vert[bm.loops.layers.uv.active].uv.x = (vert[bm.loops.layers.uv.active].uv.x * (context.scene.uvtransferxmax-context.scene.uvtransferxmin)) + context.scene.uvtransferxmin
vert[bm.loops.layers.uv.active].uv.y = (vert[bm.loops.layers.uv.active].uv.y * (context.scene.uvtransferymax-context.scene.uvtransferymin)) + context.scene.uvtransferymin
bmesh.update_edit_mesh(obj.data)
#cycle if needed:
if (aspect >= 1 and aspecttarget < 1) or (aspect <= 1 and aspecttarget > 1):
bpy.ops.view3d.dreamuv_uvcycle()
return {'FINISHED'}
class DREAMUV_OT_uv_transfer_grab(bpy.types.Operator):
"""UV Transfer Grab"""
bl_idname = "view3d.dreamuv_uvtransfergrab"
bl_label = "UV Transfer Grab"
bl_options = {"UNDO"}
def execute(self, context):
bpy.ops.uv.select_split()
obj = bpy.context.view_layer.objects.active
bm = bmesh.from_edit_mesh(obj.data)
uv_layer = bm.loops.layers.uv.verify()
faces = list()
xmin,xmax,ymin,ymax=0,0,0,0
selected_uv_loops = list()
for i,face in enumerate(bm.faces):
if face.select:
for o,vert in enumerate(face.loops):
if vert[bm.loops.layers.uv.active].select:
selected_uv_loops.append(vert)
#if nothing is selected, match UV selection to mesh selection
if len(selected_uv_loops) == 0:
for i,face in enumerate(bm.faces):
if face.select:
selected_uv_loops.extend(face.loops)
first = True
for vert in selected_uv_loops:
if first:
xmin=vert[bm.loops.layers.uv.active].uv.x
xmax=vert[bm.loops.layers.uv.active].uv.x
ymin=vert[bm.loops.layers.uv.active].uv.y
ymax=vert[bm.loops.layers.uv.active].uv.y
first=False
else:
if vert[bm.loops.layers.uv.active].uv.x < xmin:
xmin=vert[bm.loops.layers.uv.active].uv.x
elif vert[bm.loops.layers.uv.active].uv.x > xmax:
xmax=vert[bm.loops.layers.uv.active].uv.x
if vert[bm.loops.layers.uv.active].uv.y < ymin:
ymin=vert[bm.loops.layers.uv.active].uv.y
elif vert[bm.loops.layers.uv.active].uv.y > ymax:
ymax=vert[bm.loops.layers.uv.active].uv.y
context.scene.uvtransferxmax = xmax
context.scene.uvtransferxmin = xmin
context.scene.uvtransferymax = ymax
context.scene.uvtransferymin = ymin
return {'FINISHED'}