addons-contrib: more view_layer syntax updates
[blender-addons-contrib.git] / mesh_selectbuffer.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 import bpy
20 from bpy.types import Panel
21 from bpy.props import StringProperty
22
23
24 bl_info = {
25     "name": "KTX Selectbuffer",
26     "description": "Enable boolean operations on selections",
27     "author": "Roel Koster, @koelooptiemanna, irc:kostex",
28     "version": (1, 4, 0),
29     "blender": (2, 80, 0),
30     "location": "View3D > Properties",
31     "warning": "",
32     "wiki_url": "https://github.com/kostex/blenderscripts/",
33     "tracker_url": "https://developer.blender.org/maniphest/task/edit/form/2/",
34     "category": "3D View"}
35
36
37 class Oldbuffer:
38     data = []
39
40
41 class KTX_Selectbuffer_Mutate(bpy.types.Operator):
42     bl_label = "select buffer mutate"
43     bl_idname = "ktx.selectbuffer_mutate"
44     bl_description = ("A.union(B) elements from both A and B\n"
45                       "A.difference(B) elements in A but not in B\n"
46                       "A.symmetric_difference(B) elements in either A or B but not both\n"
47                       "A.intersection(B) elements common to A and B")
48
49     operation : StringProperty()
50
51     def execute(self, context):
52         old_buffer = bpy.context.scene.ktx_selectbuffer
53         emode = bpy.context.tool_settings.mesh_select_mode
54
55         c_mode = bpy.context.object.mode
56         bpy.ops.object.mode_set(mode='OBJECT')
57
58         if emode[0]:
59             all_vefs = bpy.context.object.data.vertices
60         elif emode[1]:
61             all_vefs = bpy.context.object.data.edges
62         elif emode[2]:
63             all_vefs = bpy.context.object.data.polygons
64
65         selected_vefs = [vef for vef in all_vefs if vef.select]
66         selected_vefs_buffer = []
67         for vef in selected_vefs:
68             selected_vefs_buffer.append(vef.index)
69         if self.operation == 'union':
70             resulting_vefs = set(old_buffer.data).union(selected_vefs_buffer)
71         elif self.operation == 'difference':
72             resulting_vefs = set(old_buffer.data).difference(selected_vefs_buffer)
73         elif self.operation == 'sym_difference':
74             resulting_vefs = set(old_buffer.data).symmetric_difference(selected_vefs_buffer)
75         elif self.operation == 'intersection':
76             resulting_vefs = set(old_buffer.data).intersection(selected_vefs_buffer)
77         elif self.operation == 'set':
78             resulting_vefs = selected_vefs_buffer
79         elif self.operation == 'clear':
80             resulting_vefs = []
81         old_buffer.data = resulting_vefs
82         bpy.ops.object.mode_set(mode='EDIT')
83         bpy.ops.mesh.select_all(action='DESELECT')
84         bpy.ops.object.mode_set(mode='OBJECT')
85         for vef in resulting_vefs:
86             all_vefs[vef].select = True
87         bpy.ops.object.mode_set(mode=c_mode)
88
89         bpy.ops.ed.undo_push()
90
91         return {'FINISHED'}
92
93
94 class KTX_Selectbuffer(bpy.types.Panel):
95     bl_label = "KTX Selectbuffer"
96     bl_idname = "ktx.selectbuffer"
97     bl_space_type = 'VIEW_3D'
98     bl_region_type = 'UI'
99
100     def draw(self, context):
101         obj = bpy.context.object
102         layout = self.layout
103         row = layout.row()
104         col = row.column()
105         if obj == None:
106             col.label(text='Select/Create something first')
107         else:
108             if obj.type == 'MESH':
109                 c_mode = bpy.context.object.mode
110                 if c_mode == 'EDIT':
111                     col.operator("ktx.selectbuffer_mutate", text="Set").operation = 'set'
112                     col.operator("ktx.selectbuffer_mutate", text="Clear").operation = 'clear'
113                     col.operator("ktx.selectbuffer_mutate", text="Union").operation = 'union'
114                     col.operator("ktx.selectbuffer_mutate", text="Difference").operation = 'difference'
115                     col.operator("ktx.selectbuffer_mutate", text="Symmetric Difference").operation = 'sym_difference'
116                     col.operator("ktx.selectbuffer_mutate", text="Intersection").operation = 'intersection'
117                 else:
118                     col.label(text='Enter EDIT Mode to use')
119             else:
120                 col.label(text='Select a Mesh Object')
121
122
123 classes = (
124     KTX_Selectbuffer,
125     KTX_Selectbuffer_Mutate
126 )
127
128
129 def register():
130     from bpy.utils import register_class
131
132     bpy.types.Scene.ktx_selectbuffer = Oldbuffer
133
134     for cls in classes:
135         register_class(cls)
136
137
138 def unregister():
139     from bpy.utils import unregister_class
140
141     del bpy.types.Scene.ktx_selectbuffer
142
143     for cls in classes:
144         unregister_class(cls)
145
146
147 if __name__ == "__main__":
148     register()