Skip to content

Commit 8bfc43c

Browse files
author
ryanontheinside
committed
logic nodes
1 parent 4657b36 commit 8bfc43c

File tree

2 files changed

+94
-6
lines changed

2 files changed

+94
-6
lines changed

__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from .controls.value_controls import FloatControl, IntControl, StringControl
22
from .controls.sequence_controls import FloatSequence, IntSequence, StringSequence
3-
from .controls.utility_controls import FPSMonitor, SimilarityFilter, LazyCondition
3+
from .controls.utility_controls import FPSMonitor, SimilarityFilter, LazyCondition, LogicOperator, IfThenElse
44
from .controls.state_management_controls import StateResetNode, StateTestNode
55
from .controls.motion_controls import MotionController, ROINode, IntegerMotionController
66
from .misc_nodes import(
7-
DTypeConverter,
8-
FastWebcamCapture,
7+
DTypeConverter,
8+
FastWebcamCapture,
99
YOLOSimilarityCompare,
10-
TextRenderer,
11-
QuickShapeMask,
10+
TextRenderer,
11+
QuickShapeMask,
1212
MultilineText,
1313
LoadImageFromPath_
1414
)
@@ -28,6 +28,8 @@
2828
"FPSMonitor": FPSMonitor,
2929
"SimilarityFilter": SimilarityFilter,
3030
"LazyCondition": LazyCondition,
31+
"LogicOperator": LogicOperator,
32+
"IfThenElse": IfThenElse,
3133
"MotionController": MotionController,
3234
"IntegerMotionController": IntegerMotionController,
3335
"YOLOSimilarityCompare": YOLOSimilarityCompare,
@@ -40,7 +42,7 @@
4042
"LoadImageFromPath_": LoadImageFromPath_,
4143
"HandTrackingNode": HandTrackingNode,
4244
"HandMaskNode": HandMaskNode,
43-
#"RepulsiveMaskNode": RepulsiveMaskNode,
45+
"RepulsiveMaskNode": RepulsiveMaskNode,
4446
"ResizeMaskNode": ResizeMaskNode,
4547
"StateResetNode": StateResetNode,
4648
"StateTestNode": StateTestNode,

controls/utility_controls.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,92 @@ def update(self, condition, if_true, fallback, use_fallback):
267267
else:
268268
return (state["prev_output"],)
269269

270+
class LogicOperator(ControlNodeBase):
271+
DESCRIPTION = "Performs logical operations (AND, OR, NOT, XOR) on inputs based on their truthiness"
272+
273+
@classmethod
274+
def INPUT_TYPES(s):
275+
return {
276+
"required": {
277+
"operation": (["AND", "OR", "NOT", "XOR"], {
278+
"default": "AND",
279+
"tooltip": "Logical operation to perform"
280+
}),
281+
"input_a": (AlwaysEqualProxy("*"), {
282+
"tooltip": "First input to evaluate for truthiness",
283+
"forceInput": True,
284+
}),
285+
"always_execute": ("BOOLEAN", {
286+
"default": True,
287+
}),
288+
},
289+
"optional": {
290+
"input_b": (AlwaysEqualProxy("*"), {
291+
"tooltip": "Second input to evaluate for truthiness (not used for NOT operation)",
292+
}),
293+
}
294+
}
295+
296+
RETURN_TYPES = ("BOOLEAN",)
297+
FUNCTION = "update"
298+
CATEGORY = "real-time/control/logic"
299+
300+
def update(self, operation, input_a, always_execute=True, input_b=None):
301+
"""Perform the selected logical operation on inputs based on their truthiness."""
302+
a = bool(input_a)
303+
304+
if operation == "NOT":
305+
return (not a,)
306+
307+
# For all other operations, we need input_b
308+
b = bool(input_b)
309+
310+
if operation == "AND":
311+
return (a and b,)
312+
elif operation == "OR":
313+
return (a or b,)
314+
elif operation == "XOR":
315+
return (a != b,)
316+
317+
# Should never get here, but just in case
318+
return (False,)
319+
320+
class IfThenElse(ControlNodeBase):
321+
DESCRIPTION = "Selects between two values based on a condition's truthiness"
322+
323+
@classmethod
324+
def INPUT_TYPES(s):
325+
return {
326+
"required": {
327+
"condition": (AlwaysEqualProxy("*"), {
328+
"tooltip": "When truthy, outputs then_value; when falsy, outputs else_value",
329+
"forceInput": True,
330+
}),
331+
"then_value": (AlwaysEqualProxy("*"), {
332+
"tooltip": "Value to output when condition is truthy",
333+
"forceInput": True,
334+
}),
335+
"else_value": (AlwaysEqualProxy("*"), {
336+
"tooltip": "Value to output when condition is falsy",
337+
"forceInput": True,
338+
}),
339+
"always_execute": ("BOOLEAN", {
340+
"default": True,
341+
}),
342+
}
343+
}
344+
345+
RETURN_TYPES = (AlwaysEqualProxy("*"),)
346+
FUNCTION = "update"
347+
CATEGORY = "real-time/control/logic"
348+
349+
def update(self, condition, then_value, else_value, always_execute=True):
350+
"""Select between then_value and else_value based on condition truthiness."""
351+
if condition: # Let Python handle truthiness
352+
return (then_value,)
353+
else:
354+
return (else_value,)
355+
270356

271357

272358

0 commit comments

Comments
 (0)