Skip to content

Commit 1191a57

Browse files
author
Leandro Inocencio
committed
add boolean logics node
1 parent c72c08c commit 1191a57

File tree

2 files changed

+109
-174
lines changed

2 files changed

+109
-174
lines changed

example.nodes

Lines changed: 39 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,6 @@
11
{
22
"nodes":{
3-
"0x1508f548":{
4-
"type_":"Inputs.DataInputNode",
5-
"icon":null,
6-
"name":"Input A",
7-
"color":[
8-
13,
9-
18,
10-
23,
11-
255
12-
],
13-
"border_color":[
14-
74,
15-
84,
16-
85,
17-
255
18-
],
19-
"text_color":[
20-
255,
21-
255,
22-
255,
23-
180
24-
],
25-
"disabled":false,
26-
"selected":true,
27-
"width":173.0,
28-
"height":91.0,
29-
"pos":[
30-
-417.0,
31-
-160.0
32-
],
33-
"custom":{
34-
"out":"Hola"
35-
}
36-
},
37-
"0x160ea048":{
38-
"type_":"Inputs.DataInputNode",
39-
"icon":null,
40-
"name":"Input B",
41-
"color":[
42-
13,
43-
18,
44-
23,
45-
255
46-
],
47-
"border_color":[
48-
74,
49-
84,
50-
85,
51-
255
52-
],
53-
"text_color":[
54-
255,
55-
255,
56-
255,
57-
180
58-
],
59-
"disabled":false,
60-
"selected":true,
61-
"width":173.0,
62-
"height":91.0,
63-
"pos":[
64-
-390.0,
65-
-23.0
66-
],
67-
"custom":{
68-
"out":"Chau"
69-
}
70-
},
71-
"0x160ea748":{
3+
"0x15095e08":{
724
"type_":"Viewers.DataViewerNode",
735
"icon":null,
746
"name":"Output",
@@ -95,17 +27,17 @@
9527
"width":170,
9628
"height":91.0,
9729
"pos":[
98-
186.0,
99-
-231.0
30+
149.0,
31+
-146.0
10032
],
10133
"custom":{
102-
"data":"Hola"
34+
"data":""
10335
}
10436
},
105-
"0x160f3688":{
106-
"type_":"Logics.ifNode",
37+
"0x15126848":{
38+
"type_":"Inputs.BoolInputNode",
10739
"icon":null,
108-
"name":"If",
40+
"name":"Bool",
10941
"color":[
11042
13,
11143
18,
@@ -127,57 +59,20 @@
12759
"disabled":false,
12860
"selected":true,
12961
"width":170,
130-
"height":108.0,
131-
"pos":[
132-
-75.0,
133-
-178.0
134-
],
135-
"custom":{
136-
"condition":0.0,
137-
"out":"Hola"
138-
}
139-
},
140-
"0x160fad08":{
141-
"type_":"Math.MathFunctionsNode",
142-
"icon":null,
143-
"name":"Math Functions",
144-
"color":[
145-
25,
146-
58,
147-
51,
148-
255
149-
],
150-
"border_color":[
151-
74,
152-
84,
153-
85,
154-
255
155-
],
156-
"text_color":[
157-
255,
158-
255,
159-
255,
160-
180
161-
],
162-
"disabled":false,
163-
"selected":true,
164-
"width":181.0,
16562
"height":103.0,
16663
"pos":[
167-
-411.0,
168-
-319.0
64+
-413.0,
65+
-212.0
16966
],
17067
"custom":{
171-
"funcs":"fmod",
172-
"output":1.0,
173-
"x":219.0,
174-
"y":2.0
68+
"out":true,
69+
"combo":"True"
17570
}
17671
},
177-
"0x17678bc8":{
178-
"type_":"Inputs.TickTimeNode",
72+
"0x15140308":{
73+
"type_":"Logics.BooleanNode",
17974
"icon":null,
180-
"name":"Tick Time",
75+
"name":"Boolean",
18176
"color":[
18277
13,
18378
18,
@@ -198,20 +93,21 @@
19893
],
19994
"disabled":false,
20095
"selected":true,
201-
"width":173.0,
202-
"height":91.0,
96+
"width":170,
97+
"height":103.0,
20398
"pos":[
204-
-678.0,
205-
-393.0
99+
-143.0,
100+
-149.0
206101
],
207102
"custom":{
208-
"out":"219"
103+
"out":null,
104+
"funcs":"and"
209105
}
210106
},
211-
"0x1768efc8":{
212-
"type_":"Inputs.DataInputNode",
107+
"0x1514a4c8":{
108+
"type_":"Inputs.BoolInputNode",
213109
"icon":null,
214-
"name":"Basic Input",
110+
"name":"Bool 1",
215111
"color":[
216112
13,
217113
18,
@@ -232,75 +128,46 @@
232128
],
233129
"disabled":false,
234130
"selected":true,
235-
"width":173.0,
236-
"height":91.0,
131+
"width":170,
132+
"height":103.0,
237133
"pos":[
238-
-678.0,
239-
-207.0
134+
-411.25,
135+
-61.5
240136
],
241137
"custom":{
242-
"out":"2"
138+
"out":true,
139+
"combo":"True"
243140
}
244141
}
245142
},
246143
"connections":[
247-
{
248-
"out":[
249-
"0x1508f548",
250-
"out"
251-
],
252-
"in":[
253-
"0x160f3688",
254-
"then"
255-
]
256-
},
257-
{
258-
"out":[
259-
"0x160ea048",
260-
"out"
261-
],
262-
"in":[
263-
"0x160f3688",
264-
"else"
265-
]
266-
},
267144
{
268145
"in":[
269-
"0x160ea748",
146+
"0x15095e08",
270147
"data"
271148
],
272149
"out":[
273-
"0x160f3688",
150+
"0x15140308",
274151
"out"
275152
]
276153
},
277154
{
278-
"in":[
279-
"0x160f3688",
280-
"condition"
281-
],
282155
"out":[
283-
"0x160fad08",
284-
"output"
285-
]
286-
},
287-
{
288-
"in":[
289-
"0x160fad08",
290-
"x"
291-
],
292-
"out":[
293-
"0x17678bc8",
156+
"0x15126848",
294157
"out"
158+
],
159+
"in":[
160+
"0x15140308",
161+
"a"
295162
]
296163
},
297164
{
298165
"in":[
299-
"0x160fad08",
300-
"y"
166+
"0x15140308",
167+
"b"
301168
],
302169
"out":[
303-
"0x1768efc8",
170+
"0x1514a4c8",
304171
"out"
305172
]
306173
}

example_nodes/logic_nodes.py

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from NodeGraphQt import BaseNode, QtCore
22

33

4-
class ifNode(BaseNode):
4+
class IfNode(BaseNode):
55
"""
66
if node.
77
"""
@@ -10,7 +10,7 @@ class ifNode(BaseNode):
1010
NODE_NAME = 'If'
1111

1212
def __init__(self):
13-
super(ifNode, self).__init__()
13+
super(IfNode, self).__init__()
1414
self.condition = self.add_input('condition')
1515
self._then = self.add_input('then')
1616
self._else = self.add_input('else')
@@ -39,3 +39,71 @@ def on_input_connected(self, to_port, from_port):
3939
def on_input_disconnected(self, to_port, from_port):
4040
"""Override node callback method."""
4141
self.set_property('out', None)
42+
43+
44+
class BooleanNode(BaseNode):
45+
"""
46+
Boolean Logic funtions node.
47+
"""
48+
49+
__identifier__ = 'Logics'
50+
51+
NODE_NAME = 'Boolean'
52+
53+
logics = {'and': 'a and b',
54+
'or': 'a or b',
55+
'xor': '(not a and b) or (a and not b)',
56+
'not': 'not a'}
57+
58+
def __init__(self):
59+
super(BooleanNode, self).__init__()
60+
self.a = self.add_input('a')
61+
self.b = self.add_input('b')
62+
self.add_output('out')
63+
self.create_property('out', None)
64+
self.add_combo_menu('funcs',
65+
'Functions',
66+
items=list(self.logics.keys()),
67+
tab='widgets')
68+
self.func = logics['and']
69+
# switch math function type
70+
self.view.widgets['funcs'].value_changed.connect(self.addFunction)
71+
self.view.widgets['funcs'].value_changed.connect(self.update_streams)
72+
73+
def addFunction(self, prop, func):
74+
"""
75+
Create inputs based on math functions arguments.
76+
"""
77+
self.func = logics[func]
78+
if self.b.visible() and not 'b' in self.func:
79+
self.b.set_visible(False)
80+
elif not self.b.visible():
81+
self.b.set_visible(True)
82+
83+
def run(self):
84+
a = None
85+
b = None
86+
87+
for port in self.a.connected_ports():
88+
port.node().run()
89+
a = port.node().get_property(port.name())
90+
91+
for port in self.b.connected_ports():
92+
port.node().run()
93+
b = port.node().get_property(port.name())
94+
95+
if a is None or (b is None and 'b' in self.func):
96+
raise Exception("No inputs!")
97+
98+
self.set_property('out', eval(self.func))
99+
100+
def on_input_connected(self, to_port, from_port):
101+
"""Override node callback method."""
102+
from_port.node().run()
103+
result = from_port.node().get_property(from_port.name())
104+
self.set_property(to_port.name(), result)
105+
self.update_streams()
106+
107+
def on_input_disconnected(self, to_port, from_port):
108+
"""Override node callback method."""
109+
self.set_property('out', None)

0 commit comments

Comments
 (0)