Files
2026-04-11 18:14:51 +01:00

105 lines
4.3 KiB
Python

import copy
class SDXLMicroConditioningAdvanced:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"conditioning": ("CONDITIONING", ),
"original_width": ("INT", {"default": 1024, "min": 0, "max": 8192, "step": 1}),
"original_height": ("INT", {"default": 1024, "min": 0, "max": 8192, "step": 1}),
"crop_left": ("INT", {"default": 0, "min": 0, "max": 8192, "step": 1}),
"crop_top": ("INT", {"default": 0, "min": 0, "max": 8192, "step": 1}),
"target_width": ("INT", {"default": 1024, "min": 0, "max": 8192, "step": 8}),
"target_height": ("INT", {"default": 1024, "min": 0, "max": 8192, "step": 8}),
}}
RETURN_TYPES = ("CONDITIONING",)
FUNCTION = "apply_conditioning"
CATEGORY = "advanced/conditioning"
def apply_conditioning(self, conditioning, original_width, original_height, crop_left, crop_top, target_width, target_height):
new_conditioning =[]
for c in conditioning:
cond_tensor, cond_dict = c
# Deep copy to ensure we don't accidentally modify the positive prompt's dict
# if they somehow share references
new_dict = copy.deepcopy(cond_dict)
# Override with negative micro-conditioning values
new_dict["width"] = original_width
new_dict["height"] = original_height
new_dict["crop_w"] = crop_left
new_dict["crop_h"] = crop_top
new_dict["target_width"] = target_width
new_dict["target_height"] = target_height
new_conditioning.append([cond_tensor, new_dict])
return (new_conditioning, )
class SDXLMicroConditioningSimple:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"conditioning": ("CONDITIONING", ),
"latent": ("LATENT", ),
"scalar": ("FLOAT", {"default": 0.75, "min": 0.01, "max": 100.0, "step": 0.01}),
"scale_target_size": ("BOOLEAN", {"default": True}),
}}
RETURN_TYPES = ("CONDITIONING",)
FUNCTION = "apply_conditioning"
CATEGORY = "advanced/conditioning"
def apply_conditioning(self, conditioning, latent, scalar, scale_target_size):
# Extract pixel dimensions from the latent tensor
# latent["samples"] shape is[batch_size, channels, height, width]#
keep_target_size_unscaled = not scale_target_size
latent_tensor = latent["samples"]
# Multiply latent dims by 8 to get actual image pixel dimensions
target_height = latent_tensor.shape[2] * 8
target_width = latent_tensor.shape[3] * 8
# Scale and round to the nearest multiple of 8
neg_orig_width = round((target_width * scalar) / 8) * 8
neg_orig_height = round((target_height * scalar) / 8) * 8
if keep_target_size_unscaled:
# Thinking that in training the original width and height for lowres images is different to the target width and height
# So you might want to simulate that
neg_target_width = target_width
neg_target_height = target_height
else:
# Scales the target size down alongside the original size
neg_target_width = neg_orig_width
neg_target_height = neg_orig_height
new_conditioning =[]
for c in conditioning:
cond_tensor, cond_dict = c
new_dict = copy.deepcopy(cond_dict)
# Inject our calculated values
new_dict["width"] = neg_orig_width
new_dict["height"] = neg_orig_height
new_dict["crop_w"] = 0
new_dict["crop_h"] = 0
new_dict["target_width"] = neg_target_width
new_dict["target_height"] = neg_target_height
new_conditioning.append([cond_tensor, new_dict])
return (new_conditioning, )
# Register Nodes in ComfyUI
NODE_CLASS_MAPPINGS = {
"SDXLMicroConditioningAdvanced": SDXLMicroConditioningAdvanced,
"SDXLMicroConditioningSimple": SDXLMicroConditioningSimple
}
NODE_DISPLAY_NAME_MAPPINGS = {
"SDXLMicroConditioningAdvanced": "SDXL Micro-Cond (Advanced)",
"SDXLMicroConditioningSimple": "SDXL Micro-Cond (Simple)"
}