Skip to content

Commit

Permalink
Revert "Vector In and Vector Out to multilevel recursive (#3673)" (#3695
Browse files Browse the repository at this point in the history
)

This reverts commit a95871e.
  • Loading branch information
vicdoval authored Nov 5, 2020
1 parent ae82a9e commit 85e0643
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 128 deletions.
2 changes: 1 addition & 1 deletion docs/nodes/generator/random_vector_mk3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ In the N-Panel (and on the right-click menu) you can find:

**Match List**: Define how list with different lengths should be matched ('Short', 'Repeat Last' or 'Cycle')

**Output NumPy**: Get NumPy arrays in stead of regular lists (makes the node faster).
**Output NumPy**: Get NumPy arrays in stead of regular lists (makes the node faster). Available for Vertices, Edges and Pols

Outputs
-------
Expand Down
6 changes: 0 additions & 6 deletions docs/nodes/vector/vector_in.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ In the N-Panel (and on the right-click menu) you can find:

**Implementation**: Python or NumPy. Python is the default and is usually faster if you input regular lists and want to get regular list. The NumPy implementation will be faster if you are using/getting lists of NumPy arrays.

**Match List**: Define how list with different lengths should be matched ('Short', 'Repeat Last' or 'Cycle')

**Simplify Output**: Method to keep output data suitable for most of the rest of the Sverchok nodes
- None: Do not perform any change on the data. Only for advanced users
- Flat: It will flat the output to keep vectors list in Level 3 (regular vector list)

**Output NumPy**: Get NumPy arrays in stead of regular lists.

Outputs
Expand Down
4 changes: 0 additions & 4 deletions docs/nodes/vector/vector_out.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ Advanced Parameters

In the N-Panel (and on the right-click menu) you can find:

**Simplify Output**: Method to keep output data suitable for most of the rest of the Sverchok nodes
- None: Do not perform any change on the data. Only for advanced users
- Flat: It will flat the output to keep vectors list in Level 3 (regular vector list)

**Output NumPy**: Get NumPy arrays in stead of regular lists (makes the node faster if you input NumPy arrays).

Outputs
Expand Down
77 changes: 28 additions & 49 deletions nodes/vector/vector_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import bpy
from bpy.props import FloatProperty, BoolProperty, StringProperty, EnumProperty
from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import updateNode, list_match_func, numpy_list_match_modes, numpy_list_match_func
from sverchok.utils.sv_itertools import recurse_f_level_control
from sverchok.data_structure import updateNode, fullList, fullList_deep_copy, numpy_match_long_repeat
from sverchok.utils.sv_itertools import sv_zip_longest
from numpy import array

Expand All @@ -42,33 +41,27 @@ def execute(self, context):

return{'FINISHED'}

def python_pack_vecs(params, constant, matching_f):
X_, Y_, Z_ = params
output_numpy, _ = constant
def python_pack_vecs(X_, Y_, Z_, output_numpy):
series_vec = []
for vs in zip(X_, Y_, Z_):
x, y, z = matching_f(vs)
for x, y, z in zip(X_, Y_, Z_):
max_v = max(map(len, (x, y, z)))
fullList(x, max_v)
fullList(y, max_v)
fullList(z, max_v)
series_vec.append(array([x,y,z]).T if output_numpy else list(zip(x, y, z)))
return series_vec

def numpy_pack_vecs(params, constant, matching_f):
X_, Y_, Z_ = params
output_numpy, match_mode = constant
np_match = numpy_list_match_func[match_mode]
def numpy_pack_vecs(X_, Y_, Z_, output_numpy):
series_vec = []
for obj in zip(X_, Y_, Z_):
np_obj = [array(p) for p in obj]
x, y, z = np_match(np_obj)
x, y, z = numpy_match_long_repeat(np_obj)
vecs = array([x,y,z]).T
series_vec.append(vecs if output_numpy else vecs.tolist())
return series_vec

class GenVectorsNode(bpy.types.Node, SverchCustomTreeNode):
"""
Triggers: Combine XYZ
Tooltip: Generate vectors from numbers list.
"""

''' Generator vectors '''
bl_idname = 'GenVectorsNode'
bl_label = 'Vector in'
sv_icon = 'SV_VECTOR_IN'
Expand All @@ -77,7 +70,7 @@ class GenVectorsNode(bpy.types.Node, SverchCustomTreeNode):
y_: FloatProperty(name='Y', description='Y', default=0.0, precision=3, update=updateNode)
z_: FloatProperty(name='Z', description='Z', default=0.0, precision=3, update=updateNode)


advanced_mode: BoolProperty(name='deep copy', update=updateNode, default=True)
show_3d_cursor_button: BoolProperty(name='show button', update=updateNode, default=False)
implementation_modes = [
("NumPy", "NumPy", "NumPy", 0),
Expand All @@ -87,21 +80,7 @@ class GenVectorsNode(bpy.types.Node, SverchCustomTreeNode):
name='Implementation', items=implementation_modes,
description='Choose calculation method',
default="Python", update=updateNode)
list_match: EnumProperty(
name="List Match",
description="Behavior on different list lengths",
items=numpy_list_match_modes, default="REPEAT",
update=updateNode)

correct_output_modes = [
('NONE', 'None', 'Leave at multi-object level (Advanced)', 0),
('FLAT', 'Flat Output', 'Flat to object level', 2),
]
correct_output: EnumProperty(
name="Simplify Output",
description="Behavior on different list lengths, object level",
items=correct_output_modes, default="FLAT",
update=updateNode)

output_numpy: BoolProperty(
name='Output NumPy',
description='Output NumPy arrays',
Expand All @@ -124,18 +103,15 @@ def draw_buttons(self, context, layout):
get_cursor.treename = self.id_data.name

def draw_buttons_ext(self, context, layout):
layout.row().prop(self, 'advanced_mode')
layout.label(text="Implementation:")
layout.prop(self, "implementation", expand=True)
layout.prop(self, 'list_match')
layout.prop(self, "output_numpy", toggle=False)
layout.prop(self, 'correct_output')


def rclick_menu(self, context, layout):
layout.prop(self, "advanced_mode", text="use deep copy")
layout.prop_menu_enum(self, "implementation", text="Implementation")
layout.prop_menu_enum(self, 'list_match')
layout.prop(self, 'output_numpy', toggle=True)
layout.prop_menu_enum(self, 'correct_output')
layout.prop(self, "output_numpy", toggle=True)
layout.prop(self, "show_3d_cursor_button", text="show 3d cursor button")
if not any(s.is_linked for s in self.inputs):
opname = 'node.sverchok_vector_from_cursor'
Expand All @@ -144,20 +120,23 @@ def rclick_menu(self, context, layout):
get_cursor.treename = self.id_data.name

def process(self):

if not self.outputs['Vectors'].is_linked:
return

params = [si.sv_get(default=[[]], deepcopy=False) for si in self.inputs]

matching_f = list_match_func[self.list_match]
desired_levels = [2, 2, 2]
ops = [self.output_numpy, self.list_match]
concatenate = 'APPEND' if self.correct_output == 'NONE' else "EXTEND"
inputs = self.inputs
X_ = inputs['X'].sv_get()
Y_ = inputs['Y'].sv_get()
Z_ = inputs['Z'].sv_get()
series_vec = []
max_obj = max(map(len, (X_, Y_, Z_)))
pack_func = numpy_pack_vecs if self.implementation == 'NumPy' else python_pack_vecs
result = recurse_f_level_control(params, ops, pack_func, matching_f, desired_levels, concatenate=concatenate)
fullList_main = fullList_deep_copy if self.advanced_mode else fullList
fullList_main(X_, max_obj)
fullList_main(Y_, max_obj)
fullList_main(Z_, max_obj)
series_vec = pack_func(X_, Y_, Z_, self.output_numpy)


self.outputs[0].sv_set(result)
self.outputs['Vectors'].sv_set(series_vec)


def register():
Expand Down
88 changes: 20 additions & 68 deletions nodes/vector/vector_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,77 +17,29 @@
# ##### END GPL LICENSE BLOCK #####

import bpy
from bpy.props import BoolProperty, EnumProperty
from bpy.props import BoolProperty
from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import dataCorrect_np, updateNode, levels_of_list_or_np
from sverchok.data_structure import dataCorrect_np, updateNode
from numpy import ndarray, array

def unpack_np(obj):
return (obj[:, 0], obj[:, 1], obj[:, 2])


def unpack_list(obj):
return (list(x) for x in zip(*obj))


def unpack_list_to_np(obj):
return (array(x) for x in zip(*obj))


def unpack(data, output_numpy):
X, Y, Z = [], [], []
if output_numpy:
unpack_func = unpack_np if isinstance(data[0], ndarray) else unpack_list_to_np
else:
unpack_func = unpack_list
for obj in data:
print(obj)
x_, y_, z_ = unpack_func(obj)
X.append(x_)
Y.append(y_)
Z.append(z_)
return X, Y, Z


def unpack_multilevel(data, level, output_numpy):
if level > 3:
X, Y, Z = [], [], []
for obj in data:

x,y,z = unpack_multilevel(obj, level-1, output_numpy)
X.append(x)
Y.append(y)
Z.append(z)
elif level < 3:
X, Y, Z = unpack_multilevel([data], level+1, output_numpy)
else:
X,Y,Z = unpack(data, output_numpy)

return X,Y,Z


class VectorsOutNode(bpy.types.Node, SverchCustomTreeNode):
"""
Triggers: Separate XYZ
Tooltip: Split X, Y and Z components from vector.
"""
''' Vectors out '''
bl_idname = 'VectorsOutNode'
bl_label = 'Vector out'
sv_icon = 'SV_VECTOR_OUT'

output_numpy: BoolProperty(
name='Output NumPy',
description='Output NumPy arrays',
default=False, update=updateNode)
correct_output_modes = [
('NONE', 'None', 'Leave at multi-object level (Advanced)', 0),
('FLAT', 'Flat Output', 'Flat to object level', 2),
]
correct_output: EnumProperty(
name="Simplify Output",
description="Behavior on different list lengths, object level",
items=correct_output_modes, default="FLAT",
update=updateNode)

def sv_init(self, context):
self.inputs.new('SvVerticesSocket', "Vectors")
Expand All @@ -98,28 +50,28 @@ def sv_init(self, context):

def draw_buttons_ext(self, ctx, layout):
layout.prop(self, "output_numpy", toggle=False)
layout.prop(self, "correct_output")

def rclick_menu(self, context, layout):
layout.prop(self, "output_numpy", toggle=True)
layout.prop_menu_enum(self, 'correct_output')

def process(self):
if not (self.inputs['Vectors'].is_linked and any(s.is_linked for s in self.outputs)):
return

vectors = self.inputs['Vectors'].sv_get(deepcopy=False)

if self.correct_output == 'FLAT':
data = dataCorrect_np(vectors)
xyz = unpack(data, self.output_numpy)
else:
input_level = levels_of_list_or_np(vectors)
xyz = unpack_multilevel(vectors, input_level, self.output_numpy)

for i, name in enumerate(['X', 'Y', 'Z']):
if self.outputs[name].is_linked:
self.outputs[name].sv_set(xyz[i])
if self.inputs['Vectors'].is_linked and any(s.is_linked for s in self.outputs):
xyz = self.inputs['Vectors'].sv_get(deepcopy=False)

data = dataCorrect_np(xyz)
X, Y, Z = [], [], []
if self.output_numpy:
unpack_func = unpack_np if isinstance(data[0], ndarray) else unpack_list_to_np
else:
unpack_func = unpack_list
for obj in data:
x_, y_, z_ = unpack_func(obj)
X.append(x_)
Y.append(y_)
Z.append(z_)
for i, name in enumerate(['X', 'Y', 'Z']):
if self.outputs[name].is_linked:
self.outputs[name].sv_set([X, Y, Z][i])


def register():
Expand Down

0 comments on commit 85e0643

Please sign in to comment.