forked from nortikin/sverchok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_ListRangeFloat.py
125 lines (98 loc) · 3.69 KB
/
node_ListRangeFloat.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
import bpy
from node_s import *
from util import *
from bpy.props import IntProperty, EnumProperty, FloatProperty
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] '''
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:
step=1
for i in range(int(count)):
yield start
start += step
class SvGenFloatRange(Node, SverchCustomTreeNode):
''' Generator range list of floats'''
bl_idname = 'SvGenFloatRange'
bl_label = 'Float Series'
bl_icon = 'OUTLINER_OB_EMPTY'
start_ = FloatProperty(
name='start', description='start', default=0,
options={'ANIMATABLE'}, update=updateNode)
stop_ = FloatProperty(
name='stop', description='stop', default=10,
options={'ANIMATABLE'}, update=updateNode)
count_ = IntProperty(
name='count', description='num items', default=10,
options={'ANIMATABLE'}, update=updateNode)
step_ = FloatProperty(
name='step', description='step', default=1.0,
options={'ANIMATABLE'}, 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 init(self, context):
self.inputs.new('StringsSocket', "Start").prop_name = 'start_'
self.inputs.new('StringsSocket', "Step").prop_name = 'stop_'
self.inputs.new('StringsSocket', "Stop").prop_name = 'step_'
self.outputs.new('StringsSocket', "Range", "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 update(self):
inputs = self.inputs
outputs = self.outputs
# outputs, end early.
if not 'Range' in outputs or not outputs['Range'].links:
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))]
self.outputs['Range'].sv_set(out)
def register():
bpy.utils.register_class(SvGenFloatRange)
def unregister():
bpy.utils.unregister_class(SvGenFloatRange)