-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection.py
165 lines (111 loc) · 3.88 KB
/
selection.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
def get_selected_vert_sequences(verts, ensure_seq_len=False, debug=False):
sequences = []
noncyclicstartverts = [v for v in verts if len([e for e in v.link_edges if e.select]) == 1]
if noncyclicstartverts:
v = noncyclicstartverts[0]
else:
v = verts[0]
seq = []
while verts:
seq.append(v)
if v not in verts:
break
else:
verts.remove(v)
if v in noncyclicstartverts:
noncyclicstartverts.remove(v)
nextv = [e.other_vert(v) for e in v.link_edges if e.select and e.other_vert(v) not in seq]
if nextv:
v = nextv[0]
else:
cyclic = True if len([e for e in v.link_edges if e.select]) == 2 else False
sequences.append((seq, cyclic))
if verts:
if noncyclicstartverts:
v = noncyclicstartverts[0]
else:
v = verts[0]
seq = []
if ensure_seq_len:
seqs = []
for seq, cyclic in sequences:
if len(seq) > 1:
seqs.append((seq, cyclic))
sequences = seqs
if debug:
for seq, cyclic in sequences:
print(cyclic, [v.index for v in seq])
return sequences
def get_edges_vert_sequences(verts, edges, debug=False):
sequences = []
noncyclicstartverts = [v for v in verts if len([e for e in v.link_edges if e in edges]) == 1]
if noncyclicstartverts:
v = noncyclicstartverts[0]
else:
v = verts[0]
seq = []
while verts:
seq.append(v)
verts.remove(v)
if v in noncyclicstartverts:
noncyclicstartverts.remove(v)
nextv = [e.other_vert(v) for e in v.link_edges if e in edges and e.other_vert(v) not in seq]
if nextv:
v = nextv[0]
else:
cyclic = True if len([e for e in v.link_edges if e in edges]) == 2 else False
sequences.append((seq, cyclic))
if verts:
if noncyclicstartverts:
v = noncyclicstartverts[0]
else:
v = verts[0]
seq = []
if debug:
for verts, cyclic in sequences:
print(cyclic, [v.index for v in verts])
return sequences
def get_selection_islands(faces, debug=False):
if debug:
print("selected:", [f.index for f in faces])
face_islands = []
while faces:
island = [faces[0]]
foundmore = [faces[0]]
if debug:
print("island:", [f.index for f in island])
print("foundmore:", [f.index for f in foundmore])
while foundmore:
for e in foundmore[0].edges:
bf = [f for f in e.link_faces if f.select and f not in island]
if bf:
island.append(bf[0])
foundmore.append(bf[0])
if debug:
print("popping", foundmore[0].index)
foundmore.pop(0)
face_islands.append(island)
for f in island:
faces.remove(f)
if debug:
print()
for idx, island in enumerate(face_islands):
print("island:", idx)
print(" » ", ", ".join([str(f.index) for f in island]))
islands = []
for fi in face_islands:
vi = set()
ei = set()
for f in fi:
vi.update(f.verts)
ei.update(f.edges)
islands.append((list(vi), list(ei), fi))
return sorted(islands, key=lambda x: len(x[2]), reverse=True)
def get_boundary_edges(faces, region_to_loop=False):
boundary_edges = [e for f in faces for e in f.edges if (not e.is_manifold) or (any(not f.select for f in e.link_faces))]
if region_to_loop:
for f in faces:
f.select_set(False)
for e in boundary_edges:
e.select_set(True)
return boundary_edges