From 4bc2b67a85b3640146101dbc26cfefe368d1787a Mon Sep 17 00:00:00 2001 From: mcDandy Date: Sat, 21 Feb 2026 16:30:52 +0100 Subject: [PATCH] Change types --- more_math/AudioMathNode.py | 35 +++++++++++++++++++++-------------- more_math/ParseTree.py | 14 ++++++++++++++ more_math/ScriptTextWindow.py | 7 +++++-- web/script_text_input.js | 9 ++++++--- 4 files changed, 46 insertions(+), 19 deletions(-) create mode 100644 more_math/ParseTree.py diff --git a/more_math/AudioMathNode.py b/more_math/AudioMathNode.py index ba4d4f1..583f7e5 100644 --- a/more_math/AudioMathNode.py +++ b/more_math/AudioMathNode.py @@ -1,3 +1,4 @@ +from tokenize import String from .helper_functions import ( generate_dim_variables, parse_expr, @@ -17,6 +18,7 @@ import re import torch from .Stack import MrmthStack import copy +from .ParseTree import MrmthParseTree class AudioMathNode(io.ComfyNode): """ @@ -37,7 +39,11 @@ class AudioMathNode(io.ComfyNode): inputs=[ io.Autogrow.Input(id="V",template=io.Autogrow.TemplatePrefix(io.Audio.Input("values", optional=True), prefix="V", min=1, max=50)), io.Autogrow.Input(id="F", template=io.Autogrow.TemplatePrefix(io.Float.Input("float", default=0.0, optional=True, lazy=True, force_input=True), prefix="F", min=1, max=50)), - io.String.Input(id="Expression", default="I0*(1-F0)+I1*F0", tooltip="Expression to apply on input audio"), + io.MultiType.Input( + io.String.Input("Expression", default="", multiline=False), + types=[io.String,MrmthParseTree], + tooltip="3D model file or path string", + ), io.Combo.Input( id="length_mismatch", options=["do nothing","error","tile", "pad"], @@ -56,17 +62,14 @@ class AudioMathNode(io.ComfyNode): @classmethod def check_lazy_status(cls, Expression, V, F, length_mismatch="tile",batching=0,stack={}): - - input_stream = InputStream(Expression) - lexer = MathExprLexer(input_stream) - stream = CommonTokenStream(lexer) - parser = MathExprParser(stream) - - try: - tree = parser.start() - except: - # Fallback to simple token scanning if parse fails - return cls._fallback_lazy_check(Expression, V, F) + tree = None + parser = None + if isinstance(Expression,str): + tree = parse_expr(Expression) + parser = tree.parser + else: + tree = Expression + parser = tree.parser # Support aliases aliases = {"a": "V0", "b": "V1", "c": "V2", "d": "V3", @@ -106,7 +109,7 @@ class AudioMathNode(io.ComfyNode): for var in needed_vars: norm = aliases.get(var, var) if var == "V": - needed.update(V.keys()) + needed.update(V.keys()) if var == "F": needed.update(F.keys()) if re.match(r"[VF][0-9]+", norm): @@ -231,7 +234,11 @@ class AudioMathNode(io.ComfyNode): for k, val in F.items(): variables[k] = val if val is not None else 0.0 - tree = parse_expr(Expression); + tree = None + if isinstance(Expression,str): + tree = parse_expr(Expression) + else: + tree = Expression visitor = UnifiedMathVisitor(variables, a_w.shape,a_w.device,state_storage=stack) result = visitor.visit(tree) result = as_tensor(result, a_w.shape) diff --git a/more_math/ParseTree.py b/more_math/ParseTree.py new file mode 100644 index 0000000..7f026a5 --- /dev/null +++ b/more_math/ParseTree.py @@ -0,0 +1,14 @@ +import antlr4 +from comfy_api.latest import io + +@io.comfytype(io_type="PARSER") +class MrmthParseTree(io.ComfyTypeIO): + Type = antlr4.Parser # Python type hint + + class Input(io.Input): + def __init__(self, id: str, **kwargs): + super().__init__(id, **kwargs) + + class Output(io.Output): + def __init__(self, **kwargs): + super().__init__(**kwargs) diff --git a/more_math/ScriptTextWindow.py b/more_math/ScriptTextWindow.py index 894f2f4..86848c0 100644 --- a/more_math/ScriptTextWindow.py +++ b/more_math/ScriptTextWindow.py @@ -1,5 +1,7 @@ from comfy_api.latest import io import torch +from .helper_functions import parse_expr +from .ParseTree import MrmthParseTree class ScriptTextInput(io.ComfyNode): @@ -13,10 +15,11 @@ class ScriptTextInput(io.ComfyNode): io.String.Input(id="script", multiline=True), ], outputs=[ - io.String.Output() + MrmthParseTree.Output() ], ) @classmethod def execute(cls, script): - return (script,) + expr = parse_expr(script) + return (expr,) diff --git a/web/script_text_input.js b/web/script_text_input.js index e487d59..a7f25c3 100644 --- a/web/script_text_input.js +++ b/web/script_text_input.js @@ -27,6 +27,11 @@ const FUNCTIONS = new Set([ "worley", "cellular_noise", "voronoi_noise", "plasma", "turbulence", "plasma_noise" ]); +const BRACKET_PAIRS = { + '(': ')', + '[': ']', + '{': '}' +}; const OPENING_BRACKETS = new Set(['(', '[', '{']); const CLOSING_BRACKETS = new Set([')', ']', '}']); @@ -318,10 +323,8 @@ function attachLineNumbers(widget) { const rawLines = inputEl.value.split("\n"); const paddingTop = parseFloat(inputStyle.paddingTop) || 0; const paddingBottom = parseFloat(inputStyle.paddingBottom) || 0; - const paddedScrollHeight = Math.max(0, inputEl.scrollHeight - paddingTop - paddingBottom); const textLineCount = Math.max(1, rawLines.length); - const visibleLineCount = Math.max(1, Math.ceil(paddedScrollHeight / lineHeightPx)); - const lineCount = Math.max(textLineCount, visibleLineCount); + const lineCount = textLineCount; gutterContent.textContent = Array.from({ length: lineCount }, (_, i) => String(i + 1)).join("\n"); gutterContent.style.height = `${lineCount * lineHeightPx + paddingTop + paddingBottom}px`; gutter.style.height = `${editorContainer.clientHeight}px`;