forked from nortikin/sverchok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_tree.py
364 lines (292 loc) · 11.3 KB
/
node_tree.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# -*- coding: utf-8 -*-
# ##### 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 time
import bpy
from bpy.props import StringProperty, BoolProperty, FloatVectorProperty, IntProperty
from bpy.types import NodeTree, NodeSocket, NodeSocketStandard
from sverchok import data_structure
from sverchok.data_structure import (SvGetSocketInfo, SvGetSocket,
SvSetSocket, updateNode,
get_other_socket, SvNoDataError)
from sverchok.core.update_system import (build_update_list, process_from_node,
process_tree, get_update_lists,
update_error_nodes)
from sverchok.ui import color_def
sentinel = object()
def process_from_socket(self, context):
self.node.process_node(context)
# this property group is only used by the old viewer draw
class SvColors(bpy.types.PropertyGroup):
""" Class for colors CollectionProperty """
color = FloatVectorProperty(
name="svcolor", description="sverchok color",
default=(0.055, 0.312, 0.5), min=0, max=1,
step=1, precision=3, subtype='COLOR_GAMMA', size=3,
update=updateNode)
class MatrixSocket(NodeSocket):
'''4x4 matrix Socket_type'''
# ref: http://urchn.org/post/nodal-transform-experiment
bl_idname = "MatrixSocket"
bl_label = "Matrix Socket"
prop_name = StringProperty(default='')
def sv_get(self, default=sentinel, deepcopy=True):
if self.is_linked and not self.is_output:
return SvGetSocket(self, deepcopy)
elif default is sentinel:
raise SvNoDataError
else:
return default
def sv_set(self, data):
SvSetSocket(self, data)
def draw(self, context, layout, node, text):
if self.is_linked:
layout.label(text + '. ' + SvGetSocketInfo(self))
else:
layout.label(text)
def draw_color(self, context, node):
'''if self.is_linked:
return(.8,.3,.75,1.0)
else: '''
return(.2, .8, .8, 1.0)
class VerticesSocket(NodeSocket):
'''String Vertices - one string'''
bl_idname = "VerticesSocket"
bl_label = "Vertices Socket"
prop = FloatVectorProperty(default=(0, 0, 0), size=3, update=process_from_socket)
prop_name = StringProperty(default='')
use_prop = BoolProperty(default=False)
def sv_get(self, default=sentinel, deepcopy=True):
if self.is_linked and not self.is_output:
return SvGetSocket(self, deepcopy)
if self.prop_name:
return [[getattr(self.node, self.prop_name)[:]]]
elif self.use_prop:
return [[self.prop[:]]]
elif default is sentinel:
raise SvNoDataError
else:
return default
def sv_set(self, data):
SvSetSocket(self, data)
def draw(self, context, layout, node, text):
if not self.is_output and not self.is_linked:
if self.prop_name:
layout.template_component_menu(node, self.prop_name, name=self.name)
elif self.use_prop:
layout.template_component_menu(self, "prop", name=self.name)
else:
layout.label(text)
elif self.is_linked:
layout.label(text + '. ' + SvGetSocketInfo(self))
else:
layout.label(text)
def draw_color(self, context, node):
return(0.9, 0.6, 0.2, 1.0)
class StringsSocket(NodeSocketStandard):
'''String any type - one string'''
bl_idname = "StringsSocket"
bl_label = "Strings Socket"
prop_name = StringProperty(default='')
prop_type = StringProperty(default='')
prop_index = IntProperty()
def sv_get(self, default=sentinel, deepcopy=True):
if self.is_linked and not self.is_output:
return SvGetSocket(self, deepcopy)
elif self.prop_name:
return [[getattr(self.node, self.prop_name)]]
elif self.prop_type:
return [[getattr(self.node, self.prop_type)[self.prop_index]]]
elif default is not sentinel:
return default
else:
raise SvNoDataError
def sv_set(self, data):
SvSetSocket(self, data)
def draw(self, context, layout, node, text):
if self.prop_name:
if self.is_output:
t = text
msg = "Warning output socket: {name} in node: {node} has property attached"
print(msg.format(name=self.name, node=node.name))
else:
prop = node.rna_type.properties.get(self.prop_name, None)
t = prop.name if prop else text
else:
t = text
if not self.is_output and not self.is_linked:
if self.prop_name and not self.prop_type:
layout.prop(node, self.prop_name)
elif self.prop_type:
layout.prop(node, self.prop_type, index=self.prop_index, text=self.name)
else:
layout.label(t)
elif self.is_linked:
layout.label(t + '. ' + SvGetSocketInfo(self))
else:
layout.label(t)
def draw_color(self, context, node):
return(0.6, 1.0, 0.6, 1.0)
class SvNodeTreeCommon(object):
'''
Common methods shared between Sverchok node trees
'''
def build_update_list(self):
build_update_list(self)
def adjust_reroutes(self):
reroutes = [n for n in self.nodes if n.bl_idname == 'NodeReroute']
if not reroutes:
return
for n in reroutes:
s = n.inputs[0]
if s.links:
self.freeze(True)
other = get_other_socket(s)
s_type = other.bl_idname
if n.outputs[0].bl_idname != s_type:
out_socket = n.outputs.new(s_type, "Output")
in_sockets = [l.to_socket for l in n.outputs[0].links]
n.outputs.remove(n.outputs[0])
for i_s in in_sockets:
l = self.links.new(i_s, out_socket)
self.unfreeze(True)
def freeze(self, hard=False):
if hard:
self["don't update"] = 1
elif not self.is_frozen():
self["don't update"] = 0
def is_frozen(self):
return "don't update" in self
def unfreeze(self, hard=False):
if self.is_frozen():
if hard or self["don't update"] == 0:
del self["don't update"]
def get_update_lists(self):
return get_update_lists(self)
class SverchCustomTree(NodeTree, SvNodeTreeCommon):
''' Sverchok - architectural node programming of geometry in low level '''
bl_idname = 'SverchCustomTreeType'
bl_label = 'Sverchok Node Tree'
bl_icon = 'RNA'
def turn_off_ng(self, context):
process_tree(self)
#should turn off tree. for now it does by updating it whole
# should work something like this
# outputs = filter(lambda n: isinstance(n,SvOutput), self.nodes)
# for node in outputs:
# node.disable()
sv_animate = BoolProperty(name="Animate", default=True, description='Animate this layout')
sv_show = BoolProperty(name="Show", default=True, description='Show this layout',
update=turn_off_ng)
sv_bake = BoolProperty(name="Bake", default=True, description='Bake this layout')
sv_process = BoolProperty(name="Process", default=True, description='Process layout')
sv_user_colors = StringProperty(default="")
# get update list for debug info, tuple (fulllist,dictofpartiallists)
def update(self):
'''
Rebuild and update the Sverchok node tree, used at editor changes
'''
# startup safety net, a lot things will just break if this isn't
# stopped...
try:
l = bpy.data.node_groups[self.id_data.name]
except:
return
if self.is_frozen():
print("Skippiping update of {}".format(self.name))
return
self.adjust_reroutes()
self.build_update_list()
if self.sv_process:
process_tree(self)
def update_ani(self):
"""
Updates the Sverchok node tree if animation layers show true. For animation callback
"""
if self.sv_animate:
process_tree(self)
class SverchGroupTree(NodeTree, SvNodeTreeCommon):
''' Sverchok - groups '''
bl_idname = 'SverchGroupTreeType'
bl_label = 'Sverchok Group Node Tree'
bl_icon = 'NONE'
def update(self):
try:
l = bpy.data.node_groups[self.id_data.name]
except:
return
if self.is_frozen():
return
self.adjust_reroutes()
@classmethod
def poll(cls, context):
return False
class SverchCustomTreeNode:
@classmethod
def poll(cls, ntree):
return ntree.bl_idname in ['SverchCustomTreeType', 'SverchGroupTreeType']
def mark_error(self, err):
"""
marks the with system error color
will automaticly be cleared and restored to
the old color
"""
ng = self.id_data
update_error_nodes(ng, self.name, err)
def set_color(self):
color = color_def.get_color(self.bl_idname)
if color:
self.use_custom_color = True
self.color = color
def init(self, context):
ng = self.id_data
ng.freeze()
# color
if hasattr(self, "sv_init"):
self.sv_init(context)
self.set_color()
ng.unfreeze()
def process_node(self, context):
'''
Doesn't work as intended, inherited functions can't be used for bpy.props
update= ...
Still this is called from updateNode
'''
if self.id_data.is_frozen():
return
if data_structure.DEBUG_MODE:
a = time.perf_counter()
process_from_node(self)
b = time.perf_counter()
print("Partial update from node", self.name, "in", round(b-a, 4))
else:
process_from_node(self)
def register():
bpy.utils.register_class(SvColors)
bpy.utils.register_class(SverchCustomTree)
bpy.utils.register_class(SverchGroupTree)
bpy.utils.register_class(MatrixSocket)
bpy.utils.register_class(StringsSocket)
bpy.utils.register_class(VerticesSocket)
def unregister():
bpy.utils.unregister_class(VerticesSocket)
bpy.utils.unregister_class(StringsSocket)
bpy.utils.unregister_class(MatrixSocket)
bpy.utils.unregister_class(SverchCustomTree)
bpy.utils.unregister_class(SverchGroupTree)
bpy.utils.unregister_class(SvColors)