-
Notifications
You must be signed in to change notification settings - Fork 65
/
DUV_UVMirror.py
62 lines (51 loc) · 2.14 KB
/
DUV_UVMirror.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
import bpy
import math
import bmesh
from mathutils import Vector
class DREAMUV_OT_uv_mirror(bpy.types.Operator):
"""Mirror UVs but retain uv edge positions"""
bl_idname = "view3d.dreamuv_uvmirror"
bl_label = "3D View UV Mirror"
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
#do this again
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)
#prevent divide by 0:
if (xmax - xmin) == 0:
xmin = .1
if (ymax - ymin) == 0:
ymin = .1
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)
if self.direction == "x":
loop[uv_layer].uv.x = -loop[uv_layer].uv.x + 1.0
if self.direction == "y":
loop[uv_layer].uv.y = -loop[uv_layer].uv.y + 1.0
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
bmesh.update_edit_mesh(mesh, loop_triangles=False, destructive=False)
return {'FINISHED'}