-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathlevels.py
162 lines (136 loc) · 5.66 KB
/
levels.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# ##### 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, IntProperty, StringProperty, CollectionProperty, BoolVectorProperty
from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import updateNode, describe_data_shape_by_level, list_levels_adjust, SIMPLE_DATA_TYPES,\
changable_sockets
from sverchok.utils.curve.core import SvCurve
from sverchok.utils.surface.core import SvSurface
from sverchok.dependencies import FreeCAD
from sverchok.utils.sv_logging import sv_logger
ALL_TYPES = SIMPLE_DATA_TYPES + (SvCurve, SvSurface)
if FreeCAD is not None:
import Part
ALL_TYPES = ALL_TYPES + (Part.Shape,)
class SvNestingLevelEntry(bpy.types.PropertyGroup):
def update_entry(self, context):
if hasattr(context, 'node'):
updateNode(context.node, context)
else:
sv_logger.info("Node is not defined in this context, so will not update the node.")
description : StringProperty(options = {'SKIP_SAVE'}, default="?")
flatten : BoolProperty(
name = "Flatten",
description = "Concatenate all child lists into one list",
default=False,
update=update_entry)
wrap : BoolProperty(
name = "Wrap",
description = "Wrap data into additional pair of square brackets []",
default=False,
update=update_entry)
class SvListLevelsNode(SverchCustomTreeNode, bpy.types.Node):
'''
Triggers: List Levels
Tooltip: List nesting levels manipulation
'''
bl_idname = 'SvListLevelsNode'
bl_label = 'List Levels'
bl_icon = 'OUTLINER'
replacement_nodes = [('SvListLevelsNodeMK2', None, None)]
levels_config : CollectionProperty(type=SvNestingLevelEntry)
prev_nesting_level : IntProperty(default = 0, options = {'SKIP_SAVE'})
flatten_mem: BoolVectorProperty(size=32, default=[False for i in range(32)])
wrap_mem: BoolVectorProperty(size=32, default=[False for i in range(32)])
def draw_buttons(self, context, layout):
n = len(self.levels_config)
if not n:
layout.label(text="No data passed")
return
grid = layout.grid_flow(row_major=True, columns=5, align=True)
grid.label(text='Depth')
grid.label(text='Nesting')
grid.label(text='Shape')
grid.label(text='Flatten')
grid.label(text='Wrap')
for i, entry in enumerate(self.levels_config):
nesting = n-i-1
level_str = str(i)
if i == 0:
level_str += " (outermost)"
elif nesting == 0:
level_str += " (innermost)"
grid.label(text=level_str)
grid.label(text=str(nesting))
grid.label(text=entry.description)
if nesting < 2:
grid.label(icon='X', text='')
else:
grid.prop(entry, 'flatten', text='')
grid.prop(entry, 'wrap', text='')
def sv_update(self):
self.update_buttons(False)
def update_buttons(self, update_during_process):
try:
data = self.inputs['Data'].sv_get(default=[])
except LookupError:
data = []
if not data:
self.prev_nesting_level = 0
for i, l in enumerate(self.levels_config):
self.flatten_mem[i] = l.flatten
self.wrap_mem[i] = l.wrap
self.levels_config.clear()
return
changable_sockets(self, 'Data', ['Data', ])
nesting, descriptions = describe_data_shape_by_level(data, include_numpy_nesting=False)
rebuild_list = self.prev_nesting_level != nesting
self.prev_nesting_level = nesting
if rebuild_list:
self.levels_config.clear()
for i, descr in enumerate(descriptions):
l = self.levels_config.add()
l.description = descr
l.flatten = self.flatten_mem[i]
l.wrap = self.wrap_mem[i]
else:
for entry, descr in zip(self.levels_config, descriptions):
entry.description = descr
def sv_init(self, context):
self.width = 300
self.inputs.new('SvStringsSocket', 'Data')
self.outputs.new('SvStringsSocket', 'Data')
def sv_copy(self, original):
self.prev_nesting_level = 0
def process(self):
if not self.inputs['Data'].is_linked:
return
self.update_buttons(True)
if not self.outputs['Data'].is_linked:
return
data = self.inputs['Data'].sv_get(default=[], deepcopy=False)
result = list_levels_adjust(data, self.levels_config, data_types=ALL_TYPES)
self.outputs['Data'].sv_set(result)
classes = [SvNestingLevelEntry, SvListLevelsNode]
def register():
for name in classes:
bpy.utils.register_class(name)
def unregister():
for name in reversed(classes):
bpy.utils.unregister_class(name)