Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mesh_selection_topokit.py « mesh_select_tools « mesh_extra_tools - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 232714c45ec419a57c2f1edbd856a0246decf51e (plain)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

bl_info = {
    "name": "Topokit 2",
    "author": "dustractor",
    "version": (2, 0),
    "blender": (2, 60, 0),
    "location": "edit mesh vertices/edges/faces menus",
    "description": "",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Mesh"}


import bpy
from bpy.types import Operator
# In between calls, this stores any data that is expensive or static,
# matched to the size of the mesh and the id of the operator that created it
cachedata = dict()
# tkey is moved to mesh_extra_tools\__init__.py register function


# just a mix-in for the operators...
class meshpoller:
    @classmethod
    def poll(self, context):
        try:
            assert context.active_object.type == "MESH"
        except:
            return False
        finally:
            return True


# BEGIN VERTICES SECTION

# This one works similarly to normal 'grow' (ctrl + NUMPAD_PLUS),
# except the original selection is not part of the result,
#
#   0--0--0         0--1--0
#   |  |  |         |  |  |
#   0--1--0  -->    1--0--1
#   |  |  |         |  |  |
#   0--0--0         0--1--0

class MESH_OT_vneighbors_edgewise(meshpoller, Operator):
    bl_idname = "mesh.v2v_by_edge"
    bl_label = "Neighbors by Edge"
    bl_description = ("Select neighbour vertices of a starting selected vertex\n"
                      "Similar to Grow Selection - apart from the\n"
                      "original selection is not part of the result")
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        global cachedata

        bpy.ops.object.mode_set(mode="OBJECT")
        obj = context.active_object
        mesh = obj.data
        meshkey = (len(mesh.vertices), len(mesh.edges), len(mesh.polygons), id(self))
        next_state = bytearray(meshkey[0])

        if (meshkey == obj.tkkey) and (meshkey in cachedata):
            vert_to_vert_map, prev_state = cachedata[meshkey]
        else:
            vert_to_vert_map = {i: {} for i in range(meshkey[0])}
            for a, b in mesh.edge_keys:
                vert_to_vert_map[a][b] = 1
                vert_to_vert_map[b][a] = 1
            obj.tkkey = meshkey
            prev_state = None

        if not prev_state:
            selected_vert_indices = filter(lambda _: mesh.vertices[_].select,
                                           range(len(mesh.vertices)))
        else:
            selected_vert_indices = filter(
                                    lambda _: mesh.vertices[_].select and not prev_state[_],
                                    range(len(mesh.vertices))
                                    )

        for v in selected_vert_indices:
            for neighbor_index in vert_to_vert_map[v]:
                next_state[neighbor_index] = True
        mesh.vertices.foreach_set("select", next_state)
        cachedata[meshkey] = (vert_to_vert_map, next_state)
        bpy.ops.object.mode_set(mode="EDIT")

        return {"FINISHED"}


# This one is an alternate / counterpart to the previous.
# Think: diagonal opposite corners of a quad
# NOTE: does not apply to a triangle, since verts have no "opposite"
#
#   0--0--0     1--0--1
#   |  |  |     |  |  |
#   0--1--0 --> 0--0--0
#   |  |  |     |  |  |
#   0--0--0     1--0--1

class MESH_OT_vneighbors_facewise(meshpoller, Operator):
    bl_idname = "mesh.v2v_facewise"
    bl_label = "Neighbors by Face - Edge"
    bl_description = ("Select diagonal opposite vertices of neighbour quads\n"
                      "Does not work with triangles\n"
                      "The original selection is not part of the result")
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        global cachedata

        bpy.ops.object.mode_set(mode="OBJECT")
        obj = context.active_object
        mesh = obj.data
        meshkey = (len(mesh.vertices), len(mesh.edges), len(mesh.polygons), id(self))
        next_state = bytearray(meshkey[0])

        if (meshkey == obj.tkkey) and (meshkey in cachedata):
            vert_to_vert_map = cachedata[meshkey]
        else:
            vert_to_vert_map = {i: {} for i in range(meshkey[0])}
            for a, b in mesh.edge_keys:
                vert_to_vert_map[a][b] = 1
                vert_to_vert_map[b][a] = 1
            obj.tkkey = meshkey
        faces = filter(lambda face: (len(face.vertices) == 4) and
                       (face.select is False), mesh.polygons)
        for f in faces:
            has = False
            t = set()
            for v in f.vertices:
                if mesh.vertices[v].select:
                    has = True
                    t.update(vert_to_vert_map[v])
            if has:
                for v in f.vertices:
                    if not mesh.vertices[v].select:
                        if v not in t:
                            next_state[v] = 1
        mesh.vertices.foreach_set("select", next_state)
        cachedata[meshkey] = vert_to_vert_map
        bpy.ops.object.mode_set(mode="EDIT")

        return {"FINISHED"}

# END VERTICES SECTION


# BEGIN EDGES SECTION
#   +--0--+--0--+--0--+          +--0--+--0--+--0--+
#   |     |     |     |          |     |     |     |
#   0     0     0     0          0     1     1     0
#   |     |     |     |          |     |     |     |
#   +--0--+--1--+--0--+   --->   +--0--+--0--+--0--+
#   |     |     |     |          |     |     |     |
#   0     0     0     0          0     1     1     0
#   |     |     |     |          |     |     |     |
#   +--0--+--0--+--0--+          +--0--+--0--+--0--+

class MESH_OT_eneighbors_shared_v_f(meshpoller, Operator):
    bl_idname = "mesh.e2e_evfe"
    bl_label = "Neighbors by Vert and Face"
    bl_description = ("Select edges that share the neighbour vertices and faces\n"
                      "of the starting selected edge\n"
                      "The original selection is not part of the result")
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        global cachedata

        bpy.ops.object.mode_set(mode="OBJECT")
        obj = context.active_object
        mesh = obj.data
        meshkey = (len(mesh.vertices), len(mesh.edges), len(mesh.polygons), id(self))
        state_mask = bytearray(meshkey[1])

        if (meshkey == obj.tkkey) and (meshkey in cachedata):
            edge_to_edges_dict = cachedata
        else:
            edge_key_to_index = {k: i for i, k in enumerate(mesh.edge_keys)}
            edge_to_edges_dict = {i: set() for i in range(len(mesh.edges))}
            for f in mesh.polygons:
                fed = [edge_key_to_index[k] for k in f.edge_keys]
                for k in f.edge_keys:
                    edge_to_edges_dict[edge_key_to_index[k]].update(fed)
            obj.tkkey = meshkey

        for e in filter(lambda _: mesh.edges[_].select, edge_to_edges_dict):
            k1 = set(mesh.edges[e].key)
            for n in edge_to_edges_dict[e]:
                k2 = set(mesh.edges[n].key)
                if not k1.isdisjoint(k2):
                    state_mask[n] = True

        for e in mesh.edges:
            e.select ^= state_mask[e.index]
        cachedata[meshkey] = edge_key_to_index
        bpy.ops.object.mode_set(mode="EDIT")

        return {"FINISHED"}


#   +--0--+--0--+--0--+          +--0--+--0--+--0--+
#   |     |     |     |          |     |     |     |
#   0     0     0     0          0     1     1     0
#   |     |     |     |          |     |     |     |
#   +--0--+--1--+--0--+   --->   +--1--+--0--+--1--+
#   |     |     |     |          |     |     |     |
#   0     0     0     0          0     1     1     0
#   |     |     |     |          |     |     |     |
#   +--0--+--0--+--0--+          +--0--+--0--+--0--+

class MESH_OT_eneighbors_shared_v(meshpoller, Operator):
    bl_idname = "mesh.e2e_eve"
    bl_label = "Neighbors by Vert"
    bl_description = ("Select edges that share the neighbour vertices\n"
                     "of the starting selected edge\n"
                     "The original selection is not part of the result")
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        bpy.ops.object.mode_set(mode="OBJECT")
        mesh = context.active_object.data
        state_mask = bytearray(len(mesh.edges))

        for e in mesh.edges:
            state_mask[e.index] = mesh.vertices[e.vertices[0]].select ^ mesh.vertices[e.vertices[1]].select
        mesh.edges.foreach_set('select', state_mask)
        bpy.ops.object.mode_set(mode="EDIT")

        return {"FINISHED"}


#   +--0--+--0--+--0--+          +--0--+--1--+--0--+
#   |     |     |     |          |     |     |     |
#   0     0     0     0          0     1     1     0
#   |     |     |     |          |     |     |     |
#   +--0--+--1--+--0--+   --->   +--0--+--0--+--0--+
#   |     |     |     |          |     |     |     |
#   0     0     0     0          0     1     1     0
#   |     |     |     |          |     |     |     |
#   +--0--+--0--+--0--+          +--0--+--1--+--0--+

class MESH_OT_eneighbors_shared_f(meshpoller, Operator):
    bl_idname = "mesh.e2e_efe"
    bl_label = "Neighbors by Face"
    bl_description = ("Select edges of neighbour faces to the starting selected edge\n"
                     "The original selection is not part of the result")
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        global cachedata

        bpy.ops.object.mode_set(mode="OBJECT")
        obj = context.active_object
        mesh = obj.data
        meshkey = (len(mesh.vertices), len(mesh.edges), len(mesh.polygons), id(self))

        if (meshkey == obj.tkkey) and (meshkey in cachedata):
            edge_to_edges_dict = cachedata
        else:
            edge_key_to_index = {k: i for i, k in enumerate(mesh.edge_keys)}
            edge_to_edges_dict = {i: set() for i in range(len(mesh.edges))}
            for f in mesh.polygons:
                fed = [edge_key_to_index[k] for k in f.edge_keys]
                for k in f.edge_keys:
                    edge_to_edges_dict[edge_key_to_index[k]].update(fed)
            obj.tkkey = meshkey
        state_mask, esel = (bytearray(meshkey[1]), bytearray(meshkey[1]))
        mesh.edges.foreach_get('select', esel)

        for e in filter(lambda _: mesh.edges[_].select, range(meshkey[1])):
            for n in edge_to_edges_dict[e]:
                state_mask[n] = 1

        for e in range(meshkey[1]):
            esel[e] ^= state_mask[e]
        mesh.edges.foreach_set('select', esel)
        cachedata[meshkey] = edge_to_edges_dict
        bpy.ops.object.mode_set(mode="EDIT")

        return {"FINISHED"}


# Notice that on these next two, the original selection stays
#   +--0--+--0--+--0--+          +--0--+--1--+--0--+
#   |     |     |     |          |     |     |     |
#   0     0     0     0          0     0     0     0
#   |     |     |     |          |     |     |     |
#   +--0--+--1--+--0--+   --->   +--0--+--1--+--0--+
#   |     |     |     |          |     |     |     |
#   0     0     0     0          0     0     0     0
#   |     |     |     |          |     |     |     |
#   +--0--+--0--+--0--+          +--0--+--1--+--0--+

class MESH_OT_eneighbors_shared_f_notv(meshpoller, Operator):
    bl_idname = "mesh.e2e_efnve"
    bl_label = "Lateral Neighbors"
    bl_description = ("Select edges that are lateral neighbours\n"
                      "The original selection is included in the result")
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        global cachedata

        bpy.ops.object.mode_set(mode="OBJECT")
        obj = context.active_object
        mesh = obj.data
        meshkey = (len(mesh.vertices), len(mesh.edges), len(mesh.polygons), id(self))
        state_mask = bytearray(meshkey[1])

        if (meshkey == obj.tkkey) and (meshkey in cachedata):
            edge_to_face_map, edge_key_to_index = cachedata[meshkey]
        else:
            edge_key_to_index = {}
            edge_to_face_map = {i: set() for i in range(meshkey[1])}
            for i, k in enumerate(mesh. edge_keys):
                edge_key_to_index[k] = i

            for f in mesh.polygons:
                for k in f.edge_keys:
                    edge_to_face_map[edge_key_to_index[k]].add(f.index)
            obj.tkkey = meshkey
        selected_edge_indices = filter(lambda _: mesh.edges[_].select, range(meshkey[1]))

        for e in selected_edge_indices:
            for f in edge_to_face_map[e]:
                for k in mesh.polygons[f].edge_keys:
                    hasv_in = False
                    for v in mesh.edges[e].key:
                        if v in k:
                            hasv_in = True
                    if hasv_in:
                        continue
                    else:
                        state_mask[edge_key_to_index[k]] = True

        for e in filter(lambda _: state_mask[_], range(meshkey[1])):
            mesh.edges[e].select |= state_mask[e]
        cachedata[meshkey] = (edge_to_face_map, edge_key_to_index)
        bpy.ops.object.mode_set(mode="EDIT")

        return {"FINISHED"}


#   +--0--+--0--+--0--+          +--0--+--0--+--0--+
#   |     |     |     |          |     |     |     |
#   0     0     0     0          0     0     0     0
#   |     |     |     |          |     |     |     |
#   +--0--+--1--+--0--+   --->   +--1--+--1--+--1--+
#   |     |     |     |          |     |     |     |
#   0     0     0     0          0     0     0     0
#   |     |     |     |          |     |     |     |
#   +--0--+--0--+--0--+          +--0--+--0--+--0--+

class MESH_OT_eneighbors_shared_v_notf(meshpoller, Operator):
    bl_idname = "mesh.e2e_evnfe"
    bl_label = "Longitudinal Edges"
    bl_description = ("Select Edges along the same longitude of the starting edge\n"
                      "The original selection is included in the result")
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        global cachedata

        bpy.ops.object.mode_set(mode="OBJECT")
        obj = context.active_object
        mesh = obj.data
        meshkey = (len(mesh.vertices), len(mesh.edges), len(mesh.polygons), id(self))
        state_mask = bytearray(meshkey[1])
        vstate = bytearray(meshkey[0])
        mesh.vertices.foreach_get('select', vstate)

        if (meshkey == obj.tkkey) and (meshkey in cachedata):
            edge_to_face_map, vert_to_vert_map, edge_key_to_index = cachedata[meshkey]
        else:
            edge_key_to_index = {}
            vert_to_vert_map = {i: set() for i in range(meshkey[0])}
            edge_to_face_map = {i: set() for i in range(meshkey[1])}

            for i, k in enumerate(mesh.edge_keys):
                edge_key_to_index[k] = i
                vert_to_vert_map[k[0]].add(k[1])
                vert_to_vert_map[k[1]].add(k[0])

            for f in mesh.polygons:
                for k in f.edge_keys:
                    edge_to_face_map[edge_key_to_index[k]].add(f.index)
            obj.tkkey = meshkey
        selected_edge_indices = filter(lambda _: mesh.edges[_].select, range(meshkey[1]))

        for e in selected_edge_indices:
            for v in mesh.edges[e].key:
                state_mask[v] ^= 1

            for f in edge_to_face_map[e]:
                for v in mesh.polygons[f].vertices:
                    vstate[v] = 1

        for v in filter(lambda _: state_mask[_], range(meshkey[1])):
            for n in vert_to_vert_map[v]:
                if not vstate[n] and (n != v):
                    mesh.edges[edge_key_to_index[(min(v, n), max(v, n))]].select = True
        cachedata[meshkey] = (edge_to_face_map, vert_to_vert_map, edge_key_to_index)
        bpy.ops.object.mode_set(mode="EDIT")

        return {"FINISHED"}


# Deselects edges which are at the edge of a face-selection,
# causing selection to 'shrink in'
class MESH_OT_inner_edges(meshpoller, Operator):
    bl_idname = "mesh.ie"
    bl_label = "Inner Edge Selection"
    bl_description = ("Deselects edges which are at the border\n"
                      "of a starting face selection\n"
                      "causing the selection to shrink inwards")
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        global cachedata

        bpy.ops.object.mode_set(mode="OBJECT")
        obj = context.active_object
        mesh = obj.data
        meshkey = (len(mesh.vertices), len(mesh.edges), len(mesh.polygons), id(self))
        state_mask = bytearray(meshkey[1])

        if (meshkey == obj.tkkey) and (meshkey in cachedata):
            edge_to_face_map = cachedata[meshkey]
        else:
            edge_key_to_index = {k: i for i, k in enumerate(mesh.edge_keys)}
            edge_to_face_map = {i: set() for i in range(meshkey[1])}
            for f in mesh.polygons:
                for k in f.edge_keys:
                    edge_to_face_map[edge_key_to_index[k]].add(f.index)
            obj.tkkey = meshkey

        for e in filter(lambda _: mesh.edges[_].select, range(meshkey[1])):
            for f in edge_to_face_map[e]:
                if mesh.polygons[f].select:
                    state_mask[e] ^= 1

        for e in range(meshkey[1]):
            mesh.edges[e].select ^= state_mask[e]
        cachedata[meshkey] = edge_to_face_map
        bpy.ops.object.mode_set(mode="EDIT")

        return {"FINISHED"}

# END EDGES SECTION


# BEGIN FACES SECTION

# here is another one which functions very similarly to the ctrl+NUMPAD_PLUS 'growth'
# but it deselects the original selection, of course.
# This would be your checkerboard-type growth.
#   [0][0][0]          [0][1][0]
#   [0][1][0]   --->   [1][0][1]
#   [0][0][0]          [0][1][0]

class MESH_OT_fneighbors_shared_e(meshpoller, Operator):
    bl_idname = "mesh.f2f_fef"
    bl_label = "Neighbor Faces sharing an Edge"
    bl_description = ("Selects faces that share an edge with the starting face selection\n"
                      "Similar to the Grow selection \n"
                      "The original selection is not part of the result")
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        global cachedata

        bpy.ops.object.mode_set(mode="OBJECT")
        obj = context.active_object
        mesh = obj.data
        meshkey = (len(mesh.vertices), len(mesh.edges), len(mesh.polygons), id(self))

        if (meshkey == obj.tkkey) and (meshkey in cachedata):
            face_to_face_map = cachedata[meshkey]
        else:
            edge_key_to_index = {k: i for i, k in enumerate(mesh.edge_keys)}
            edge_to_face_map = {i: set() for i in range(meshkey[1])}
            for f in mesh.polygons:
                for k in f.edge_keys:
                    edge_to_face_map[edge_key_to_index[k]].add(f.index)
            face_to_face_map = {i: set() for i in range(meshkey[2])}
            for f in mesh.polygons:
                for k in f.edge_keys:
                    face_to_face_map[f.index].update(edge_to_face_map[edge_key_to_index[k]])
            obj.tkkey = meshkey
        mask_state = bytearray(meshkey[2])

        for f in filter(lambda _: mesh.polygons[_].select, range(meshkey[2])):
            for n in face_to_face_map[f]:
                mask_state[n] = True

        for f in range(meshkey[2]):
            mesh.polygons[f].select ^= mask_state[f]
        cachedata[meshkey] = face_to_face_map
        bpy.ops.object.mode_set(mode="EDIT")

        return {"FINISHED"}


#   [0][0][0]          [1][0][1]
#   [0][1][0]   --->   [0][0][0]
#   [0][0][0]          [1][0][1]

class MESH_OT_fneighbors_shared_v_note(meshpoller, Operator):
    bl_idname = "mesh.f2f_fvnef"
    bl_label = "Neighbors by Vertex not Edge"
    bl_description = ("Select neighbour faces that share a vertex\n"
                      "with the starting selection\n"
                      "The original selection is not part of the result")
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        global cachedata

        bpy.ops.object.mode_set(mode="OBJECT")
        obj = context.active_object
        mesh = obj.data
        meshkey = (len(mesh.vertices), len(mesh.edges), len(mesh.polygons), id(self))

        if (meshkey == obj.tkkey) and (meshkey in cachedata):
            edge_key_to_index = cachedata[meshkey]
        else:
            edge_key_to_index = {k: i for i, k in enumerate(mesh.edge_keys)}
            obj.tkkey = meshkey
        state_mask = bytearray(meshkey[2])
        face_verts = set()

        for f in filter(lambda _: mesh.polygons[_].select, range(meshkey[2])):
            face_verts.update(mesh.polygons[f].vertices)

        for f in filter(lambda _: not mesh.polygons[_].select, range(meshkey[2])):
            ct = 0
            for v in mesh.polygons[f].vertices:
                ct += (v in face_verts)
            if ct == 1:
                state_mask[f] = 1
        mesh.polygons.foreach_set('select', state_mask)
        cachedata[meshkey] = edge_key_to_index
        bpy.ops.object.mode_set(mode="EDIT")

        return {"FINISHED"}


# https://en.wikipedia.org/wiki/Conway's_Game_of_Life
class MESH_OT_conway(meshpoller, Operator):
    bl_idname = "mesh.conway"
    bl_label = "Conway's Selection"
    bl_description = ("Select Faces with the Conway's game of life algorithm\n"
                      "Requires an initial Face selection\n"
                      "The edges of the original selection are included in the result")
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        global cachedata

        bpy.ops.object.mode_set(mode="OBJECT")
        obj = context.active_object
        mesh = obj.data
        meshkey = (len(mesh.vertices), len(mesh.edges), len(mesh.polygons), id(self))

        if (meshkey == obj.tkkey) and (meshkey in cachedata):
            vert_to_face_map = cachedata[meshkey]
        else:
            vert_to_face_map = {i: set() for i in range(meshkey[0])}
            for f in mesh.polygons:
                for v in f.vertices:
                    vert_to_face_map[v].add(f.index)
            obj.tkkey = meshkey
        sel = set()
        uns = set()
        F = {i: set() for i in range(meshkey[2])}

        for f in range(meshkey[2]):
            for v in mesh.polygons[f].vertices:
                for n in filter(lambda _: mesh.polygons[_].select and (_ != f), vert_to_face_map[v]):
                    F[f].add(n)

        for f in F:
            if len(F[f]) == 3:
                sel.add(f)
            elif len(F[f]) != 2:
                uns.add(f)

        for f in range(meshkey[2]):
            if f in sel:
                mesh.polygons[f].select = True
            if f in uns:
                mesh.polygons[f].select = False
        cachedata[meshkey] = vert_to_face_map
        bpy.ops.object.mode_set(mode="EDIT")

        return {"FINISHED"}


def register():
    bpy.utils.register_module(__name__)


def unregister():
    bpy.utils.unregister_module(__name__)


if __name__ == "__main__":
    register()