Skip to content

Commit 5ae89ca

Browse files
authored
Merge pull request #149 from cesioarg/master
Fix port visibility bug + update math node example
2 parents 2e6b760 + 9990b92 commit 5ae89ca

File tree

4 files changed

+55
-46
lines changed

4 files changed

+55
-46
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# virtual enviroment
2+
env/*
3+
14
# sphinx documentation
25
docs/_build/
36
api

NodeGraphQt/base/commands.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def set_visible(self, visible):
336336
node_view.post_init()
337337

338338
def undo(self):
339-
self.set_visible(self.visible)
340-
341-
def redo(self):
342339
self.set_visible(not self.visible)
340+
341+
def redo(self):
342+
self.set_visible(self.visible)

NodeGraphQt/base/port.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,13 @@ def set_visible(self, visible=True):
104104
Args:
105105
visible (bool): true if visible.
106106
"""
107+
self.model.visible = visible
107108
label = 'show' if visible else 'hide'
108109
undo_stack = self.node().graph.undo_stack()
109110
undo_stack.beginMacro('{} port {}'.format(label, self.name()))
110111

111-
connected_ports = self.connected_ports()
112-
if connected_ports:
113-
for port in connected_ports:
114-
undo_stack.push(PortDisconnectedCmd(self, port))
112+
for port in self.connected_ports():
113+
undo_stack.push(PortDisconnectedCmd(self, port))
115114

116115
undo_stack.push(PortVisibleCmd(self))
117116
undo_stack.endMacro()

example_math_nodes.py

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33
import os
4+
import math
45
import inspect
56
from functools import partial
67

8+
# add basic math functions to math library
9+
math.add = lambda x, y: x + y
10+
math.sub = lambda x, y: x - y
11+
math.mul = lambda x, y: x * y
12+
math.div = lambda x, y: x / y
13+
14+
# Transform float to functions
15+
for constant in ['pi', 'e', 'tau', 'inf', 'nan']:
16+
setattr(math, constant, partial(lambda x: x, getattr(math, constant)))
17+
18+
719
from NodeGraphQt import (NodeGraph,
820
BaseNode,
921
setup_context_menu)
1022
from NodeGraphQt import QtWidgets, QtCore, PropertiesBinWidget, NodeTreeWidget
1123

1224

13-
def update_streams(node):
25+
def update_streams(node, *args):
1426
"""
1527
Update all nodes joined by pipes
1628
"""
@@ -46,33 +58,13 @@ class DataInputNode(BaseNode):
4658
def __init__(self):
4759
super(DataInputNode, self).__init__()
4860
self.output = self.add_output('out')
49-
self.add_text_input('out', 'Data Input', text='4', tab='widgets')
61+
self.add_text_input('out', 'Data Input', text='0.4', tab='widgets')
5062
self.view.widgets['out'].value_changed.connect(partial(update_streams, self))
51-
63+
5264
def run(self):
5365
return
5466

5567

56-
def add(a, b):
57-
return a + b
58-
59-
60-
def sub(a, b):
61-
return a - b
62-
63-
64-
def mul(a, b):
65-
return a * b
66-
67-
68-
def div(a, b):
69-
return a / b
70-
71-
72-
# create a dict with all function
73-
funcs = {'add': add, 'sub': sub, 'mul': mul, 'div': div}
74-
75-
7668
class MathFunctionsNode(BaseNode):
7769
"""
7870
Math functions node.
@@ -85,51 +77,66 @@ class MathFunctionsNode(BaseNode):
8577
# set the initial default node name.
8678
NODE_NAME = 'Functions node'
8779

80+
mathFuncs = [func for func in dir(math) if not func.startswith('_')]
81+
8882
def __init__(self):
8983
super(MathFunctionsNode, self).__init__()
9084
self.set_color(25, 58, 51)
91-
self.add_combo_menu('functions', 'Functions',
92-
items=funcs.keys(), tab='widgets')
85+
self.add_combo_menu('functions', 'Functions', items=self.mathFuncs,
86+
tab='widgets')
87+
9388
# switch math function type
9489
self.view.widgets['functions'].value_changed.connect(self.addFunction)
95-
self.set_property('functions', 'add')
96-
self.view.widgets['functions'].value_changed.connect(
97-
partial(update_streams, self))
90+
update = partial(update_streams, self)
91+
self.view.widgets['functions'].value_changed.connect(update)
9892
self.output = self.add_output('output')
9993
self.create_property(self.output.name(), None)
94+
self.trigger_type = 'no_inPorts'
95+
96+
self.view.widgets['functions'].widget.setCurrentIndex(2)
10097

10198
def addFunction(self, prop, func):
10299
"""
103100
Create inputs based on math functions arguments.
104101
"""
105-
self.func = funcs[func] # add, sub, mul, div
106-
107-
dataFunc = inspect.getargspec(self.func)
102+
self.func = getattr(math, func)
103+
dataFunc = inspect.getfullargspec(self.func)
108104

109105
for arg in dataFunc.args:
110106
if not self.has_property(arg):
111-
self.add_input(arg)
107+
inPort = self.add_input(arg)
108+
inPort.trigger = True
112109
self.create_property(arg, None)
113110

111+
for inPort in self._inputs:
112+
if inPort.name() in dataFunc.args:
113+
if not inPort.visible():
114+
inPort.set_visible(True)
115+
else:
116+
inPort.set_visible(False)
117+
114118
def run(self):
115119
"""
116120
Evaluate all entries, pass them as arguments of the
117121
chosen mathematical function.
118122
"""
119123
for to_port in self.input_ports():
124+
if to_port.visible() == False:
125+
continue
120126
from_ports = to_port.connected_ports()
121127
if not from_ports:
122-
raise Exception('Port %s not connected!' % to_port.name())
128+
raise Exception('Port %s not connected!' % to_port.name(),
129+
to_port)
123130

124131
for from_port in from_ports:
125132
from_port.node().run()
126133
data = from_port.node().get_property(from_port.name())
127-
self.set_property(to_port.name(), int(data))
134+
self.set_property(to_port.name(), float(data))
128135

129136
try:
130137
# Execute math function with arguments.
131-
output = self.func(*[self.get_property(prop)
132-
for prop in self.properties()['inputs']])
138+
output = self.func(*[self.get_property(inport.name()) for inport in self._inputs if inport.visible()])
139+
133140
self.set_property('output', output)
134141
except KeyError as error:
135142
print("An input is missing! %s" % str(error))
@@ -216,19 +223,19 @@ def show_nodes_list(node):
216223
for n in reg_nodes:
217224
graph.register_node(n)
218225

219-
my_node = graph.create_node('com.chantasticvfx.MathFunctionsNode',
226+
mathNodeA = graph.create_node('com.chantasticvfx.MathFunctionsNode',
220227
name='Math Functions A',
221228
color='#0a1e20',
222229
text_color='#feab20',
223230
pos=[-250, 70])
224231

225-
my_node = graph.create_node('com.chantasticvfx.MathFunctionsNode',
232+
mathNodeB = graph.create_node('com.chantasticvfx.MathFunctionsNode',
226233
name='Math Functions B',
227234
color='#0a1e20',
228235
text_color='#feab20',
229236
pos=[-250, -70])
230237

231-
my_node = graph.create_node('com.chantasticvfx.MathFunctionsNode',
238+
mathNodeC = graph.create_node('com.chantasticvfx.MathFunctionsNode',
232239
name='Math Functions C',
233240
color='#0a1e20',
234241
text_color='#feab20',

0 commit comments

Comments
 (0)