@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis ;
using System.ComponentModel.DataAnnotations ;
using System.Diagnostics.CodeAnalysis ;
using System.Drawing ;
using System.Runtime.Serialization ;
using System.Text.Json.Serialization ;
@ -26,9 +27,7 @@ public class ComfyNodeBuilder
{
if ( i > 1 _ 0 0 0 _ 0 0 0 )
{
throw new InvalidOperationException (
$"Could not find unique name for base {nameBase}"
) ;
throw new InvalidOperationException ( $"Could not find unique name for base {nameBase}" ) ;
}
name = $"{nameBase}_{i + 1}" ;
@ -63,39 +62,6 @@ public class ComfyNodeBuilder
public required double Denoise { get ; init ; }
}
/ * public static NamedComfyNode < LatentNodeConnection > KSampler (
string name ,
ModelNodeConnection model ,
ulong seed ,
int steps ,
double cfg ,
ComfySampler sampler ,
ComfyScheduler scheduler ,
ConditioningNodeConnection positive ,
ConditioningNodeConnection negative ,
LatentNodeConnection latentImage ,
double denoise
)
{
return new NamedComfyNode < LatentNodeConnection > ( name )
{
ClassType = "KSampler" ,
Inputs = new Dictionary < string , object? >
{
["model"] = model . Data ,
["seed"] = seed ,
["steps"] = steps ,
["cfg"] = cfg ,
["sampler_name"] = sampler . Name ,
["scheduler"] = scheduler . Name ,
["positive"] = positive . Data ,
["negative"] = negative . Data ,
["latent_image"] = latentImage . Data ,
["denoise"] = denoise
}
} ;
} * /
public record KSamplerAdvanced : ComfyTypedNodeBase < LatentNodeConnection >
{
public required ModelNodeConnection Model { get ; init ; }
@ -117,44 +83,34 @@ public class ComfyNodeBuilder
public bool ReturnWithLeftoverNoise { get ; init ; }
}
/ * public static NamedComfyNode < LatentNodeConnection > KSamplerAdvanced (
string name ,
ModelNodeConnection model ,
bool addNoise ,
ulong noiseSeed ,
int steps ,
double cfg ,
ComfySampler sampler ,
ComfyScheduler scheduler ,
ConditioningNodeConnection positive ,
ConditioningNodeConnection negative ,
LatentNodeConnection latentImage ,
int startAtStep ,
int endAtStep ,
bool returnWithLeftoverNoise
)
public record SamplerCustom : ComfyTypedNodeBase < LatentNodeConnection , LatentNodeConnection >
{
return new NamedComfyNode < LatentNodeConnection > ( name )
{
ClassType = "KSamplerAdvanced" ,
Inputs = new Dictionary < string , object? >
{
["model"] = model . Data ,
["add_noise"] = addNoise ? "enable" : "disable" ,
["noise_seed"] = noiseSeed ,
["steps"] = steps ,
["cfg"] = cfg ,
["sampler_name"] = sampler . Name ,
["scheduler"] = scheduler . Name ,
["positive"] = positive . Data ,
["negative"] = negative . Data ,
["latent_image"] = latentImage . Data ,
["start_at_step"] = startAtStep ,
["end_at_step"] = endAtStep ,
["return_with_leftover_noise"] = returnWithLeftoverNoise ? "enable" : "disable"
}
} ;
} * /
public required ModelNodeConnection Model { get ; init ; }
public required bool AddNoise { get ; init ; }
public required ulong NoiseSeed { get ; init ; }
[Range(0d, 100d)]
public required double Cfg { get ; init ; }
public required ConditioningNodeConnection Positive { get ; init ; }
public required ConditioningNodeConnection Negative { get ; init ; }
public required SamplerNodeConnection Sampler { get ; init ; }
public required SigmasNodeConnection Sigmas { get ; init ; }
public required LatentNodeConnection LatentImage { get ; init ; }
}
public record KSamplerSelect : ComfyTypedNodeBase < SamplerNodeConnection >
{
public required string SamplerName { get ; init ; }
}
public record SDTurboScheduler : ComfyTypedNodeBase < SigmasNodeConnection >
{
public required ModelNodeConnection Model { get ; init ; }
[Range(1, 10)]
public required int Steps { get ; init ; }
}
public record EmptyLatentImage : ComfyTypedNodeBase < LatentNodeConnection >
{
@ -191,18 +147,11 @@ public class ComfyNodeBuilder
return new NamedComfyNode < ImageNodeConnection > ( name )
{
ClassType = "ImageUpscaleWithModel" ,
Inputs = new Dictionary < string , object? >
{
["upscale_model"] = upscaleModel . Data ,
["image"] = image . Data
}
Inputs = new Dictionary < string , object? > { [ "upscale_model" ] = upscaleModel . Data , [ "image" ] = image . Data }
} ;
}
public static NamedComfyNode < UpscaleModelNodeConnection > UpscaleModelLoader (
string name ,
string modelName
)
public static NamedComfyNode < UpscaleModelNodeConnection > UpscaleModelLoader ( string name , string modelName )
{
return new NamedComfyNode < UpscaleModelNodeConnection > ( name )
{
@ -323,8 +272,7 @@ public class ComfyNodeBuilder
public required string ControlNetName { get ; init ; }
}
public record ControlNetApplyAdvanced
: ComfyTypedNodeBase < ConditioningNodeConnection , ConditioningNodeConnection >
public record ControlNetApplyAdvanced : ComfyTypedNodeBase < ConditioningNodeConnection , ConditioningNodeConnection >
{
public required ConditioningNodeConnection Positive { get ; init ; }
public required ConditioningNodeConnection Negative { get ; init ; }
@ -335,10 +283,7 @@ public class ComfyNodeBuilder
public required double EndPercent { get ; init ; }
}
public ImageNodeConnection Lambda_LatentToImage (
LatentNodeConnection latent ,
VAENodeConnection vae
)
public ImageNodeConnection Lambda_LatentToImage ( LatentNodeConnection latent , VAENodeConnection vae )
{
var name = GetUniqueName ( "VAEDecode" ) ;
return Nodes
@ -353,10 +298,7 @@ public class ComfyNodeBuilder
. Output ;
}
public LatentNodeConnection Lambda_ImageToLatent (
ImageNodeConnection pixels ,
VAENodeConnection vae
)
public LatentNodeConnection Lambda_ImageToLatent ( ImageNodeConnection pixels , VAENodeConnection vae )
{
var name = GetUniqueName ( "VAEEncode" ) ;
return Nodes
@ -380,9 +322,7 @@ public class ComfyNodeBuilder
ImageNodeConnection image
)
{
var modelLoader = Nodes . AddNamedNode (
UpscaleModelLoader ( $"{name}_UpscaleModelLoader" , modelName )
) ;
var modelLoader = Nodes . AddNamedNode ( UpscaleModelLoader ( $"{name}_UpscaleModelLoader" , modelName ) ) ;
var upscaler = Nodes . AddNamedNode (
ImageUpscaleWithModel ( $"{name}_ImageUpscaleWithModel" , modelLoader . Output , image )
@ -425,16 +365,7 @@ public class ComfyNodeBuilder
. Output ,
image = >
Nodes
. AddNamedNode (
ImageScale (
$"{name}_ImageUpscale" ,
image ,
upscaleInfo . Name ,
height ,
width ,
false
)
)
. AddNamedNode ( ImageScale ( $"{name}_ImageUpscale" , image , upscaleInfo . Name , height , width , false ) )
. Output
) ;
}
@ -445,22 +376,11 @@ public class ComfyNodeBuilder
var samplerImage = GetPrimaryAsImage ( primary , vae ) ;
// Do group upscale
var modelUpscaler = Group_UpscaleWithModel (
$"{name}_ModelUpscale" ,
upscaleInfo . Name ,
samplerImage
) ;
var modelUpscaler = Group_UpscaleWithModel ( $"{name}_ModelUpscale" , upscaleInfo . Name , samplerImage ) ;
// Since the model upscale is fixed to model (2x/4x), scale it again to the requested size
var resizedScaled = Nodes . AddNamedNode (
ImageScale (
$"{name}_ImageScale" ,
modelUpscaler . Output ,
"bilinear" ,
height ,
width ,
false
)
ImageScale ( $"{name}_ImageScale" , modelUpscaler . Output , "bilinear" , height , width , false )
) ;
return resizedScaled . Output ;
@ -512,22 +432,11 @@ public class ComfyNodeBuilder
) ;
// Do group upscale
var modelUpscaler = Group_UpscaleWithModel (
$"{name}_ModelUpscale" ,
upscaleInfo . Name ,
samplerImage . Output
) ;
var modelUpscaler = Group_UpscaleWithModel ( $"{name}_ModelUpscale" , upscaleInfo . Name , samplerImage . Output ) ;
// Since the model upscale is fixed to model (2x/4x), scale it again to the requested size
var resizedScaled = Nodes . AddNamedNode (
ImageScale (
$"{name}_ImageScale" ,
modelUpscaler . Output ,
"bilinear" ,
height ,
width ,
false
)
ImageScale ( $"{name}_ImageScale" , modelUpscaler . Output , "bilinear" , height , width , false )
) ;
// Convert back to latent space
@ -597,22 +506,11 @@ public class ComfyNodeBuilder
) ;
// Do group upscale
var modelUpscaler = Group_UpscaleWithModel (
$"{name}_ModelUpscale" ,
upscaleInfo . Name ,
samplerImage . Output
) ;
var modelUpscaler = Group_UpscaleWithModel ( $"{name}_ModelUpscale" , upscaleInfo . Name , samplerImage . Output ) ;
// Since the model upscale is fixed to model (2x/4x), scale it again to the requested size
var resizedScaled = Nodes . AddNamedNode (
ImageScale (
$"{name}_ImageScale" ,
modelUpscaler . Output ,
"bilinear" ,
height ,
width ,
false
)
ImageScale ( $"{name}_ImageScale" , modelUpscaler . Output , "bilinear" , height , width , false )
) ;
// No need to convert back to latent space
@ -654,22 +552,11 @@ public class ComfyNodeBuilder
if ( upscaleInfo . Type = = ComfyUpscalerType . ESRGAN )
{
// Do group upscale
var modelUpscaler = Group_UpscaleWithModel (
$"{name}_ModelUpscale" ,
upscaleInfo . Name ,
image
) ;
var modelUpscaler = Group_UpscaleWithModel ( $"{name}_ModelUpscale" , upscaleInfo . Name , image ) ;
// Since the model upscale is fixed to model (2x/4x), scale it again to the requested size
var resizedScaled = Nodes . AddNamedNode (
ImageScale (
$"{name}_ImageScale" ,
modelUpscaler . Output ,
"bilinear" ,
height ,
width ,
false
)
ImageScale ( $"{name}_ImageScale" , modelUpscaler . Output , "bilinear" , height , width , false )
) ;
// No need to convert back to latent space
@ -764,10 +651,7 @@ public class ComfyNodeBuilder
/// <summary>
/// Get or convert latest primary connection to latent
/// </summary>
public LatentNodeConnection GetPrimaryAsLatent (
PrimaryNodeConnection primary ,
VAENodeConnection vae
)
public LatentNodeConnection GetPrimaryAsLatent ( PrimaryNodeConnection primary , VAENodeConnection vae )
{
return primary . Match ( latent = > latent , image = > Lambda_ImageToLatent ( image , vae ) ) ;
}
@ -807,10 +691,7 @@ public class ComfyNodeBuilder
/// <summary>
/// Get or convert latest primary connection to image
/// </summary>
public ImageNodeConnection GetPrimaryAsImage (
PrimaryNodeConnection primary ,
VAENodeConnection vae
)
public ImageNodeConnection GetPrimaryAsImage ( PrimaryNodeConnection primary , VAENodeConnection vae )
{
return primary . Match ( latent = > Lambda_LatentToImage ( latent , vae ) , image = > image ) ;
}
@ -825,10 +706,7 @@ public class ComfyNodeBuilder
return Connections . Primary . AsT1 ;
}
return GetPrimaryAsImage (
Connections . Primary ? ? throw new NullReferenceException ( "No primary connection" ) ,
vae
) ;
return GetPrimaryAsImage ( Connections . Primary ? ? throw new NullReferenceException ( "No primary connection" ) , vae ) ;
}
/// <summary>
@ -878,9 +756,7 @@ public class ComfyNodeBuilder
public ConditioningNodeConnection GetRefinerOrBaseConditioning ( )
{
return RefinerConditioning
? ? BaseConditioning
? ? throw new NullReferenceException ( "No Conditioning" ) ;
return RefinerConditioning ? ? BaseConditioning ? ? throw new NullReferenceException ( "No Conditioning" ) ;
}
public ConditioningNodeConnection GetRefinerOrBaseNegativeConditioning ( )
@ -892,10 +768,7 @@ public class ComfyNodeBuilder
public VAENodeConnection GetDefaultVAE ( )
{
return PrimaryVAE
? ? RefinerVAE
? ? BaseVAE
? ? throw new NullReferenceException ( "No VAE" ) ;
return PrimaryVAE ? ? RefinerVAE ? ? BaseVAE ? ? throw new NullReferenceException ( "No VAE" ) ;
}
}