forked from nortikin/sverchok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_ListFunc.py
92 lines (73 loc) · 2.79 KB
/
node_ListFunc.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
import bpy
from mathutils import Vector, Matrix
from node_s import *
from util import *
class ListFuncNode(Node, SverchCustomTreeNode):
''' List function '''
bl_idname = 'ListFuncNode'
bl_label = 'List function '
bl_icon = 'OUTLINER_OB_EMPTY'
mode_items = [
("MIN", "Minimum", "",1),
("MAX", "Maximum", "",2),
("AVR", "Average", "",3),
]
func_=bpy.props.EnumProperty( items = mode_items, name="Function",
description="Function choice", default="AVR", update=updateNode)
level = bpy.props.IntProperty(name = 'level_to_count', default=1, min=0, update=updateNode)
# typ = bpy.props.StringProperty(name='typ', default='')
# newsock = bpy.props.BoolProperty(name='newsock', default=False)
def draw_buttons(self, context, layout):
layout.prop(self, "level", text="level")
layout.prop(self,"func_","Functions:")
def init(self, context):
self.inputs.new('StringsSocket', "Data", "Data")
self.outputs.new('StringsSocket',"Function","Function")
def update(self):
if 'Function' in self.outputs and self.outputs['Function'].links:
if 'Data' in self.inputs and self.inputs['Data'].links:
data = SvGetSocketAnyType(self, self.inputs['Data'])
if self.func_=='MIN':
func=min
elif self.func_=='MAX':
func=max
else:
func=self.avr
if not self.level:
out = [func(data)]
else:
out = self.count(data, self.level, func)
SvSetSocketAnyType(self, 'Function', [out])
def count(self, data, level, func):
out = []
if level:
for obj in data:
out.append(self.count(obj, level-1, func))
elif type(data) in [list, tuple] and len(data)>0:
if len(data)==1:
data.extend(data)
out = func(data)
else:
pass
return out
def avr(self, data):
sum_d = 0.0
flag = True
for d in data:
if type(d) not in [float, int]:
idx_avr = len(data)//2
result = data[idx_avr]
flag = False
break
sum_d += d
if flag:
result = sum_d / len(data)
return result
def update_socket(self, context):
self.update()
def register():
bpy.utils.register_class(ListFuncNode)
def unregister():
bpy.utils.unregister_class(ListFuncNode)
if __name__ == "__main__":
register()