-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathrange_float.py
154 lines (124 loc) · 4.69 KB
/
range_float.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
# ##### 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, EnumProperty, FloatProperty, StringProperty
from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import updateNode, match_long_repeat
def frange(start, stop, step):
'''Behaves like range but for floats'''
if start == stop:
stop += 1
step = max(1e-5, abs(step))
if start < stop:
while start < stop:
yield start
start += step
else:
step = -abs(step)
while start > stop:
yield start
start += step
def frange_count(start, stop, count):
''' Gives count total values in [start,stop] '''
# we are casting to int here because the input can be floats.
if count < 2:
yield start
elif start == stop:
for i in range(int(count)):
yield start
else:
count = int(count)
step = (stop - start) / (count - 1)
yield start
for i in range(count - 2):
start += step
yield start
yield stop
def frange_step(start, step, count):
''' Gives count values with step from start'''
if abs(step) < 1e-5:
for i in range(int(count)):
yield start
else:
for i in range(int(count)):
yield start
start += step
class SvGenFloatRange(SverchCustomTreeNode, bpy.types.Node):
''' Generator range list of floats'''
bl_idname = 'SvGenFloatRange'
bl_label = 'Range Float'
bl_icon = 'IPO_LINEAR'
replacement_nodes = [('SvGenNumberRange', None, None)]
start_: FloatProperty(
name='start', description='start',
default=0, update=updateNode)
stop_: FloatProperty(
name='stop', description='stop',
default=10, update=updateNode)
count_: IntProperty(
name='count', description='num items',
default=10, min=1, update=updateNode)
step_: FloatProperty(
name='step', description='step',
default=1.0, update=updateNode)
current_mode: StringProperty(default="FRANGE")
modes = [
("FRANGE", "Range", "Series based frange like function", 1),
("FRANGE_COUNT", "Count", "Create series based on count", 2),
("FRANGE_STEP", "Step", "Create range based step and count", 3),
]
def mode_change(self, context):
# just because click doesn't mean we need to change mode
mode = self.mode
if mode == self.current_mode:
return
if mode == 'FRANGE':
self.inputs[1].prop_name = 'stop_'
self.inputs[2].prop_name = 'step_'
elif mode == 'FRANGE_COUNT':
self.inputs[1].prop_name = 'stop_'
self.inputs[2].prop_name = 'count_'
else:
self.inputs[1].prop_name = 'step_'
self.inputs[2].prop_name = 'count_'
self.current_mode = mode
updateNode(self, context)
mode: EnumProperty(items=modes, default='FRANGE', update=mode_change)
def sv_init(self, context):
self.inputs.new('SvStringsSocket', "Start").prop_name = 'start_'
self.inputs.new('SvStringsSocket', "Step").prop_name = 'stop_'
self.inputs.new('SvStringsSocket', "Stop").prop_name = 'step_'
self.outputs.new('SvStringsSocket', "Range")
def draw_buttons(self, context, layout):
layout.prop(self, "mode", expand=True)
func_dict = {'FRANGE': frange,
'FRANGE_COUNT': frange_count,
'FRANGE_STEP': frange_step}
def process(self):
inputs = self.inputs
outputs = self.outputs
if not outputs[0].is_linked:
return
param = [inputs[i].sv_get()[0] for i in range(3)]
f = self.func_dict[self.mode]
out = [list(f(*args)) for args in zip(*match_long_repeat(param))]
outputs['Range'].sv_set(out)
def register():
bpy.utils.register_class(SvGenFloatRange)
def unregister():
bpy.utils.unregister_class(SvGenFloatRange)