-
Notifications
You must be signed in to change notification settings - Fork 1
/
edge_sets.py
333 lines (251 loc) · 10.1 KB
/
edge_sets.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
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
import bpy
import bmesh
bl_info = {
"name": "Edge Sets",
"author": "Isaac Weaver",
"version": (0, 1),
"blender": (2, 78, 0),
"location": "Properties > Mesh > Edge Sets",
"description": "Create edge sets to easily manage edge creases and bevel weights.",
"warning": "",
"wiki_url": "",
"category": "Mesh",
}
# ------------------
# --- Utilities ----
# ------------------
class create_bmesh(object):
def __init__(self, mesh):
self.bm = bmesh.new()
self.mesh = mesh
def __enter__(self):
self.bm.from_mesh(self.mesh)
return self.bm
def __exit__(self, exc_type, exc_val, exc_tb):
self.bm.to_mesh(self.mesh)
self.bm.free()
class create_edit_bmesh(object):
def __init__(self, mesh):
self.bm = None
self.mesh = mesh
def __enter__(self):
self.bm = bmesh.from_edit_mesh(self.mesh)
return self.bm
def __exit__(self, exc_type, exc_val, exc_tb):
bmesh.update_edit_mesh(self.mesh)
def create_general_bmesh(context, mesh):
if context.mode == 'EDIT_MESH':
return create_edit_bmesh(mesh)
else:
return create_bmesh(mesh)
# ------------------
# --- Properties ---
# ------------------
def crease_update_callback(self, context):
mesh = self.id_data
index = mesh.edge_sets.values().index(self)
with create_general_bmesh(context, mesh) as bm:
name = 'edge_set'
if name in bm.edges.layers.int:
if len(bm.edges.layers.crease) > 0:
crease = bm.edges.layers.crease[0]
else:
crease = bm.edges.layers.crease.new()
edge_set = bm.edges.layers.int[name]
for edge in bm.edges:
if edge[edge_set] - 1 == index:
edge[crease] = self.crease
def bevel_update_callback(self, context):
mesh = self.id_data
index = mesh.edge_sets.values().index(self)
with create_general_bmesh(context, mesh) as bm:
name = 'edge_set'
if name in bm.edges.layers.int:
if len(bm.edges.layers.bevel_weight) > 0:
bevel = bm.edges.layers.bevel_weight[0]
else:
bevel = bm.edges.layers.bevel_weight.new()
edge_set = bm.edges.layers.int[name]
for edge in bm.edges:
if edge[edge_set] - 1 == index:
edge[bevel] = self.bevel
class EdgeSetItem(bpy.types.PropertyGroup):
crease = bpy.props.FloatProperty(
name="Crease",
subtype='FACTOR',
min=0, max=1,
update=crease_update_callback)
bevel = bpy.props.FloatProperty(
name="Bevel Weight",
subtype='FACTOR',
min=0, max=1,
update=bevel_update_callback)
# ------------------
# --- Operators ----
# ------------------
class AddEdgeSetOperator(bpy.types.Operator):
"""Add an edge set to the active mesh"""
bl_idname = "mesh.edge_set_add"
bl_label = "Add Edge Set"
@classmethod
def poll(cls, context):
return context.object and context.object.type == "MESH"
def execute(self, context):
mesh = context.object.data
mesh.edge_sets.add().name = "Edge Set"
mesh.active_edge_set_index = len(mesh.edge_sets) - 1
return {"FINISHED"}
class RemoveEdgeSetOperator(bpy.types.Operator):
"""Remove an edge set from the active mesh"""
bl_idname = "mesh.edge_set_remove"
bl_label = "Remove Edge Set"
@classmethod
def poll(cls, context):
return context.object and context.object.type == "MESH"
def execute(self, context):
mesh = context.object.data
mesh.edge_sets.remove(mesh.active_edge_set_index)
if mesh.active_edge_set_index > 0:
mesh.active_edge_set_index -= 1
return {"FINISHED"}
class AssignEdgeSetOperator(bpy.types.Operator):
"""Assign the selected edges to the active edge set"""
bl_idname = "mesh.edge_set_assign"
bl_label = "Assign"
@classmethod
def poll(cls, context):
return context.object and context.object.type == "MESH" and context.mode == "EDIT_MESH"
def execute(self, context):
mesh = context.object.data
name = 'edge_set'
with create_edit_bmesh(mesh) as bm:
if name not in bm.edges.layers.int:
edge_set = bm.edges.layers.int.new(name)
else:
edge_set = bm.edges.layers.int[name]
for edge in bm.edges:
if edge.select:
edge[edge_set] = mesh.active_edge_set_index + 1
return {"FINISHED"}
class RemoveFromEdgeSetOperator(bpy.types.Operator):
"""Remove the selected edges from the active edge set"""
bl_idname = "mesh.edge_set_remove_from"
bl_label = "Remove"
@classmethod
def poll(cls, context):
return context.object and context.object.type == "MESH" and context.mode == "EDIT_MESH"
def execute(self, context):
mesh = context.object.data
name = "edge_set"
with create_edit_bmesh(mesh) as bm:
if name in bm.edges.layers.int:
edge_set = bm.edges.layers.int[name]
for edge in bm.edges:
if edge.select:
edge[edge_set] = 0
return {"FINISHED"}
class SelectEdgeSetOperator(bpy.types.Operator):
"""Select the edges in the edge set"""
bl_idname = "mesh.edge_set_select"
bl_label = "Select"
@classmethod
def poll(cls, context):
return context.object and context.object.type == "MESH" and context.mode == "EDIT_MESH"
def execute(self, context):
mesh = context.object.data
name = "edge_set"
with create_edit_bmesh(mesh) as bm:
if name in bm.edges.layers.int:
edge_set = bm.edges.layers.int[name]
for edge in bm.edges:
if edge[edge_set] - 1 == mesh.active_edge_set_index:
edge.select = True
bm.select_flush_mode()
return {"FINISHED"}
class DeselectEdgeSetOperator(bpy.types.Operator):
"""Deselect the edges in the edge set"""
bl_idname = "mesh.edge_set_deselect"
bl_label = "Deselect"
@classmethod
def poll(cls, context):
return context.object and context.object.type == "MESH" and context.mode == "EDIT_MESH"
def execute(self, context):
mesh = context.object.data
name = "edge_set"
with create_edit_bmesh(mesh) as bm:
if name in bm.edges.layers.int:
edge_set = bm.edges.layers.int[name]
for edge in bm.edges:
if edge[edge_set] - 1 == mesh.active_edge_set_index:
edge.select = False
bm.select_flush_mode()
return {"FINISHED"}
# ------------------
# ------ UI --------
# ------------------
class OBJECT_UL_edge_sets(bpy.types.UIList):
# The draw_item function is called for each item of the collection that is visible in the list.
# data is the RNA object containing the collection,
# item is the current drawn item of the collection,
# icon is the "computed" icon for the item (as an integer, because some objects like materials or textures
# have custom icons ID, which are not available as enum items).
# active_data is the RNA object containing the active property for the collection (i.e. integer pointing to the
# active item of the collection).
# active_propname is the name of the active property (use 'getattr(active_data, active_propname)').
# index is index of the current item in the collection.
# flt_flag is the result of the filtering process for this item.
# Note: as index and flt_flag are optional arguments, you do not have to use/declare them here if you don't
# need them.
def draw_item(self, context, layout, data, item, icon, active_data, active_propname):
edge_set = item
if self.layout_type in {'DEFAULT', 'COMPACT'}:
row = layout.row()
col = row.column()
col.prop(edge_set, "name", text="", emboss=False, icon_value=icon)
col = row.column(align=True)
col.prop(edge_set, "crease", emboss=False)
elif self.layout_type in {'GRID'}:
layout.alignment = 'CENTER'
layout.label(text="", icon_value=icon)
# And now we can use this list everywhere in Blender. Here is a small example panel.
class EdgeSetsPanel(bpy.types.Panel):
"""Object mesh edges sets panel"""
bl_label = "Edge Sets"
bl_idname = "OBJECT_PT_ui_edge_sets"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "data"
@classmethod
def poll(cls, context):
return (context.object is not None) and (context.object.type == "MESH")
def draw(self, context):
layout = self.layout
mesh = context.object.data
row = layout.row()
col = row.column()
col.template_list("OBJECT_UL_edge_sets", "", mesh, "edge_sets", mesh, "active_edge_set_index", rows=1)
col = row.column(align=True)
col.operator("mesh.edge_set_add", icon='ZOOMIN', text="")
col.operator("mesh.edge_set_remove", icon='ZOOMOUT', text="")
row = layout.row()
row.prop(mesh.edge_sets[mesh.active_edge_set_index], 'crease')
row = layout.row()
row.prop(mesh.edge_sets[mesh.active_edge_set_index], 'bevel')
if context.mode == "EDIT_MESH" and len(mesh.edge_sets) > 0:
row = layout.row()
sub = row.row(align=True)
sub.operator("mesh.edge_set_assign", text="Assign")
sub.operator("mesh.edge_set_remove_from", text="Remove")
sub = row.row(align=True)
sub.operator("mesh.edge_set_select", text="Select")
sub.operator("mesh.edge_set_deselect", text="Deselect")
def register():
bpy.utils.register_module(__name__)
bpy.types.Mesh.edge_sets = bpy.props.CollectionProperty(type=EdgeSetItem)
bpy.types.Mesh.active_edge_set_index = bpy.props.IntProperty()
def unregister():
del bpy.types.Mesh.edge_sets
del bpy.types.Mesh.active_edge_set_index
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()