-
Notifications
You must be signed in to change notification settings - Fork 65
/
DUV_UVInset.py
284 lines (226 loc) · 10.3 KB
/
DUV_UVInset.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
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
import bpy
import bmesh
import math
class DREAMUV_OT_uv_inset(bpy.types.Operator):
"""Inset UVs in the 3D Viewport"""
bl_idname = "view3d.dreamuv_uvinset"
bl_label = "UV Inset"
bl_options = {"GRAB_CURSOR", "UNDO", "BLOCKING"}
first_mouse_x = None
first_value = None
mesh = None
bm = None
bm2 = None
xcenter=0
ycenter=0
shiftreset = False
xlock=False
ylock=False
constrainttest = False
s1=3
s2=.5
move_snap = 2
def invoke(self, context, event):
#object->edit switch seems to "lock" the data. Ugly but hey it works
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')
self.shiftreset = False
self.xlock=False
self.ylock=False
self.constrainttest = False
self.scale_snap = 2
module_name = __name__.split('.')[0]
addon_prefs = bpy.context.preferences.addons[module_name].preferences
self.scale_snap = addon_prefs.scale_snap
if context.object:
self.first_mouse_x = event.mouse_x+1000/self.s1
self.first_mouse_y = event.mouse_y+1000/self.s1
self.mesh = bpy.context.object.data
self.bm = bmesh.from_edit_mesh(self.mesh)
#save original for reference
self.bm2 = bmesh.new()
self.bm2.from_mesh(self.mesh)
#have to do this for some reason
self.bm.faces.ensure_lookup_table()
self.bm2.faces.ensure_lookup_table()
#find "center"
#loop through every selected face and move the uv's using original uv as reference
xmin=0
xmax=0
ymin=0
ymax=0
first = True
for i,face in enumerate(self.bm.faces):
if face.select:
for o,vert in enumerate(face.loops):
if first:
xmin=vert[self.bm.loops.layers.uv.active].uv.x
xmax=vert[self.bm.loops.layers.uv.active].uv.x
ymin=vert[self.bm.loops.layers.uv.active].uv.y
ymax=vert[self.bm.loops.layers.uv.active].uv.y
first=False
else:
if vert[self.bm.loops.layers.uv.active].uv.x < xmin:
xmin=vert[self.bm.loops.layers.uv.active].uv.x
elif vert[self.bm.loops.layers.uv.active].uv.x > xmax:
xmax=vert[self.bm.loops.layers.uv.active].uv.x
if vert[self.bm.loops.layers.uv.active].uv.y < ymin:
ymin=vert[self.bm.loops.layers.uv.active].uv.y
elif vert[self.bm.loops.layers.uv.active].uv.y > ymax:
ymax=vert[self.bm.loops.layers.uv.active].uv.y
self.xcenter=(xmin+xmax)/2
self.ycenter=(ymin+ymax)/2
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "No active object")
return {'CANCELLED'}
def modal(self, context, event):
if event.type == 'X':
self.xlock=False
self.ylock=True
if event.type == 'Y':
self.xlock=True
self.ylock=False
#test is middle mouse held down
if event.type == 'MIDDLEMOUSE' and event.value == 'PRESS':
self.constrainttest = True
if event.type == 'MIDDLEMOUSE' and event.value == 'RELEASE':
self.constrainttest = False
#test if mouse is in the right quadrant for X or Y movement
if self.constrainttest:
mouseangle=math.atan2(event.mouse_y-self.first_mouse_y,event.mouse_x-self.first_mouse_x)
mousetestx=False
if (mouseangle < 0.785 and mouseangle > -0.785) or (mouseangle > 2.355 or mouseangle < -2.355):
mousetestx=True
if mousetestx:
self.xlock=True
self.ylock=False
else:
self.xlock=False
self.ylock=True
if event.type == 'MOUSEMOVE':
deltax = self.first_mouse_x - event.mouse_x
deltay = self.first_mouse_y - event.mouse_y
if event.shift and not event.ctrl:
#self.delta*=.1
#reset origin position to shift into precision mode
if not self.shiftreset:
self.shiftreset=True
self.first_mouse_x = event.mouse_x+1000/self.s2
self.first_mouse_y = event.mouse_y+1000/self.s2
for i,face in enumerate(self.bm.faces):
if face.select:
for o,vert in enumerate(face.loops):
self.bm2.faces[i].loops[o][self.bm2.loops.layers.uv.active].uv = vert[self.bm.loops.layers.uv.active].uv
deltax = self.first_mouse_x - event.mouse_x
deltay = self.first_mouse_y - event.mouse_y
deltax*=0.001*self.s2
deltay*=0.001*self.s2
else:
#reset origin position to shift into normal mode
if self.shiftreset:
self.shiftreset=False
self.first_mouse_x = event.mouse_x+1000/self.s1
self.first_mouse_y = event.mouse_y+1000/self.s1
for i,face in enumerate(self.bm.faces):
if face.select:
for o,vert in enumerate(face.loops):
self.bm2.faces[i].loops[o][self.bm2.loops.layers.uv.active].uv = vert[self.bm.loops.layers.uv.active].uv
deltax = self.first_mouse_x - event.mouse_x
deltay = self.first_mouse_y - event.mouse_y
deltax*=0.001*self.s1
deltay*=0.001*self.s1
if not self.xlock and not self.ylock:
delta=(deltax+deltay)*.5
deltax=delta
deltay=delta
if self.xlock:
deltax=1
if self.ylock:
deltay=1
if event.ctrl and not event.shift:
deltax=math.floor(deltax*self.scale_snap)/self.scale_snap
deltay=math.floor(deltay*self.scale_snap)/self.scale_snap
if event.ctrl and event.shift:
deltax=math.floor(deltax*self.scale_snap*self.scale_snap)/(self.scale_snap*self.scale_snap)
deltay=math.floor(deltay*self.scale_snap*self.scale_snap)/(self.scale_snap*self.scale_snap)
#loop through every selected face and move the uv's using original uv as reference
for i,face in enumerate(self.bm.faces):
if face.select:
for o,vert in enumerate(face.loops):
vert[self.bm.loops.layers.uv.active].uv.x=((deltax)*self.bm2.faces[i].loops[o][self.bm2.loops.layers.uv.active].uv.x)+((1-(deltax))*self.xcenter)
vert[self.bm.loops.layers.uv.active].uv.y=((deltay)*self.bm2.faces[i].loops[o][self.bm2.loops.layers.uv.active].uv.y)+((1-(deltay))*self.ycenter)
#update mesh
bmesh.update_edit_mesh(self.mesh, False, False)
elif event.type == 'LEFTMOUSE':
#finish up and make sure changes are locked in place
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')
return {'FINISHED'}
elif event.type in {'RIGHTMOUSE', 'ESC'}:
#reset all uvs to reference
for i,face in enumerate(self.bm.faces):
if face.select:
for o,vert in enumerate(face.loops):
vert[self.bm.loops.layers.uv.active].uv = self.bm2.faces[i].loops[o][self.bm2.loops.layers.uv.active].uv
#update mesh
bmesh.update_edit_mesh(self.mesh, loop_triangles=False, destructive=False)
return {'CANCELLED'}
return {'RUNNING_MODAL'}
class DREAMUV_OT_uv_inset_step(bpy.types.Operator):
"""Inset UVs using pixel size"""
bl_idname = "view3d.dreamuv_uvinsetstep"
bl_label = "inset"
bl_options = {"UNDO"}
direction : bpy.props.StringProperty()
def execute(self, context):
mesh = bpy.context.object.data
bm = bmesh.from_edit_mesh(mesh)
bm.faces.ensure_lookup_table()
uv_layer = bm.loops.layers.uv.active
faces = list()
#MAKE FACE LIST
for face in bm.faces:
if face.select:
faces.append(face)
#get original size
xmin, xmax = faces[0].loops[0][uv_layer].uv.x, faces[0].loops[0][uv_layer].uv.x
ymin, ymax = faces[0].loops[0][uv_layer].uv.y, faces[0].loops[0][uv_layer].uv.y
for face in faces:
for vert in face.loops:
xmin = min(xmin, vert[uv_layer].uv.x)
xmax = max(xmax, vert[uv_layer].uv.x)
ymin = min(ymin, vert[uv_layer].uv.y)
ymax = max(ymax, vert[uv_layer].uv.y)
print(xmin)
print(xmax)
print(ymin)
print(ymax)
for face in faces:
for loop in face.loops:
loop[uv_layer].uv.x-= xmin
loop[uv_layer].uv.y -= ymin
loop[uv_layer].uv.x /= (xmax-xmin)
loop[uv_layer].uv.y /= (ymax-ymin)
pixel_inset = context.scene.uvinsetpixels/context.scene.uvinsettexsize
if self.direction == "in":
xmin += pixel_inset
xmax -= pixel_inset
ymin += pixel_inset
ymax -= pixel_inset
if self.direction == "out":
xmin -= pixel_inset
xmax += pixel_inset
ymin -= pixel_inset
ymax += pixel_inset
#move into new quad
for face in faces:
for loop in face.loops:
loop[uv_layer].uv.x *= xmax-xmin
loop[uv_layer].uv.y *= ymax-ymin
loop[uv_layer].uv.x += xmin
loop[uv_layer].uv.y += ymin
#update mesh
bmesh.update_edit_mesh(mesh, loop_triangles=False, destructive=False)
return {'FINISHED'}