Skip to content

Commit

Permalink
Merge pull request #2352 from vicdoval/length_node
Browse files Browse the repository at this point in the history
Added Path Length Node
  • Loading branch information
vicdoval authored Jan 23, 2019
2 parents 5caad0a + 31f7b11 commit 6bbd868
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/nodes/analyzers/analyzers_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Analyzers
normals
bvh_overlap_polys
object_insolation
path_length
points_inside_mesh
polygons_centers
polygons_centers_mk3
Expand Down
48 changes: 48 additions & 0 deletions docs/nodes/analyzers/path_length.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Path Length
===========

Functionality
-------------

Path Length node is one of the analyzer type. It is used to get the length of any path, no matter the number of its vertices or its world position.


Inputs / Parameters
-------------------


+------------------+---------------+-------------+--------------------------------------------------+
| Param | Type | Default | Description |
+==================+===============+=============+==================================================+
| **Vertices** | Vertices | None | Vertices of the edges / path |
+------------------+---------------+-------------+--------------------------------------------------+
| **Edges** | Strings | None | Edges referenced to vertices |
+------------------+---------------+-------------+--------------------------------------------------+
| **Segment** | Boolean | True | output the segments lengths or its sum |
+------------------+---------------+-------------+--------------------------------------------------+

In the N-Panel you can use the toggle **Output NumPy** to get NumPy arrays in stead of regular lists (makes the node faster).

Outputs
-------

**Length**: it will be calculated if **Vertices** input is linked. If no **Edges** are supplied the node will use the sequence order to calculate de length.


Example of usage
----------------

.. image:: https://user-images.githubusercontent.com/10011941/51251936-c4449e00-199a-11e9-89a7-557cc7e93731.png
:alt: PathLengthDemo1.PNG

Measuring a Bender curve with the default vertices, with a higher interpolation and by segments

.. image:: https://user-images.githubusercontent.com/10011941/51251933-c4449e00-199a-11e9-99b8-fa53c8586484.png
:alt: PathLengthDemo2.PNG

Using the node to know the linear distance needed to build a 3 meter radius geodesic dome

.. image:: https://user-images.githubusercontent.com/10011941/51251931-c4449e00-199a-11e9-9e75-69ead34fad64.png
:alt: PathLengthDemo2.PNG

Using the *Path Length* node to place circles of one meter of diameter along a given curve
1 change: 1 addition & 0 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
SvVolumeNode
AreaNode
DistancePPNode
SvPathLengthNode
CentersPolsNodeMK2
CentersPolsNodeMK3
GetNormalsNode
Expand Down
131 changes: 131 additions & 0 deletions nodes/analyzer/path_length.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# ##### 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, get_edge_list
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(bpy.types.Node, SverchCustomTreeNode):
'''
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'

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('VerticesSocket', "Vertices")
sinw('StringsSocket', "Edges")

sonw('StringsSocket', "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)
Binary file added ui/icons/sv_path_length.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 10 additions & 11 deletions utils/sv_default_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ def verbose_macro_handler(cls, operator, context, term):
elif term == 'gp +':
needed_nodes = [
['SvGetAssetProperties', (0.00, 0.00)],
['SvScriptNodeLite', (250, 55)],
['SvScalarMathNodeMK2', (430, 115)],
['Float2IntNode', (600, 50)],
['SvGenFloatRange', (720, 90)],
['SvInterpolationNodeMK3', (880, 40)],
['LineConnectNodeMK2', (1060, -40)],
['ViewerNode2', (1245, 50)],
['SvPathLengthNode', (250, 115)],
['SvScalarMathNodeMK2', (420, 115)],
['Float2IntNode', (590, 115)],
['SvGenFloatRange', (760, 115)],
['SvInterpolationNodeMK3', (930, 115)],
['LineConnectNodeMK2', (1100, 115)],
['ViewerNode2', (1290, 115)],
]

made_nodes = []
Expand All @@ -149,9 +149,8 @@ def verbose_macro_handler(cls, operator, context, term):
n.location = node_location[0] + x, node_location[1] + y
made_nodes.append(n)

# Script node lite
snlite = made_nodes[1]
sn_loader(snlite, script_name='path_length.py')
# Path Length
made_nodes[1].segment = False

# ID Selector
made_nodes[0].Mode = 'grease_pencil' # the rest must be user driven
Expand All @@ -160,7 +159,7 @@ def verbose_macro_handler(cls, operator, context, term):
# Scalar Math node
made_nodes[2].current_op = 'MUL'
made_nodes[2].y_ = 2.5
links.new(made_nodes[1].outputs[0], made_nodes[2].inputs[0]) # snlite-> math
links.new(made_nodes[1].outputs[0], made_nodes[2].inputs[0]) # path length -> math
links.new(made_nodes[2].outputs[0], made_nodes[3].inputs[0]) # math -> float

# Float2Int node
Expand Down

0 comments on commit 6bbd868

Please sign in to comment.