-
Notifications
You must be signed in to change notification settings - Fork 63
/
DUV_UVTranslate.py
282 lines (229 loc) · 10.5 KB
/
DUV_UVTranslate.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
import bpy
import bmesh
import math
from mathutils import Vector
from . import DUV_Utils
class DREAMUV_OT_uv_translate(bpy.types.Operator):
"""Translate UVs in the 3D Viewport"""
bl_idname = "view3d.dreamuv_uvtranslate"
bl_label = "UV Translate"
bl_options = {"GRAB_CURSOR", "UNDO", "BLOCKING"}
first_mouse_x = None
first_mouse_y = None
first_value = None
mesh = None
bm = None
bm2 = None
bm_orig = None
shiftreset = False
delta = 0
xlock = False
ylock = False
stateswitch = False
mousetestx = False
constrainttest = False
pixel_steps = None
do_pixel_snap = False
move_snap = 4
def invoke(self, context, event):
self.shiftreset = False
self.xlock = False
self.ylock = False
self.constrainttest = False
self.stateswitch = False
self.mousetestx = False
self.pixel_steps = None
self.do_pixel_snap = False
self.move_snap = 0.25
# 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')
if context.object:
self.first_mouse_x = event.mouse_x
self.first_mouse_y = event.mouse_y
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)
self.bm_orig = bmesh.new()
self.bm_orig.from_mesh(self.mesh)
# have to do this for some reason
self.bm.faces.ensure_lookup_table()
self.bm2.faces.ensure_lookup_table()
self.bm_orig.faces.ensure_lookup_table()
# Get refrerence to addon preference to get snap and scale setting
module_name = __name__.split('.')[0]
addon_prefs = bpy.context.preferences.addons[module_name].preferences
self.do_pixel_snap = addon_prefs.pixel_snap
self.move_snap = addon_prefs.move_snap
self.move_snap = 1/self.move_snap
print(self.move_snap)
# Precalculate data before going into modal
self.pixel_steps = {}
for i, face in enumerate(self.bm.faces):
if face.select is False:
continue
# Find pixel steps per face here to look up in future translations
if self.do_pixel_snap:
pixel_step = DUV_Utils.get_face_pixel_step(context, face)
if pixel_step is not None:
self.pixel_steps[face.index] = pixel_step
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "No active object")
return {'CANCELLED'}
def modal(self, context, event):
#context.area.header_text_set(
# "DUV UVTranslate: X/Y - contrain along X/Y Axis, MMB drag - alternative axis contrain method, SHIFT - precision mode, CTRL - stepped mode, CTRL + SHIFT - stepped with smaller increments")
#context.area.tag_redraw()
# setup constraints first
if event.type == 'X':
self.stateswitch = True
self.xlock = False
self.ylock = True
if event.type == 'Y':
self.stateswitch = True
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 = False
self.ylock = True
else:
self.xlock = True
self.ylock = False
if mousetestx is not self.mousetestx:
self.stateswitch = True
self.mousetestx = not self.mousetestx
if self.stateswitch:
self.stateswitch = False
# reset to start editing from start position
for i, face in enumerate(self.bm.faces):
if face.select:
for o, vert in enumerate(face.loops):
reset_uv = self.bm2.faces[i].loops[o][self.bm2.loops.layers.uv.active].uv
vert[self.bm.loops.layers.uv.active].uv = reset_uv
if event.type == 'MOUSEMOVE':
self.delta = (
(self.first_mouse_x - event.mouse_x),
(self.first_mouse_y - event.mouse_y)
)
sensitivity = 0.001 if not self.do_pixel_snap else 0.1
self.delta = Vector(self.delta) * sensitivity
if self.do_pixel_snap:
self.delta.x = int(round(self.delta.x))
self.delta.y = int(round(self.delta.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
self.first_mouse_y = event.mouse_y
for i, face in enumerate(self.bm.faces):
if face.select:
for o, vert in enumerate(face.loops):
reset_uv = vert[self.bm.loops.layers.uv.active].uv
self.bm2.faces[i].loops[o][self.bm2.loops.layers.uv.active].uv = reset_uv
self.delta = (0, 0)
self.delta = Vector(self.delta)
else:
# reset origin position to shift into normal mode
if self.shiftreset:
self.shiftreset = False
self.first_mouse_x = event.mouse_x
self.first_mouse_y = event.mouse_y
for i, face in enumerate(self.bm.faces):
if face.select:
for o, vert in enumerate(face.loops):
reset_uv = vert[self.bm.loops.layers.uv.active].uv
self.bm2.faces[i].loops[o][self.bm2.loops.layers.uv.active].uv = reset_uv
self.delta = (0, 0)
self.delta = Vector(self.delta)
if event.ctrl and not event.shift:
self.delta.x = math.floor(self.delta.x * self.move_snap) / self.move_snap
self.delta.y = math.floor(self.delta.y * self.move_snap) / self.move_snap
if event.ctrl and event.shift:
self.delta.x = math.floor(self.delta.x * (self.move_snap*self.move_snap)) / (self.move_snap*self.move_snap)
self.delta.y = math.floor(self.delta.y * (self.move_snap*self.move_snap)) / (self.move_snap*self.move_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 is False:
continue
local_delta = self.delta.copy()
if self.do_pixel_snap and face.index in self.pixel_steps.keys():
pixel_step = self.pixel_steps[face.index]
local_delta.x *= pixel_step.x
local_delta.y *= pixel_step.y
uv_x_axis = Vector((1.0, 0.0))
uv_y_axis = Vector((0.0, 1.0))
if self.xlock:
uv_x_axis = Vector((0, 0))
if self.ylock:
uv_y_axis = Vector((0, 0))
for o, vert in enumerate(face.loops):
origin_uv = self.bm2.faces[i].loops[o][self.bm2.loops.layers.uv.active].uv
uv_offset = local_delta.x * uv_x_axis + local_delta.y * uv_y_axis
vert[self.bm.loops.layers.uv.active].uv = origin_uv + uv_offset
# update mesh
bmesh.update_edit_mesh(self.mesh, loop_triangles=False, destructive=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):
reset_uv = self.bm_orig.faces[i].loops[o][self.bm_orig.loops.layers.uv.active].uv
vert[self.bm.loops.layers.uv.active].uv = reset_uv
# update mesh
bmesh.update_edit_mesh(self.mesh, loop_triangles=False, destructive=False)
return {'CANCELLED'}
return {'RUNNING_MODAL'}
class DREAMUV_OT_uv_translate_step(bpy.types.Operator):
"""Move UVs using snap size"""
bl_idname = "view3d.dreamuv_uvtranslatestep"
bl_label = "UV Translate Step"
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
module_name = __name__.split('.')[0]
addon_prefs = bpy.context.preferences.addons[module_name].preferences
move_snap = addon_prefs.move_snap
xmove = 0
ymove = 0
if self.direction == "left":
xmove = move_snap
if self.direction == "right":
xmove = -move_snap
if self.direction == "up":
ymove = -move_snap
if self.direction == "down":
ymove = move_snap
for face in bm.faces:
if face.select:
for loop in face.loops:
loop[uv_layer].uv.x += xmove
loop[uv_layer].uv.y += ymove
#update mesh
bmesh.update_edit_mesh(mesh, loop_triangles=False, destructive=False)
return {'FINISHED'}