Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

line mk3 with AB and OD modes #2294

Merged
merged 3 commits into from
Nov 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions docs/nodes/generator/line_mk3.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
Line
====

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

Line generator creates a series of connected segments based on the number of vertices and the length between them. Just a standard subdivided line along X axis

Inputs
------

All parameters except **Center** are vectorized. They will accept single or multiple values.
Both inputs will accept a single number or an array of them. It also will work an array of arrays::

[2]
[2, 4, 6]
[[2], [4]]

Parameters
----------

All parameters except **Center** can be given by the node or an external input.


+---------------+---------------+--------------+---------------------------------------------------------+
| Param | Type | Default | Description |
+===============+===============+==============+=========================================================+
| **Direction** | Enum | "X" | Ortho direction, "from A to B" or "Origin and Direction"|
+---------------+---------------+--------------+---------------------------------------------------------+
| **N Verts** | Int | 2 | number of vertices. The minimum is 2 |
+---------------+---------------+--------------+---------------------------------------------------------+
| **Step** | Float | 1.00 | length between vertices |
+---------------+---------------+--------------+---------------------------------------------------------+
| **Center** | Boolean     | False       | center line around 0 |
+---------------+---------------+--------------+---------------------------------------------------------+
| **Normalize** | Boolean     | False       | determine steps by fixing total length line |
+---------------+---------------+--------------+---------------------------------------------------------+
| **Size** | Float     | 10     | total length of the segment |
+---------------+---------------+--------------+---------------------------------------------------------+
| **A, O** | Vector     | (0,0,0)     | origin point of line |
+---------------+---------------+--------------+---------------------------------------------------------+
| **B** | Vector     | (0.5,0.5,0.5)| end point of line |
+---------------+---------------+--------------+---------------------------------------------------------+
| **D** | Vector     | (1,1,1) | direction of the line |
+---------------+---------------+--------------+---------------------------------------------------------+

Outputs
-------

**Vertices** and **Edges**. Verts and Edges will be generated. Depending on the inputs, the node will generate only one or multiples independent lines. See examples below.


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

.. image:: https://user-images.githubusercontent.com/10011941/47713459-a177d880-dc3a-11e8-935b-a2fa494dc49b.png
:alt: LineDemo1.PNG

The first example shows just an standard line with 5 vertices and 1.00 ud between them

.. image:: https://user-images.githubusercontent.com/10011941/47713473-a9377d00-dc3a-11e8-94ab-39095761788c.png
:alt: LineDemo2.PNG

In this example the step is given by a series of numbers

.. image:: https://user-images.githubusercontent.com/10011941/47713477-ad639a80-dc3a-11e8-9884-6568326d2a33.png
:alt: LineDemo3.PNG

You can create multiple lines if input multiple lists

.. image:: https://user-images.githubusercontent.com/10011941/47713487-b3597b80-dc3a-11e8-996b-17edf1cec9da.png
:alt: LineDemo4.PNG

The AB mode will output a divided segment for each vector pair, the step can be used to change the proportions of the divisions

.. image:: https://user-images.githubusercontent.com/10011941/47713488-b3597b80-dc3a-11e8-9e6e-f742d0338ba5.png
:alt: LineDemo5.PNG

The "OD" mode can be used to visualize normals

.. image:: https://user-images.githubusercontent.com/10011941/47713490-b3597b80-dc3a-11e8-9b6d-b937c0375ec5.png
:alt: LineDemo5.PNG

Advanced example using the node to create a paraboloid grid
2 changes: 1 addition & 1 deletion index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
> Failing to follow these points will break the node category parser.

## Generator
SvLineNodeMK2
SvLineNodeMK3
SvPlaneNodeMK2
SvNGonNode
SvBoxNode
Expand Down
2 changes: 1 addition & 1 deletion json_examples/Architecture/ROADS_LARGE_nikitron_2014.json
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@
"width": 140.0
},
"Line MK2": {
"bl_idname": "SvLineNodeMK2",
"bl_idname": "SvLineNodeMK3",
"color": [
0.0,
0.5,
Expand Down
2 changes: 1 addition & 1 deletion json_examples/Design/lights_colors.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"width": 140.0
},
"Line MK2": {
"bl_idname": "SvLineNodeMK2",
"bl_idname": "SvLineNodeMK3",
"color": [
0.0,
0.5,
Expand Down
2 changes: 1 addition & 1 deletion json_examples/ParametricModelling/hilbert_to_circle.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"width": 140.0
},
"Line": {
"bl_idname": "SvLineNodeMK2",
"bl_idname": "SvLineNodeMK3",
"color": [
0.0,
0.5,
Expand Down
4 changes: 2 additions & 2 deletions json_examples/Shapes/bind_sections_shape.json
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
"width": 391.4091796875
},
"Line MK2": {
"bl_idname": "SvLineNodeMK2",
"bl_idname": "SvLineNodeMK3",
"color": [
0.9200000166893005,
0.9200000166893005,
Expand All @@ -297,7 +297,7 @@
"width": 140.0
},
"Line MK2.001": {
"bl_idname": "SvLineNodeMK2",
"bl_idname": "SvLineNodeMK3",
"color": [
0.9200000166893005,
0.9200000166893005,
Expand Down
240 changes: 240 additions & 0 deletions nodes/generator/line_mk3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
# ##### 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 IntProperty, FloatProperty, BoolProperty, EnumProperty, FloatVectorProperty
from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import updateNode, fullList, match_long_repeat
from sverchok.utils.modules.geom_utils import interp_v3_v3v3, normalize, add_v3_v3v3, sub_v3_v3v3

directionItems = [
("X", "X", "Along X axis", 0),
("Y", "Y", "Along Y axis", 1),
("Z", "Z", "Along Z axis", 2),
("AB", "AB", "Between 2 points", 3),
("OD", "OD", "Origin and Direction", 4),
]


def make_line(steps, center, direction, vert_a, vert_b):
if direction == "X":
vec = lambda l: (l, 0.0, 0.0)
elif direction == "Y":
vec = lambda l: (0.0, l, 0.0)
elif direction == "Z":
vec = lambda l: (0.0, 0.0, l)
elif direction in ["AB", "OD"]:
vec = lambda l: interp_v3_v3v3(vert_a, vert_b, l)

verts = []
add_vert = verts.append
x = -sum(steps) / 2 if center else 0
for s in [0.0] + steps:
x = x + s
add_vert(vec(x))
edges = [[i, i + 1] for i in range(len(steps))]
return verts, edges


class SvLineNodeMK3(bpy.types.Node, SverchCustomTreeNode):
"""
Triggers: Line, segment.
Tooltip: Generate line.
"""
bl_idname = 'SvLineNodeMK3'
bl_label = 'Line'
bl_icon = 'GRIP'

def update_size_socket(self, context):
""" need to do UX transformation before updating node"""
size_socket = self.inputs["Size"]
size_socket.hide_safe = not self.normalize

updateNode(self, context)

def update_vect_socket(self, context):
""" need to do UX transformation before updating node"""
si = self.inputs
sd = self.direction
if sd == "OD" and not si[3].name[0] == "O":
si[3].name = "Origin"
si[4].name = "Direction"
si[3].prop_name = 'v3_origin'
si[4].prop_name = 'v3_dir'
elif sd == "AB" and not si[3].name[0] == "A":
si[3].name = "A"
si[4].name = "B"
si[3].prop_name = 'v3_input_0'
si[4].prop_name = 'v3_input_1'

ortho = sd not in ["AB", "OD"]
if (not ortho and si[3].hide_safe) or ortho:
si[3].hide_safe = ortho
si[4].hide_safe = ortho

updateNode(self, context)

direction = EnumProperty(
name="Direction", items=directionItems,
default="X", update=update_vect_socket)

num = IntProperty(
name='Num Verts', description='Number of Vertices',
default=2, min=2, update=updateNode)

step = FloatProperty(
name='Step', description='Step length',
default=1.0, update=updateNode)

center = BoolProperty(
name='Center', description='Center the line',
default=False, update=updateNode)

normalize = BoolProperty(
name='Normalize', description='Normalize line to size',
default=False, update=update_size_socket)

size = FloatProperty(
name='Size', description='Size of line',
default=10.0, update=updateNode)

v3_input_0 = FloatVectorProperty(
name='A', description='Starting point',
size=3, default=(0, 0, 0),
update=updateNode)

v3_input_1 = FloatVectorProperty(
name='B', description='End point',
size=3, default=(0.5, 0.5, 0.5),
update=updateNode)

v3_origin = FloatVectorProperty(
name='Origin', description='Origin of line',
size=3, default=(0, 0, 0),
update=updateNode)

v3_dir = FloatVectorProperty(
name='Direction', description='Direction',
size=3, default=(1, 1, 1),
update=updateNode)

def set_size_socket(self):
size_socket = self.inputs.new('StringsSocket', "Size")
size_socket.prop_name = 'size'
size_socket.hide_safe = not self.normalize

def set_vector_sockets(self):
si = self.inputs
si.new('VerticesSocket', "A").prop_name = 'v3_input_0'
si.new('VerticesSocket', "B").prop_name = 'v3_input_1'
si[3].hide_safe = self.direction not in ["AB", " OD"]
si[4].hide_safe = self.direction not in ["AB", " OD"]

def sv_init(self, context):
si = self.inputs
si.new('StringsSocket', "Num").prop_name = 'num'
si.new('StringsSocket', "Step").prop_name = 'step'
self.set_size_socket()
self.set_vector_sockets()
self.outputs.new('VerticesSocket', "Vertices", "Vertices")
self.outputs.new('StringsSocket', "Edges", "Edges")

def draw_buttons(self, context, layout):
col = layout.column(align=True)
row = col.row(align=True)
row.prop(self, "direction", expand=True)
row = col.row(align=True)
row.prop(self, "center", toggle=True)
row.prop(self, "normalize", toggle=True)

def get_data(self):
c, d = self.center, self.direction
input_num = self.inputs["Num"].sv_get()
input_step = self.inputs["Step"].sv_get()
normal_size = [2.0 if c else 1.0]

if self.normalize:

normal_size = self.inputs["Size"].sv_get()[0]

params = [input_num, input_step, normal_size]

if d in ["AB", "OD"]:
v_a = self.inputs[3].sv_get()[0]
v_b = self.inputs[4].sv_get()[0]
params.append(v_a)
params.append(v_b)

return match_long_repeat(params)

def define_steplist(self, step_list, s, n, nor, normal):

for num in n:
num = max(2, num)
s = s[:(num - 1)] # shorten if needed
fullList(s, num - 1) # extend if needed
step_list.append([S * nor / sum(s) for S in s] if normal else s)

def process_vectors(self, pts_list, d, va, vb):
if d == "AB" and self.normalize:
vb = add_v3_v3v3(normalize(sub_v3_v3v3(vb, va)), va)
elif d == "OD":
vb = add_v3_v3v3(normalize(vb), va)
pts_list.append((va, vb))

def process(self):
if not any(s.is_linked for s in self.outputs):
return

c, d = self.center, self.direction
step_list = []
pts_list = []
verts_out, edges_out = [], []
normal = self.normalize or d == "AB"
advanced = d in ["AB", "OD"]
params = self.get_data()
if advanced:
for p in zip(*params):
n, s, nor, va, vb = p
self.define_steplist(step_list, s, n, nor, normal)
self.process_vectors(pts_list, d, va, vb)
for s, vc in zip(step_list, pts_list):
r1, r2 = make_line(s, c, d, vc[0], vc[1])
verts_out.append(r1)
edges_out.append(r2)
else:
for p in zip(*params):
n, s, nor = p
self.define_steplist(step_list, s, n, nor, normal)
for s in step_list:
r1, r2 = make_line(s, c, d, [], [])
verts_out.append(r1)
edges_out.append(r2)

if self.outputs['Vertices'].is_linked:
self.outputs['Vertices'].sv_set(verts_out)
if self.outputs['Edges'].is_linked:
self.outputs['Edges'].sv_set(edges_out)


def register():
bpy.utils.register_class(SvLineNodeMK3)


def unregister():
bpy.utils.unregister_class(SvLineNodeMK3)
2 changes: 2 additions & 0 deletions nodes/generator/line_mk2.py → old_nodes/line_mk2.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class SvLineNodeMK2(bpy.types.Node, SverchCustomTreeNode):
bl_label = 'Line MK2'
bl_icon = 'GRIP'

replacement_nodes = [('SvLineNodeMK3', None, None)]

def upgrade_if_needed(self):
""" This allows us to keep the node mk2 - on the fly node upgrade"""
if "Size" not in self.inputs:
Expand Down
3 changes: 3 additions & 0 deletions utils/modules/geom_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def normalize(v):
def sub_v3_v3v3(a, b):
return a[0]-b[0], a[1]-b[1], a[2]-b[2]

def add_v3_v3v3(a, b):
return a[0]+b[0], a[1]+b[1], a[2]+b[2]

def madd_v3_v3v3fl(a, b, f=1.0):
return a[0]+b[0]*f, a[1]+b[1]*f, a[2]+b[2]*f

Expand Down