forked from drewcassidy/TexTools-Blender
-
Notifications
You must be signed in to change notification settings - Fork 1
/
op_island_rotate_90.py
81 lines (57 loc) · 2.18 KB
/
op_island_rotate_90.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
import bpy
import bmesh
import operator
import math
from mathutils import Vector
from collections import defaultdict
from math import pi
from . import utilities_uv
class op(bpy.types.Operator):
bl_idname = "uv.textools_island_rotate_90"
bl_label = "Rotate 90 degrees"
bl_description = "Rotate the selected UV island 90 degrees left or right"
bl_options = {'REGISTER', 'UNDO'}
angle : bpy.props.FloatProperty(name="Angle")
@classmethod
def poll(cls, context):
#Only in UV editor mode
if bpy.context.area.type != 'IMAGE_EDITOR':
return False
if not bpy.context.active_object:
return False
if bpy.context.active_object.type != 'MESH':
return False
#Only in Edit mode
if bpy.context.active_object.mode != 'EDIT':
return False
#Requires UV map
if not bpy.context.object.data.uv_layers:
return False
# Not in Synced mode
if bpy.context.scene.tool_settings.use_uv_select_sync:
return False
return True
def execute(self, context):
main(context, self.angle)
return {'FINISHED'}
def main(context, angle):
#Store selection
utilities_uv.selection_store()
bm = bmesh.from_edit_mesh(bpy.context.active_object.data)
uv_layers = bm.loops.layers.uv.verify()
bpy.ops.uv.select_linked()
#Bounds
bounds_initial = utilities_uv.getSelectionBBox()
bpy.ops.transform.rotate(value=angle, orient_axis='Z', constraint_axis=(False, False, False), use_proportional_edit=False)
#Align rotation to top left|right
bounds_post = utilities_uv.getSelectionBBox()
dy = bounds_post['max'].y - bounds_initial['max'].y
dx = 0
if angle > 0:
dx = bounds_post['max'].x - bounds_initial['max'].x
else:
dx = bounds_post['min'].x - bounds_initial['min'].x
bpy.ops.transform.translate(value=(-dx, -dy, 0), constraint_axis=(False, False, False), use_proportional_edit=False)
#Restore selection
utilities_uv.selection_restore()
bpy.utils.register_class(op)