UI: Move Extensions repositories popover to header
[blender-addons-contrib.git] / mesh_selectbuffer.py
blobefe7f110e7519c25cd45f612f72dd4650244ef7d
1 # ##### BEGIN GPL LICENSE BLOCK #####
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.
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.
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.
17 # ##### END GPL LICENSE BLOCK #####
19 import bpy
20 from bpy.types import Panel
21 from bpy.props import StringProperty
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, 1),
29 "blender": (2, 80, 0),
30 "location": "View3D > Properties",
31 "warning": "",
32 "doc_url": "https://github.com/kostex/blenderscripts/",
33 "tracker_url": "https://developer.blender.org/maniphest/task/edit/form/2/",
34 "category": "Mesh"}
37 class Oldbuffer:
38 data = []
41 class KTXSELECTBUFFER_OT_Mutate(bpy.types.Operator):
42 bl_label = "select buffer mutate"
43 bl_idname = "ktxselectbuffer.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")
49 operation : StringProperty()
51 def execute(self, context):
52 old_buffer = bpy.context.scene.ktx_selectbuffer
53 emode = bpy.context.tool_settings.mesh_select_mode
55 c_mode = bpy.context.object.mode
56 bpy.ops.object.mode_set(mode='OBJECT')
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
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)
89 bpy.ops.ed.undo_push()
91 return {'FINISHED'}
94 class KTXSELECTBUFFER_PT_Panel(bpy.types.Panel):
95 bl_label = "KTX Selectbuffer"
96 bl_idname = "KTXSELECTBUFFER_PT_Panel"
97 bl_space_type = 'VIEW_3D'
98 bl_region_type = 'UI'
99 bl_category = 'Edit'
100 bl_context = "mesh_edit"
101 bl_options = {'DEFAULT_CLOSED'}
103 def draw(self, context):
104 obj = bpy.context.object
105 layout = self.layout
106 row = layout.row()
107 col = row.column()
108 if obj == None:
109 col.label(text='Select/Create something first')
110 else:
111 if obj.type == 'MESH':
112 c_mode = bpy.context.object.mode
113 if c_mode == 'EDIT':
114 col.operator("ktxselectbuffer.mutate", text="Set").operation = 'set'
115 col.operator("ktxselectbuffer.mutate", text="Clear").operation = 'clear'
116 col.operator("ktxselectbuffer.mutate", text="Union").operation = 'union'
117 col.operator("ktxselectbuffer.mutate", text="Difference").operation = 'difference'
118 col.operator("ktxselectbuffer.mutate", text="Symmetric Difference").operation = 'sym_difference'
119 col.operator("ktxselectbuffer.mutate", text="Intersection").operation = 'intersection'
120 else:
121 col.label(text='Enter EDIT Mode to use')
122 else:
123 col.label(text='Select a Mesh Object')
126 classes = (
127 KTXSELECTBUFFER_OT_Mutate,
128 KTXSELECTBUFFER_PT_Panel
132 def register():
133 from bpy.utils import register_class
135 bpy.types.Scene.ktx_selectbuffer = Oldbuffer
137 for cls in classes:
138 register_class(cls)
141 def unregister():
142 from bpy.utils import unregister_class
144 del bpy.types.Scene.ktx_selectbuffer
146 for cls in classes:
147 unregister_class(cls)
150 if __name__ == "__main__":
151 register()