-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathconvex_hull.py
87 lines (65 loc) · 2.47 KB
/
convex_hull.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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# 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 #####
import bpy
import bmesh
from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import Vector_generate
from sverchok.utils.sv_bmesh_utils import pydata_from_bmesh
#
# Convex Hull
# by Linus Yng
def make_hull(vertices):
if not vertices:
return False
bm = bmesh.new()
bm_verts = [bm.verts.new(v) for v in vertices]
bmesh.ops.convex_hull(bm, input=bm_verts, use_existing_faces=False)
verts, _, faces = pydata_from_bmesh(bm)
bm.clear()
bm.free()
return (verts, faces)
class SvConvexHullNode(SverchCustomTreeNode, bpy.types.Node):
'''Create convex hull'''
bl_idname = 'SvConvexHullNode'
bl_label = 'Convex Hull'
bl_icon = 'OUTLINER_OB_EMPTY'
sv_icon = 'SV_CONVEX_HULL'
replacement_nodes = [('SvConvexHullNodeMK2', None, None)]
def sv_init(self, context):
self.inputs.new('SvVerticesSocket', 'Vertices')
self.outputs.new('SvVerticesSocket', 'Vertices')
self.outputs.new('SvStringsSocket', 'Polygons')
def draw_buttons(self, context, layout):
pass
def process(self):
if self.inputs['Vertices'].is_linked:
verts = Vector_generate(self.inputs['Vertices'].sv_get())
verts_out = []
polys_out = []
for v_obj in verts:
res = make_hull(v_obj)
if not res:
return
verts_out.append(res[0])
polys_out.append(res[1])
self.outputs['Vertices'].sv_set(verts_out)
self.outputs['Polygons'].sv_set(polys_out)
def register():
bpy.utils.register_class(SvConvexHullNode)
def unregister():
bpy.utils.unregister_class(SvConvexHullNode)