-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathpath_length.py
133 lines (100 loc) · 3.88 KB
/
path_length.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
# ##### 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
from bpy.props import BoolProperty
from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import updateNode, match_long_repeat
import numpy as np
def edges_aux(vertices):
'''create auxiliary edges array '''
v_len = [len(v) for v in vertices]
v_len_max = max(v_len)
np_in = np.arange(v_len_max - 1)
np_edges = np.array([np_in, np_in + 1]).T
return [np_edges]
def edges_length(meshes, gates, result):
'''calculate edges length '''
for vertices, edges in zip(*meshes):
np_verts = np.array(vertices)
if type(edges[0]) in (list, tuple):
np_edges = np.array(edges)
else:
np_edges = edges[:len(vertices)-1, :]
vect = np_verts[np_edges[:, 0], :] - np_verts[np_edges[:, 1], :]
length = np.linalg.norm(vect, axis=1)
if not gates[1]:
length = np.sum(length)[np.newaxis]
result.append(length if gates[0] else length.tolist())
return result
class SvPathLengthNode(SverchCustomTreeNode, bpy.types.Node):
'''
Triggers: Path / Edges length
Tooltip: Measures the length of a path or the length of its segments
'''
bl_idname = 'SvPathLengthNode'
bl_label = 'Path Length'
sv_icon = 'SV_PATH_LENGTH'
replacement_nodes = [('SvPathLengthMk2Node', None, None)]
output_numpy : BoolProperty(
name='Output NumPy', description='output NumPy arrays',
default=False, update=updateNode)
segment : BoolProperty(
name='Segment', description='Get segments length or the sum of them',
default=True, update=updateNode)
def draw_buttons(self, context, layout):
'''draw buttons on the Node'''
layout.prop(self, "segment", toggle=False)
def draw_buttons_ext(self, context, layout):
'''draw buttons on the N-panel'''
self.draw_buttons(context, layout)
layout.prop(self, "output_numpy", toggle=False)
def sv_init(self, context):
'''create sockets'''
sinw = self.inputs.new
sonw = self.outputs.new
sinw('SvVerticesSocket', "Vertices")
sinw('SvStringsSocket', "Edges")
sonw('SvStringsSocket', "Length")
def get_data(self):
'''get all data from sockets'''
si = self.inputs
vertices = si['Vertices'].sv_get()
if si['Edges'].is_linked:
edges_in = si['Edges'].sv_get()
else:
edges_in = edges_aux(vertices)
return match_long_repeat([vertices, edges_in])
def process(self):
'''main node function called every update'''
si = self.inputs
so = self.outputs
if not (so['Length'].is_linked):
return
result = []
gates = []
gates.append(self.output_numpy)
gates.append(self.segment)
meshes = self.get_data()
result = edges_length(meshes, gates, result)
so['Length'].sv_set(result)
def register():
'''register class in Blender'''
bpy.utils.register_class(SvPathLengthNode)
def unregister():
'''unregister class in Blender'''
bpy.utils.unregister_class(SvPathLengthNode)