-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
facemap_select.py
307 lines (245 loc) · 9.14 KB
/
facemap_select.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
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# facemap_select, (c) 2023 Michel Anders (varkenvarken) and contributors.
#
# 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 #####
# we cannot select faces from a defined facemap (yet), see
# https://projects.blender.org/blender/blender/issues/105317
# therefore I created this tiny add-on.
# It adds two options to the Select menu in mesh edit mode:
# From facemap and Create facemap.
bl_info = {
"name": "FacemapSelect",
"author": "Michel Anders (varkenvarken) with contribution from Andrew Leichter (aleichter) and Tyo79",
"version": (0, 0, 20241107144005),
"blender": (4, 0, 0),
"location": "Edit mode 3d-view, Select- -> From facemap | Create facemap",
"description": "Select faces based on the active boolean facemap or create a new facemap",
"warning": "",
"wiki_url": "https://github.com/varkenvarken/blenderaddons",
"category": "Mesh",
}
import bpy
from bpy.types import Panel, UIList
class FacemapCreate(bpy.types.Operator):
bl_idname = "mesh.facemap_create"
bl_label = "FacemapCreate"
bl_description = (
"Create a new boolean face map and set value according to current selection"
)
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(self, context):
return context.mode == "EDIT_MESH" and context.active_object.type == "MESH"
def execute(self, context):
bpy.ops.object.editmode_toggle()
facemap = context.active_object.data.attributes.new(
name="FaceMap", domain="FACE", type="BOOLEAN"
)
for polygon in context.active_object.data.polygons:
facemap.data[polygon.index].value = polygon.select
bpy.ops.object.editmode_toggle()
attrs = context.object.data.attributes
attrs.active = facemap
return {"FINISHED"}
class FacemapAssign(bpy.types.Operator):
bl_idname = "mesh.facemap_assign"
bl_label = "FacemapAssign"
bl_description = "Assign or remove faces from the active facemap"
param: bpy.props.StringProperty()
@classmethod
def poll(self, context):
fm = context.object.data.attributes.active
return fm is not None and fm.domain == "FACE" and fm.data_type == "BOOLEAN"
def execute(self, context):
obj = context.object
facemap = context.object.data.attributes.active
attribute_name = facemap.name
bpy.ops.object.editmode_toggle()
if attribute_name in obj.data.attributes:
attribute = obj.data.attributes[attribute_name]
for poly in obj.data.polygons:
if poly.select:
if self.param == "Assign":
attribute.data[poly.index].value = True
if self.param == "Remove":
attribute.data[poly.index].value = False
bpy.ops.object.editmode_toggle()
return {"FINISHED"}
class FacemapSelections(bpy.types.Operator):
bl_idname = "mesh.facemap_selections"
bl_label = "Facemap Selections"
bl_description = "Select or deselect faces marked in the active facemap\nShift-Select replaces the selection"
param: bpy.props.StringProperty()
@classmethod
def poll(self, context):
fm = context.object.data.attributes.active
return fm is not None and fm.domain == "FACE" and fm.data_type == "BOOLEAN"
def execute(self, context):
obj = context.object
facemap = context.object.data.attributes.active
attribute_name = facemap.name
bpy.ops.object.editmode_toggle()
if attribute_name in obj.data.attributes:
attribute = obj.data.attributes[attribute_name]
if not self.__shift: # add to or remove from selection
for poly in obj.data.polygons:
# if poly.select:
if attribute.data[poly.index].value:
if self.param == "Select":
poly.select = True
if self.param == "Deselect":
poly.select = False
else: # set the selection (deselect act the same regardless whether shift is pressed)
for poly in obj.data.polygons:
if self.param == "Select":
poly.select = attribute.data[poly.index].value
if self.param == "Deselect" and attribute.data[poly.index].value:
poly.select = False
edge = bpy.context.object.data.edges
for i in edge:
i.select = False
bpy.ops.object.editmode_toggle()
return {"FINISHED"}
def invoke(self, context, event):
self.__shift = event.shift
return self.execute(context)
class FMS_OT_facemap_delete(bpy.types.Operator):
bl_idname = "fms.facemap_delete"
bl_label = "FacemapDelete"
bl_description = "Delete Active face map "
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(self, context):
fm = context.object.data.attributes.active
return fm is not None and fm.domain == "FACE" and fm.data_type == "BOOLEAN"
def execute(self, context):
bpy.ops.geometry.attribute_remove()
return {"FINISHED"}
class MESH_UL_fmaps(UIList):
def filter_items(self, context, data, propname):
items = getattr(data, propname)
flt_flags = [
(
self.bitflag_filter_item
if item.domain == "FACE"
and item.data_type == "BOOLEAN"
and not item.is_internal
and item.name != "sharp_face"
else 0
)
for item in items
]
return flt_flags, []
def draw_item(
self,
_context,
layout,
_data,
item,
icon,
_active_data,
_active_propname,
_index,
):
fmap = item
if self.layout_type in {"DEFAULT", "COMPACT"}:
layout.prop(fmap, "name", text="", emboss=False, icon="FACE_MAPS")
elif self.layout_type == "GRID":
layout.alignment = "CENTER"
layout.label(text="", icon_value=icon)
class DATA_PT_face_maps(Panel):
bl_label = "Face Maps"
bl_space_type = "PROPERTIES"
bl_context = "data"
bl_region_type = "WINDOW"
bl_options = {"DEFAULT_CLOSED"}
COMPAT_ENGINES = {
"BLENDER_RENDER",
"BLENDER_EEVEE",
"BLENDER_WORKBENCH",
"BLENDER_WORKBENCH_NEXT",
}
@classmethod
def poll(cls, context):
obj = context.object
return obj and obj.type == "MESH"
def draw(self, context):
layout = self.layout
ob = context.object
mesh = context.mesh
attributes = context.object.data.attributes
face_maps = [
att
for att in attributes
if att.domain == "FACE"
and att.data_type == "BOOLEAN"
and not att.is_internal
and att.name != "sharp_face"
]
facemap = mesh.attributes.active
rows = 2
if facemap:
rows = 4
row = layout.row()
row.template_list(
"MESH_UL_fmaps",
"",
ob.data,
"attributes",
ob.data.attributes,
"active_index",
rows=rows,
)
col = row.column(align=True)
col.operator("mesh.facemap_create", icon="ADD", text="")
col.operator("fms.facemap_delete", icon="REMOVE", text="")
if len(face_maps) > 0 and (ob.mode == "EDIT" and ob.type == "MESH"):
row = layout.row()
sub = row.row(align=True)
sub.operator(
"mesh.facemap_assign",
text="Assign",
).param = "Assign"
sub.operator("mesh.facemap_assign", text="Remove").param = "Remove"
sub = row.row(align=True)
sub.operator("mesh.facemap_selections", text="Select").param = "Select"
sub.operator("mesh.facemap_selections", text="Deselect").param = "Deselect"
classes = [
FacemapCreate,
FMS_OT_facemap_delete,
FacemapAssign,
FacemapSelections,
MESH_UL_fmaps,
DATA_PT_face_maps,
]
classes = [
FacemapCreate,
FMS_OT_facemap_delete,
FacemapAssign,
FacemapSelections,
MESH_UL_fmaps,
DATA_PT_face_maps,
]
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()