72 lines
2.8 KiB
Python
72 lines
2.8 KiB
Python
import folder_paths
|
|
|
|
class LoRAStepRange:
|
|
@classmethod
|
|
def INPUT_TYPES(cls):
|
|
return {
|
|
"required": {
|
|
"clip": ("CLIP",),
|
|
"lora_name": (folder_paths.get_filename_list("loras"),),
|
|
"strength_clip": ("FLOAT", {"default": 1.0, "min": -20.0, "max": 20.0, "step": 0.01}),
|
|
"start_percent": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001, "display": "slider"}),
|
|
"end_percent": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.001, "display": "slider"}),
|
|
}
|
|
}
|
|
|
|
RETURN_TYPES = ("CLIP",)
|
|
FUNCTION = "apply_lora_with_schedule"
|
|
CATEGORY = "loaders"
|
|
|
|
def apply_lora_with_schedule(self, clip, lora_name, strength_clip, start_percent, end_percent):
|
|
from nodes import NODE_CLASS_MAPPINGS
|
|
|
|
# Step 1: Create LoRA hook
|
|
# Set strength_model to 0 to keep it CLIP-only as per your workflow
|
|
CreateHookLora = NODE_CLASS_MAPPINGS["CreateHookLora"]
|
|
lora_hook_node = CreateHookLora()
|
|
lora_method = getattr(lora_hook_node, CreateHookLora.FUNCTION)
|
|
|
|
hooks = lora_method(
|
|
lora_name=lora_name,
|
|
strength_model=0.0,
|
|
strength_clip=strength_clip,
|
|
prev_hooks=None
|
|
)[0]
|
|
|
|
# Step 2: Create keyframe schedule
|
|
CreateHookKeyframe = NODE_CLASS_MAPPINGS["CreateHookKeyframe"]
|
|
keyframe_node = CreateHookKeyframe()
|
|
keyframe_method = getattr(keyframe_node, CreateHookKeyframe.FUNCTION)
|
|
keyframes = None
|
|
|
|
if start_percent > 0.0:
|
|
keyframes = keyframe_method(strength_mult=0.0, start_percent=0.0, prev_hook_kf=None)[0]
|
|
|
|
keyframes = keyframe_method(strength_mult=1.0, start_percent=start_percent, prev_hook_kf=keyframes)[0]
|
|
|
|
if end_percent < 1.0:
|
|
keyframes = keyframe_method(strength_mult=0.0, start_percent=end_percent, prev_hook_kf=keyframes)[0]
|
|
|
|
# Step 3: Apply keyframes to hooks
|
|
SetHookKeyframes = NODE_CLASS_MAPPINGS["SetHookKeyframes"]
|
|
set_kf_node = SetHookKeyframes()
|
|
set_kf_method = getattr(set_kf_node, SetHookKeyframes.FUNCTION)
|
|
|
|
final_hooks = set_kf_method(hooks=hooks, hook_kf=keyframes)[0]
|
|
|
|
# Step 4: Apply to CLIP
|
|
SetClipHooks = NODE_CLASS_MAPPINGS["SetClipHooks"]
|
|
set_clip_hooks_node = SetClipHooks()
|
|
set_clip_method = getattr(set_clip_hooks_node, SetClipHooks.FUNCTION)
|
|
|
|
# CRITICAL FOR FLUX: schedule_clip must be True.
|
|
# This ensures the T5 encoder and CLIP-L both respect the start/end timing.
|
|
clip_with_hooks = set_clip_method(
|
|
clip=clip,
|
|
hooks=final_hooks,
|
|
schedule_clip=True,
|
|
apply_to_conds=True
|
|
)[0]
|
|
|
|
return (clip_with_hooks,)
|