|
|
@@ -1,345 +0,0 @@
|
|
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
|
|
-# --------------------------------- DUAL MESH -------------------------------- #
|
|
20
|
|
-# -------------------------------- version 0.3 ------------------------------- #
|
|
21
|
|
-# #
|
|
22
|
|
-# Convert a generic mesh to its dual. With open meshes it can get some wired #
|
|
23
|
|
-# effect on the borders. #
|
|
24
|
|
-# #
|
|
25
|
|
-# (c) Alessandro Zomparelli #
|
|
26
|
|
-# (2017) #
|
|
27
|
|
-# #
|
|
28
|
|
-# http://www.co-de-it.com/ #
|
|
29
|
|
-# #
|
|
30
|
|
-# ############################################################################ #
|
|
31
|
|
-
|
|
32
|
|
-
|
|
33
|
|
-import bpy
|
|
34
|
|
-from bpy.types import Operator
|
|
35
|
|
-from bpy.props import (
|
|
36
|
|
- BoolProperty,
|
|
37
|
|
- EnumProperty,
|
|
38
|
|
- )
|
|
39
|
|
-import bmesh
|
|
40
|
|
-from .utils import *
|
|
41
|
|
-
|
|
42
|
|
-
|
|
43
|
|
-class dual_mesh_tessellated(Operator):
|
|
44
|
|
- bl_idname = "object.dual_mesh_tessellated"
|
|
45
|
|
- bl_label = "Dual Mesh"
|
|
46
|
|
- bl_description = ("Generate a polygonal mesh using Tessellate. (Non-destructive)")
|
|
47
|
|
- bl_options = {'REGISTER', 'UNDO'}
|
|
48
|
|
-
|
|
49
|
|
- apply_modifiers : BoolProperty(
|
|
50
|
|
- name="Apply Modifiers",
|
|
51
|
|
- default=True,
|
|
52
|
|
- description="Apply object's modifiers"
|
|
53
|
|
- )
|
|
54
|
|
-
|
|
55
|
|
- source_faces : EnumProperty(
|
|
56
|
|
- items=[
|
|
57
|
|
- ('QUAD', 'Quad Faces', ''),
|
|
58
|
|
- ('TRI', 'Triangles', '')],
|
|
59
|
|
- name="Source Faces",
|
|
60
|
|
- description="Source polygons",
|
|
61
|
|
- default="QUAD",
|
|
62
|
|
- options={'LIBRARY_EDITABLE'}
|
|
63
|
|
- )
|
|
64
|
|
-
|
|
65
|
|
- def execute(self, context):
|
|
66
|
|
- auto_layer_collection()
|
|
67
|
|
- ob0 = context.object
|
|
68
|
|
- name1 = "DualMesh_{}_Component".format(self.source_faces)
|
|
69
|
|
- # Generate component
|
|
70
|
|
- if self.source_faces == 'QUAD':
|
|
71
|
|
- verts = [(0.0, 0.0, 0.0), (0.0, 0.5, 0.0),
|
|
72
|
|
- (0.0, 1.0, 0.0), (0.5, 1.0, 0.0),
|
|
73
|
|
- (1.0, 1.0, 0.0), (1.0, 0.5, 0.0),
|
|
74
|
|
- (1.0, 0.0, 0.0), (0.5, 0.0, 0.0),
|
|
75
|
|
- (1/3, 1/3, 0.0), (2/3, 2/3, 0.0)]
|
|
76
|
|
- edges = [(0,1), (1,2), (2,3), (3,4), (4,5), (5,6), (6,7),
|
|
77
|
|
- (7,0), (1,8), (8,7), (3,9), (9,5), (8,9)]
|
|
78
|
|
- faces = [(7,8,1,0), (8,9,3,2,1), (9,5,4,3), (9,8,7,6,5)]
|
|
79
|
|
- else:
|
|
80
|
|
- verts = [(0.0,0.0,0.0), (0.5,0.0,0.0), (1.0,0.0,0.0), (0.0,1.0,0.0), (0.5,1.0,0.0), (1.0,1.0,0.0)]
|
|
81
|
|
- edges = [(0,1), (1,2), (2,5), (5,4), (4,3), (3,0), (1,4)]
|
|
82
|
|
- faces = [(0,1,4,3), (1,2,5,4)]
|
|
83
|
|
-
|
|
84
|
|
- # check pre-existing component
|
|
85
|
|
- try:
|
|
86
|
|
- _verts = [0]*len(verts)*3
|
|
87
|
|
- __verts = [c for co in verts for c in co]
|
|
88
|
|
- ob1 = bpy.data.objects[name1]
|
|
89
|
|
- ob1.data.vertices.foreach_get("co",_verts)
|
|
90
|
|
- for a, b in zip(_verts, __verts):
|
|
91
|
|
- if abs(a-b) > 0.0001:
|
|
92
|
|
- raise ValueError
|
|
93
|
|
- except:
|
|
94
|
|
- me = bpy.data.meshes.new("Dual-Mesh") # add a new mesh
|
|
95
|
|
- me.from_pydata(verts, edges, faces)
|
|
96
|
|
- me.update(calc_edges=True, calc_edges_loose=True)
|
|
97
|
|
- if self.source_faces == 'QUAD': n_seams = 8
|
|
98
|
|
- else: n_seams = 6
|
|
99
|
|
- for i in range(n_seams): me.edges[i].use_seam = True
|
|
100
|
|
- ob1 = bpy.data.objects.new(name1, me)
|
|
101
|
|
- context.collection.objects.link(ob1)
|
|
102
|
|
- # fix visualization issue
|
|
103
|
|
- context.view_layer.objects.active = ob1
|
|
104
|
|
- ob1.select_set(True)
|
|
105
|
|
- bpy.ops.object.editmode_toggle()
|
|
106
|
|
- bpy.ops.object.editmode_toggle()
|
|
107
|
|
- ob1.select_set(False)
|
|
108
|
|
- # hide component
|
|
109
|
|
- ob1.hide_select = True
|
|
110
|
|
- ob1.hide_render = True
|
|
111
|
|
- ob1.hide_viewport = True
|
|
112
|
|
- ob = convert_object_to_mesh(ob0,False,False)
|
|
113
|
|
- ob.name = 'DualMesh'
|
|
114
|
|
- #ob = bpy.data.objects.new("DualMesh", convert_object_to_mesh(ob0,False,False))
|
|
115
|
|
- #context.collection.objects.link(ob)
|
|
116
|
|
- #context.view_layer.objects.active = ob
|
|
117
|
|
- #ob.select_set(True)
|
|
118
|
|
- ob.tissue_tessellate.component = ob1
|
|
119
|
|
- ob.tissue_tessellate.generator = ob0
|
|
120
|
|
- ob.tissue_tessellate.gen_modifiers = self.apply_modifiers
|
|
121
|
|
- ob.tissue_tessellate.merge = True
|
|
122
|
|
- ob.tissue_tessellate.bool_dissolve_seams = True
|
|
123
|
|
- if self.source_faces == 'TRI': ob.tissue_tessellate.fill_mode = 'FAN'
|
|
124
|
|
- bpy.ops.object.update_tessellate()
|
|
125
|
|
- ob.location = ob0.location
|
|
126
|
|
- ob.matrix_world = ob0.matrix_world
|
|
127
|
|
- return {'FINISHED'}
|
|
128
|
|
-
|
|
129
|
|
- def invoke(self, context, event):
|
|
130
|
|
- return context.window_manager.invoke_props_dialog(self)
|
|
131
|
|
-
|
|
132
|
|
-class dual_mesh(Operator):
|
|
133
|
|
- bl_idname = "object.dual_mesh"
|
|
134
|
|
- bl_label = "Convert to Dual Mesh"
|
|
135
|
|
- bl_description = ("Convert a generic mesh into a polygonal mesh. (Destructive)")
|
|
136
|
|
- bl_options = {'REGISTER', 'UNDO'}
|
|
137
|
|
-
|
|
138
|
|
- quad_method : EnumProperty(
|
|
139
|
|
- items=[('BEAUTY', 'Beauty',
|
|
140
|
|
- 'Split the quads in nice triangles, slower method'),
|
|
141
|
|
- ('FIXED', 'Fixed',
|
|
142
|
|
- 'Split the quads on the 1st and 3rd vertices'),
|
|
143
|
|
- ('FIXED_ALTERNATE', 'Fixed Alternate',
|
|
144
|
|
- 'Split the quads on the 2nd and 4th vertices'),
|
|
145
|
|
- ('SHORTEST_DIAGONAL', 'Shortest Diagonal',
|
|
146
|
|
- 'Split the quads based on the distance between the vertices')
|
|
147
|
|
- ],
|
|
148
|
|
- name="Quad Method",
|
|
149
|
|
- description="Method for splitting the quads into triangles",
|
|
150
|
|
- default="FIXED",
|
|
151
|
|
- options={'LIBRARY_EDITABLE'}
|
|
152
|
|
- )
|
|
153
|
|
- polygon_method : EnumProperty(
|
|
154
|
|
- items=[
|
|
155
|
|
- ('BEAUTY', 'Beauty', 'Arrange the new triangles evenly'),
|
|
156
|
|
- ('CLIP', 'Clip',
|
|
157
|
|
- 'Split the polygons with an ear clipping algorithm')],
|
|
158
|
|
- name="Polygon Method",
|
|
159
|
|
- description="Method for splitting the polygons into triangles",
|
|
160
|
|
- default="BEAUTY",
|
|
161
|
|
- options={'LIBRARY_EDITABLE'}
|
|
162
|
|
- )
|
|
163
|
|
- preserve_borders : BoolProperty(
|
|
164
|
|
- name="Preserve Borders",
|
|
165
|
|
- default=True,
|
|
166
|
|
- description="Preserve original borders"
|
|
167
|
|
- )
|
|
168
|
|
- apply_modifiers : BoolProperty(
|
|
169
|
|
- name="Apply Modifiers",
|
|
170
|
|
- default=True,
|
|
171
|
|
- description="Apply object's modifiers"
|
|
172
|
|
- )
|
|
173
|
|
-
|
|
174
|
|
- def execute(self, context):
|
|
175
|
|
- mode = context.mode
|
|
176
|
|
- if mode == 'EDIT_MESH':
|
|
177
|
|
- mode = 'EDIT'
|
|
178
|
|
- act = context.active_object
|
|
179
|
|
- if mode != 'OBJECT':
|
|
180
|
|
- sel = [act]
|
|
181
|
|
- bpy.ops.object.mode_set(mode='OBJECT')
|
|
182
|
|
- else:
|
|
183
|
|
- sel = context.selected_objects
|
|
184
|
|
- doneMeshes = []
|
|
185
|
|
-
|
|
186
|
|
- for ob0 in sel:
|
|
187
|
|
- if ob0.type != 'MESH':
|
|
188
|
|
- continue
|
|
189
|
|
- if ob0.data.name in doneMeshes:
|
|
190
|
|
- continue
|
|
191
|
|
- ob = ob0
|
|
192
|
|
- mesh_name = ob0.data.name
|
|
193
|
|
-
|
|
194
|
|
- # store linked objects
|
|
195
|
|
- clones = []
|
|
196
|
|
- n_users = ob0.data.users
|
|
197
|
|
- count = 0
|
|
198
|
|
- for o in bpy.data.objects:
|
|
199
|
|
- if o.type != 'MESH':
|
|
200
|
|
- continue
|
|
201
|
|
- if o.data.name == mesh_name:
|
|
202
|
|
- count += 1
|
|
203
|
|
- clones.append(o)
|
|
204
|
|
- if count == n_users:
|
|
205
|
|
- break
|
|
206
|
|
-
|
|
207
|
|
- if self.apply_modifiers:
|
|
208
|
|
- bpy.ops.object.convert(target='MESH')
|
|
209
|
|
- ob.data = ob.data.copy()
|
|
210
|
|
- bpy.ops.object.select_all(action='DESELECT')
|
|
211
|
|
- ob.select_set(True)
|
|
212
|
|
- context.view_layer.objects.active = ob0
|
|
213
|
|
- bpy.ops.object.mode_set(mode='EDIT')
|
|
214
|
|
-
|
|
215
|
|
- # prevent borders erosion
|
|
216
|
|
- bpy.ops.mesh.select_mode(
|
|
217
|
|
- use_extend=False, use_expand=False, type='EDGE'
|
|
218
|
|
- )
|
|
219
|
|
- bpy.ops.mesh.select_non_manifold(
|
|
220
|
|
- extend=False, use_wire=False, use_boundary=True,
|
|
221
|
|
- use_multi_face=False, use_non_contiguous=False,
|
|
222
|
|
- use_verts=False
|
|
223
|
|
- )
|
|
224
|
|
- bpy.ops.mesh.extrude_region_move(
|
|
225
|
|
- MESH_OT_extrude_region={"mirror": False},
|
|
226
|
|
- TRANSFORM_OT_translate={"value": (0, 0, 0)}
|
|
227
|
|
- )
|
|
228
|
|
-
|
|
229
|
|
- bpy.ops.mesh.select_mode(
|
|
230
|
|
- use_extend=False, use_expand=False, type='VERT',
|
|
231
|
|
- action='TOGGLE'
|
|
232
|
|
- )
|
|
233
|
|
- bpy.ops.mesh.select_all(action='SELECT')
|
|
234
|
|
- bpy.ops.mesh.quads_convert_to_tris(
|
|
235
|
|
- quad_method=self.quad_method, ngon_method=self.polygon_method
|
|
236
|
|
- )
|
|
237
|
|
- bpy.ops.mesh.select_all(action='DESELECT')
|
|
238
|
|
- bpy.ops.object.mode_set(mode='OBJECT')
|
|
239
|
|
- bpy.ops.object.modifier_add(type='SUBSURF')
|
|
240
|
|
- ob.modifiers[-1].name = "dual_mesh_subsurf"
|
|
241
|
|
- while True:
|
|
242
|
|
- bpy.ops.object.modifier_move_up(modifier="dual_mesh_subsurf")
|
|
243
|
|
- if ob.modifiers[0].name == "dual_mesh_subsurf":
|
|
244
|
|
- break
|
|
245
|
|
-
|
|
246
|
|
- bpy.ops.object.modifier_apply(
|
|
247
|
|
- apply_as='DATA', modifier='dual_mesh_subsurf'
|
|
248
|
|
- )
|
|
249
|
|
-
|
|
250
|
|
- bpy.ops.object.mode_set(mode='EDIT')
|
|
251
|
|
- bpy.ops.mesh.select_all(action='DESELECT')
|
|
252
|
|
-
|
|
253
|
|
- verts = ob.data.vertices
|
|
254
|
|
-
|
|
255
|
|
- bpy.ops.object.mode_set(mode='OBJECT')
|
|
256
|
|
- verts[-1].select = True
|
|
257
|
|
- bpy.ops.object.mode_set(mode='EDIT')
|
|
258
|
|
- bpy.ops.mesh.select_more(use_face_step=False)
|
|
259
|
|
-
|
|
260
|
|
- bpy.ops.mesh.select_similar(
|
|
261
|
|
- type='EDGE', compare='EQUAL', threshold=0.01)
|
|
262
|
|
- bpy.ops.mesh.select_all(action='INVERT')
|
|
263
|
|
-
|
|
264
|
|
- bpy.ops.mesh.dissolve_verts()
|
|
265
|
|
- bpy.ops.mesh.select_all(action='DESELECT')
|
|
266
|
|
-
|
|
267
|
|
- bpy.ops.mesh.select_non_manifold(
|
|
268
|
|
- extend=False, use_wire=False, use_boundary=True,
|
|
269
|
|
- use_multi_face=False, use_non_contiguous=False, use_verts=False)
|
|
270
|
|
- bpy.ops.mesh.select_more()
|
|
271
|
|
-
|
|
272
|
|
- # find boundaries
|
|
273
|
|
- bpy.ops.object.mode_set(mode='OBJECT')
|
|
274
|
|
- bound_v = [v.index for v in ob.data.vertices if v.select]
|
|
275
|
|
- bound_e = [e.index for e in ob.data.edges if e.select]
|
|
276
|
|
- bound_p = [p.index for p in ob.data.polygons if p.select]
|
|
277
|
|
- bpy.ops.object.mode_set(mode='EDIT')
|
|
278
|
|
-
|
|
279
|
|
- # select quad faces
|
|
280
|
|
- context.tool_settings.mesh_select_mode = (False, False, True)
|
|
281
|
|
- bpy.ops.mesh.select_face_by_sides(number=4, extend=False)
|
|
282
|
|
-
|
|
283
|
|
- # deselect boundaries
|
|
284
|
|
- bpy.ops.object.mode_set(mode='OBJECT')
|
|
285
|
|
- for i in bound_v:
|
|
286
|
|
- context.active_object.data.vertices[i].select = False
|
|
287
|
|
- for i in bound_e:
|
|
288
|
|
- context.active_object.data.edges[i].select = False
|
|
289
|
|
- for i in bound_p:
|
|
290
|
|
- context.active_object.data.polygons[i].select = False
|
|
291
|
|
-
|
|
292
|
|
- bpy.ops.object.mode_set(mode='EDIT')
|
|
293
|
|
-
|
|
294
|
|
- context.tool_settings.mesh_select_mode = (False, False, True)
|
|
295
|
|
- bpy.ops.mesh.edge_face_add()
|
|
296
|
|
- context.tool_settings.mesh_select_mode = (True, False, False)
|
|
297
|
|
- bpy.ops.mesh.select_all(action='DESELECT')
|
|
298
|
|
-
|
|
299
|
|
- # delete boundaries
|
|
300
|
|
- bpy.ops.mesh.select_non_manifold(
|
|
301
|
|
- extend=False, use_wire=True, use_boundary=True,
|
|
302
|
|
- use_multi_face=False, use_non_contiguous=False, use_verts=True
|
|
303
|
|
- )
|
|
304
|
|
- bpy.ops.mesh.delete(type='VERT')
|
|
305
|
|
-
|
|
306
|
|
- # remove middle vertices
|
|
307
|
|
- bm = bmesh.from_edit_mesh(ob.data)
|
|
308
|
|
- for v in bm.verts:
|
|
309
|
|
- if len(v.link_edges) == 2 and len(v.link_faces) < 3:
|
|
310
|
|
- v.select = True
|
|
311
|
|
-
|
|
312
|
|
- # dissolve
|
|
313
|
|
- bpy.ops.mesh.dissolve_verts()
|
|
314
|
|
- bpy.ops.mesh.select_all(action='DESELECT')
|
|
315
|
|
-
|
|
316
|
|
- # remove border faces
|
|
317
|
|
- if not self.preserve_borders:
|
|
318
|
|
- bpy.ops.mesh.select_non_manifold(
|
|
319
|
|
- extend=False, use_wire=False, use_boundary=True,
|
|
320
|
|
- use_multi_face=False, use_non_contiguous=False, use_verts=False
|
|
321
|
|
- )
|
|
322
|
|
- bpy.ops.mesh.select_more()
|
|
323
|
|
- bpy.ops.mesh.delete(type='FACE')
|
|
324
|
|
-
|
|
325
|
|
- # clean wires
|
|
326
|
|
- bpy.ops.mesh.select_non_manifold(
|
|
327
|
|
- extend=False, use_wire=True, use_boundary=False,
|
|
328
|
|
- use_multi_face=False, use_non_contiguous=False, use_verts=False
|
|
329
|
|
- )
|
|
330
|
|
- bpy.ops.mesh.delete(type='EDGE')
|
|
331
|
|
-
|
|
332
|
|
- bpy.ops.object.mode_set(mode='OBJECT')
|
|
333
|
|
- ob0.data.name = mesh_name
|
|
334
|
|
- doneMeshes.append(mesh_name)
|
|
335
|
|
-
|
|
336
|
|
- for o in clones:
|
|
337
|
|
- o.data = ob.data
|
|
338
|
|
-
|
|
339
|
|
- for o in sel:
|
|
340
|
|
- o.select_set(True)
|
|
341
|
|
-
|
|
342
|
|
- context.view_layer.objects.active = act
|
|
343
|
|
- bpy.ops.object.mode_set(mode=mode)
|
|
344
|
|
-
|
|
345
|
|
- return {'FINISHED'}
|