
    +j                        S r SSKrSSKrSSKJr  SSKJr  SSKJrJ	r	  Sr
S rS r0 S	\_S
\_S\_S\_S\_S\_S\R"                  _S\R$                  _S\R&                  _S\R(                  _S\R*                  _S\R,                  _S\R.                  _S\R0                  _S\R2                  _S\_S\_r " S S\	R:                  5      r " S S\5      rS\4S jr g) zMath expression node using simpleeval for safe evaluation.

Provides a ComfyMathExpression node that evaluates math expressions
against dynamically-grown numeric inputs.
    N)simple_eval)override)ComfyExtensionioi  c                  z    [        U 5      S:X  a"  [        U S   S5      (       a  [        U S   5      $ [        U 5      $ )z*Support both sum(values) and sum(a, b, c).   r   __iter__)lenhasattrsum)argss    7/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_math.py_variadic_sumr      s5    
4yA~'$q':6647|t9    c                 j    [        U5      [        :  a  [        SU S[         S35      e[        X5      $ )zWrap pow() with an exponent cap to prevent DoS via huge exponents.

The ** operator is already guarded by simpleeval's safe_power, but
pow() as a callable bypasses that guard.
z	Exponent z exceeds maximum allowed ())absMAX_EXPONENT
ValueErrorpow)baseexps     r   	_safe_powr      s6     3x,9SE)CL>QRSTTt>r   r   minmaxr   roundr   sqrtceilfloorloglog2log10sincostanintfloatc                       \ rS rSrSr\S\R                  4S j5       r\S\	S\R                  R                  S\R                  4S j5       rSrg	)
MathExpressionNode;   z=Evaluates a math expression against dynamically-grown inputs.returnc                 X   [         R                  R                  [         R                  R	                  S[         R
                  [         R                  [         R                  /5      [        [        R                  5      SS9n[         R                  " SSS/ SQ[         R                  R	                  SS	S
S9[         R                  R	                  SUS9/[         R
                  R                  SS9[         R                  R                  SS9[         R                  R                  SS9/S9$ )Nvaluer   )inputnamesr   ComfyMathExpressionzMath Expression	utilities)
expressionformula	calculate
calculatorevalmathr2   za + bT)default	multilinevalues)templateFLOAT)display_nameINTBOOL)node_idr=   categorysearch_aliasesinputsoutputs)r   AutogrowTemplateNames	MultiTypeInputFloatIntBooleanliststringascii_lowercaseSchemaStringOutput)clsautogrows     r   define_schema MathExpressionNode.define_schema>   s    ;;,,,,$$Wrxx.LMv--. - 

 yy)* 
 		gN!!(X!>
 W551

!!v!6
 	
r   r2   r:   c                 6   UR                  5       (       d  [        S5      e[        U5      n[        UR	                  5       5      US'   [        X[        S9n[        U[        [        45      (       d(  [        SU S[        U5      R                   SU< 35      e [        U5      n[        R                  " U5      (       d  [        SU SU 35      e[        R                   " U[        U5      [#        U5      5      $ ! [         a    [        SU SU 35      S ef = f)	NzExpression cannot be empty.r:   )r/   	functionszMath Expression 'z)' must evaluate to a numeric result, got z: z7' produced a result too large to represent as a float: z ' produced a non-finite result: )stripr   dictrL   r:   r   MATH_FUNCTIONS
isinstancer&   r'   type__name__OverflowErrorr7   isfiniter   
NodeOutputbool)rR   r2   r:   contextresultfloat_results         r   executeMathExpressionNode.executeX   s"    !!:;;V 1Z.Q&3,//#J< 0F|,,-Rz; 	 =L }}\**#J</OPVxX  }}\3v;VEE  	#J< 0))/2 	s   C; ;D N)r]   
__module____qualname____firstlineno____doc__classmethodr   rO   rT   strrE   Typer`   re   __static_attributes__rg   r   r   r)   r)   ;   sa    G
bii 
 
2 FF&(kk&6&6F	F Fr   r)   c                   L    \ rS rSr\S\\\R                        4S j5       r	Sr
g)MathExtensionw   r+   c                    #    [         /$ 7fN)r)   )selfs    r   get_node_listMathExtension.get_node_listx   s     "##s   
rg   N)r]   rh   ri   rj   r   rL   r\   r   	ComfyNoderv   ro   rg   r   r   rq   rq   w   s)    $T$r||*<%= $ $r   rq   r+   c                     #    [        5       $ 7frt   )rq   rg   r   r   comfy_entrypointrz   }   s     ?s   )!rk   r7   rM   
simpleevalr   typing_extensionsr   comfy_api.latestr   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   rZ   rx   r)   rq   rz   rg   r   r   <module>r~      sJ     " & / 	=	3 
3 
3	
 U 
9 DII DII TZZ 
488 DII TZZ 
488 
488 
488  
3!" U#*9F 9Fx$N $ r   