Skip to content

Commit 2329e78

Browse files
author
Leandro Inocencio
committed
Move example nodes to example_nodes folder and import from there.
Add 2 nodes TickTimeNode and TextFileInputNode. Update example_math_nodes.py.
1 parent f668a62 commit 2329e78

File tree

6 files changed

+229
-159
lines changed

6 files changed

+229
-159
lines changed

NodeGraphQt/base/node.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,8 @@ def update_streams(self, *args):
787787

788788
while nodes:
789789
node = nodes.pop()
790+
if node.disabled():
791+
continue
790792
if node not in trash:
791793
trash.append(node)
792794

example_math_nodes.py

Lines changed: 15 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,12 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
3-
import math
4-
import inspect
5-
from functools import partial
6-
7-
# add basic math functions to math library
8-
math.add = lambda x, y: x + y
9-
math.sub = lambda x, y: x - y
10-
math.mul = lambda x, y: x * y
11-
math.div = lambda x, y: x / y
12-
13-
# Transform float to functions
14-
for constant in ['pi', 'e', 'tau', 'inf', 'nan']:
15-
setattr(math, constant, partial(lambda x: x, getattr(math, constant)))
16-
17-
3+
import example_nodes as Nodes
184
from NodeGraphQt import (NodeGraph,
195
BaseNode,
206
setup_context_menu)
217
from NodeGraphQt import QtWidgets, QtCore, PropertiesBinWidget, NodeTreeWidget
228

239

24-
class DataInputNode(BaseNode):
25-
"""
26-
Input node data.
27-
"""
28-
29-
__identifier__ = 'com.chantasticvfx'
30-
NODE_NAME = 'Input Numbers'
31-
32-
def __init__(self):
33-
super(DataInputNode, self).__init__()
34-
self.add_output('out')
35-
self.add_text_input('out', 'Data Output', text='0.4', tab='widgets')
36-
self.view.widgets['out'].value_changed.connect(self.update_streams)
37-
38-
39-
class MathFunctionsNode(BaseNode):
40-
"""
41-
Math functions node.
42-
"""
43-
44-
# set a unique node identifier.
45-
__identifier__ = 'com.chantasticvfx'
46-
47-
# set the initial default node name.
48-
NODE_NAME = 'Functions node'
49-
50-
mathFuncs = [func for func in dir(math) if not func.startswith('_')]
51-
52-
def __init__(self):
53-
super(MathFunctionsNode, self).__init__()
54-
self.set_color(25, 58, 51)
55-
self.add_combo_menu('funcs', 'Functions', items=self.mathFuncs,
56-
tab='widgets')
57-
58-
# switch math function type
59-
self.view.widgets['funcs'].value_changed.connect(self.addFunction)
60-
self.view.widgets['funcs'].value_changed.connect(self.update_streams)
61-
self.add_output('output')
62-
self.create_property('output', None)
63-
self.trigger_type = 'no_inPorts'
64-
65-
self.view.widgets['funcs'].widget.setCurrentIndex(2)
66-
67-
def addFunction(self, prop, func):
68-
"""
69-
Create inputs based on math functions arguments.
70-
"""
71-
self.func = getattr(math, func)
72-
dataFunc = inspect.getfullargspec(self.func)
73-
74-
for arg in dataFunc.args:
75-
if not self.has_property(arg):
76-
inPort = self.add_input(arg)
77-
inPort.trigger = True
78-
self.create_property(arg, None)
79-
80-
for inPort in self._inputs:
81-
if inPort.name() in dataFunc.args:
82-
if not inPort.visible():
83-
inPort.set_visible(True)
84-
else:
85-
inPort.set_visible(False)
86-
87-
def run(self):
88-
"""
89-
Evaluate all entries, pass them as arguments of the
90-
chosen mathematical function.
91-
"""
92-
for to_port in self.input_ports():
93-
if to_port.visible() == False:
94-
continue
95-
from_ports = to_port.connected_ports()
96-
if not from_ports:
97-
raise Exception('Port %s not connected!' % to_port.name(),
98-
to_port)
99-
100-
for from_port in from_ports:
101-
from_port.node().run()
102-
data = from_port.node().get_property(from_port.name())
103-
self.set_property(to_port.name(), float(data))
104-
105-
try:
106-
# Execute math function with arguments.
107-
data = self.func(*[self.get_property(inport.name()) for inport in self._inputs if inport.visible()])
108-
109-
self.set_property('output', data)
110-
except KeyError as error:
111-
print("An input is missing! %s" % str(error))
112-
except TypeError as error:
113-
print("Error evaluating function: %s" % str(error))
114-
115-
def on_input_connected(self, to_port, from_port):
116-
"""Override node callback method."""
117-
self.set_property(to_port.name(), from_port.node().run())
118-
self.update_streams()
119-
120-
def on_input_disconnected(self, to_port, from_port):
121-
"""Override node callback method."""
122-
self.set_property('output', None)
123-
self.update_streams()
124-
125-
126-
class DataViewerNode(BaseNode):
127-
__identifier__ = 'com.chantasticvfx'
128-
NODE_NAME = 'Result View'
129-
130-
def __init__(self):
131-
super(DataViewerNode, self).__init__()
132-
self.inPort = self.add_input('data')
133-
self.add_text_input('data', 'Data Viewer', tab='widgets')
134-
135-
def run(self):
136-
"""Evaluate input to show it."""
137-
for source in self.inPort.connected_ports():
138-
from_node = source.node()
139-
try:
140-
from_node.run()
141-
except Exception as error:
142-
print("%s no inputs connected: %s" % (from_node.name(), str(error)))
143-
self.set_property('data', None)
144-
return
145-
value = from_node.get_property(source.name())
146-
self.set_property('data', str(value))
147-
148-
def on_input_connected(self, to_port, from_port):
149-
"""Override node callback method"""
150-
self.run()
151-
152-
def on_input_disconnected(self, to_port, from_port):
153-
"""Override node callback method"""
154-
self.set_property('data', None)
155-
156-
15710
if __name__ == '__main__':
15811
app = QtWidgets.QApplication([])
15912

@@ -187,38 +40,41 @@ def show_nodes_list(node):
18740
graph.node_double_clicked.connect(show_nodes_list)
18841

18942
# registered nodes.
190-
reg_nodes = [DataInputNode, DataViewerNode, MathFunctionsNode]
43+
reg_nodes = [
44+
Nodes.__dict__[n] for n in Nodes.__dict__
45+
if hasattr(Nodes.__dict__[n], 'NODE_NAME')
46+
]
19147

19248
for n in reg_nodes:
19349
graph.register_node(n)
19450

195-
mathNodeA = graph.create_node('com.chantasticvfx.MathFunctionsNode',
51+
mathNodeA = graph.create_node('Math.MathFunctionsNode',
19652
name='Math Functions A',
19753
color='#0a1e20',
19854
text_color='#feab20',
19955
pos=[-250, 70])
20056

201-
mathNodeB = graph.create_node('com.chantasticvfx.MathFunctionsNode',
57+
mathNodeB = graph.create_node('Math.MathFunctionsNode',
20258
name='Math Functions B',
20359
color='#0a1e20',
20460
text_color='#feab20',
20561
pos=[-250, -70])
20662

207-
mathNodeC = graph.create_node('com.chantasticvfx.MathFunctionsNode',
208-
name='Math Functions C',
209-
color='#0a1e20',
210-
text_color='#feab20',
211-
pos=[0, 0])
63+
mathNodeC = graph.create_node('Math.MathFunctionsNode',
64+
name='Math Functions C',
65+
color='#0a1e20',
66+
text_color='#feab20',
67+
pos=[0, 0])
21268

213-
inputANode = graph.create_node('com.chantasticvfx.DataInputNode',
69+
inputANode = graph.create_node('Inputs.DataInputNode',
21470
name='Input A',
21571
pos=[-500, -50])
21672

217-
inputBNode = graph.create_node('com.chantasticvfx.DataInputNode',
73+
inputBNode = graph.create_node('Inputs.DataInputNode',
21874
name='Input B',
21975
pos=[-500, 50])
22076

221-
outputNode = graph.create_node('com.chantasticvfx.DataViewerNode',
77+
outputNode = graph.create_node('Viewers.DataViewerNode',
22278
name='Output',
22379
pos=[250, 0])
22480

example_nodes/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,10 @@
2525
# param = info[1]
2626
# tb = info[2]
2727
# traceback.print_exception(type, param, tb, file=sys.stderr)
28+
29+
# Manually loading nodes
30+
from .math_node import MathFunctionsNode
31+
from .input_nodes import (DataInputNode, TickTimeNode, TextFileInputNode)
32+
from .viewer_nodes import DataViewerNode
33+
from .basic_nodes import (FooNode, BarNode)
34+
from .widget_nodes import (DropdownMenuNode, TextInputNode, CheckboxNode)

example_nodes/input_nodes.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from NodeGraphQt import BaseNode, QtCore
2+
3+
4+
class DataInputNode(BaseNode):
5+
"""
6+
Input node data.
7+
"""
8+
9+
__identifier__ = 'Inputs'
10+
NODE_NAME = 'Basic Input'
11+
12+
def __init__(self):
13+
super(DataInputNode, self).__init__()
14+
self.add_output('out')
15+
self.add_text_input('out', 'Data Output', text='0.4', tab='widgets')
16+
self.view.widgets['out'].value_changed.connect(self.update_streams)
17+
18+
19+
class TickTimeNode(BaseNode):
20+
"""
21+
Every second that passes adds a tick.
22+
"""
23+
24+
__identifier__ = 'Inputs'
25+
NODE_NAME = 'Tick Time'
26+
27+
def __init__(self):
28+
super(TickTimeNode, self).__init__()
29+
self.add_output('out')
30+
self.add_text_input('out', 'Data Input', text='0', tab='widgets')
31+
self.view.widgets['out'].value_changed.connect(self.update_streams)
32+
33+
self.timer = QtCore.QTimer()
34+
self.timer.timeout.connect(self.tick)
35+
self.timer.start(1000)
36+
37+
def tick(self):
38+
if not self.disabled():
39+
current = int(self.get_property('out'))
40+
current += 1
41+
self.view.widgets['out'].value_changed.emit('out', str(current))
42+
43+
44+
class TextFileInputNode(BaseNode):
45+
"""
46+
Text File Input node data.
47+
"""
48+
49+
__identifier__ = 'Inputs'
50+
NODE_NAME = 'Text File'
51+
52+
def __init__(self):
53+
super(TextFileInputNode, self).__init__()
54+
self.add_output('out')
55+
self.create_property('out', None)
56+
self.add_text_input('path', 'Text File Path', text='', tab='widgets')
57+
self.view.widgets['path'].value_changed.connect(self.update_streams)
58+
59+
def run(self):
60+
if not self.disabled():
61+
path = self.get_property('path')
62+
if os.path.exists(path):
63+
with open(path, 'r') as fread:
64+
data = fread.read()
65+
self.set_property('output', data)
66+
else:
67+
print('No existe %s' % path)
68+
self.set_property('output', '')

0 commit comments

Comments
 (0)