From fbcf2e8bbac596855fff13610fa8343f72dcac15 Mon Sep 17 00:00:00 2001 From: aria1th <35677394+aria1th@users.noreply.github.com> Date: Mon, 11 Dec 2023 03:52:17 +0900 Subject: [PATCH] more conversion --- autonode.py | 24 ++++++++++++++++++++++++ conversion.py | 45 ++++++++++++++++++++++++++++++++++++++++++++- logic_gates.py | 27 ++++++++++++++++++++++++++- 3 files changed, 94 insertions(+), 2 deletions(-) diff --git a/autonode.py b/autonode.py index 6af127f..fa2f0f6 100644 --- a/autonode.py +++ b/autonode.py @@ -21,3 +21,27 @@ def validate(container): for attr in ["FUNCTION", "INPUT_TYPES", "RETURN_TYPES", "CATEGORY"]: if not hasattr(cls, attr): raise Exception("Class {} doesn't have attribute {}".format(cls.__name__, attr)) + +class AllTrue(str): + def __init__(self, representation=None) -> None: + self.repr = representation + pass + def __ne__(self, __value: object) -> bool: + return False + # isinstance, jsonserializable hijack + def __instancecheck__(self, instance): + return True + def __subclasscheck__(self, subclass): + return True + def __bool__(self): + return True + def __str__(self): + return self.repr + # jsonserializable hijack + def __jsonencode__(self): + return self.repr + def __repr__(self) -> str: + return self.repr + def __eq__(self, __value: object) -> bool: + return True +anytype = AllTrue("variable") # when a != b is called, it will always return False diff --git a/conversion.py b/conversion.py index 1e2781b..6a42501 100644 --- a/conversion.py +++ b/conversion.py @@ -2,7 +2,7 @@ Converts int / float / boolean Note that it depends on the order of the conversion """ -from .autonode import validate, node_wrapper, get_node_names_mappings +from .autonode import validate, node_wrapper, get_node_names_mappings, anytype classes = [] node = node_wrapper(classes) @@ -35,6 +35,49 @@ def create_class(type_from, type_to): node(CustomClass) return CustomClass + +@node +class StringListToCombo: + """ + Converts raw string, separates with separator, then return as raw list + """ + RETURN_TYPES = (anytype,) + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "string": ("STRING", {"default": ""}), + "separator": ("STRING", {"default": "$"}), + } + } + FUNCTION = "stringListToCombo" + CATEGORY = "Logic Gates" + custom_name = "String List to Combo" + def stringListToCombo(self, string, separator): + return (string.split(separator),) + +@node +class ConvertComboToString: + """ + Converts raw list to string, separated with separator + """ + RETURN_TYPES = ("STRING",) + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "combo": (anytype, {"default": []}), + "separator": ("STRING", {"default": "$"}), + } + } + FUNCTION = "convertComboToString" + CATEGORY = "Logic Gates" + custom_name = "Convert Combo to String" + def convertComboToString(self, combo, separator): + if isinstance(combo, (str, float, int, bool)): + return (combo,) + return (separator.join(combo),) + for type_from in conversion_operators: for type_to in conversion_operators: create_class(type_from, type_to) diff --git a/logic_gates.py b/logic_gates.py index 80352dd..829d349 100644 --- a/logic_gates.py +++ b/logic_gates.py @@ -2,7 +2,7 @@ Implements logic gate nodes """ import re -from .autonode import node_wrapper, get_node_names_mappings, validate +from .autonode import node_wrapper, get_node_names_mappings, validate, anytype classes = [] @@ -469,6 +469,31 @@ class MergeString: custom_name = "Merge String" def merge(self, input1, input2): return (input1 + input2,) + +@node +class MemoryNode: + """ + Stores a value in memory. + Flip-flop behaviour. + """ + RETURN_TYPES = (anytype,) + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "input1": (anytype, {"default": ""}), + "flag": (anytype, {"default": 0}), + } + } + FUNCTION = "memory" + CATEGORY = "Logic Gates" + custom_name = "Memory String" + def memory(self, input1, flag): + if flag: + self.memory_value = input1 + return (self.memory_value,) + + CLASS_MAPPINGS, CLASS_NAMES = get_node_names_mappings(classes) validate(classes)