forked from franMarz/TexTools-Blender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_unwrap_edge_peel.py
95 lines (71 loc) · 2.66 KB
/
op_unwrap_edge_peel.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
import bpy
import os
import bmesh
from mathutils import Vector
from collections import defaultdict
from math import pi
from . import utilities_uv
from . import utilities_ui
class op(bpy.types.Operator):
bl_idname = "uv.textools_unwrap_edge_peel"
bl_label = "Peel Edge"
bl_description = "Unwrap pipe along selected edges"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
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
# Need view Face mode
if tuple(bpy.context.scene.tool_settings.mesh_select_mode)[1] == False:
return False
return True
def execute(self, context):
unwrap_edges_pipe(self, context)
return {'FINISHED'}
def unwrap_edges_pipe(self, context):
bm = bmesh.from_edit_mesh(bpy.context.active_object.data)
uv_layers = bm.loops.layers.uv.verify()
contextViewUV = utilities_ui.GetContextViewUV()
if not contextViewUV:
self.report({'ERROR_INVALID_INPUT'}, "This tool requires an available UV/Image view.")
return
# selected_initial = [edge for edge in bm.edges if edge.select]
selected_edges = []
selected_faces = []
# Extend loop selection
bpy.ops.mesh.loop_multi_select(ring=False)
selected_edges = [edge for edge in bm.edges if edge.select]
if len(selected_edges) == 0:
self.report({'ERROR_INVALID_INPUT'}, "No edges selected in the view" )
return
# Convert linked selection to single UV island
bpy.ops.mesh.select_linked(delimit=set())
bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='FACE')
bpy.ops.uv.textools_unwrap_faces_iron()
selected_faces = [face for face in bm.faces if face.select]
if len(selected_faces) == 0:
self.report({'ERROR_INVALID_INPUT'}, "No faces available" )
return
# Mark previous selected edges as Seam
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='EDGE')
for edge in selected_edges:
edge.select = True
bpy.ops.mesh.mark_seam(clear=False)
# Follow active quad unwrap for faces
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='FACE')
for face in selected_faces:
face.select = True
bm.faces.active = selected_faces[0]
bpy.ops.uv.unwrap(method='ANGLE_BASED', margin=0.0226216)
bpy.ops.uv.select_all(action='SELECT')
bpy.ops.uv.textools_rectify(contextViewUV)
# TODO: Restore initial selection
bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='EDGE')
bpy.utils.register_class(op)