File size: 2,988 Bytes
f50a305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from copy import deepcopy

def inject(assembler, chain_definition, chain_items):
    if not chain_items:
        return

    output_map = chain_definition.get('output_map', {})
    current_connections = {}
    for key, type_name in output_map.items():
        if ':' in str(key):
            node_name, idx_str = key.split(':')
            if node_name not in assembler.node_map:
                print(f"Warning: [NewBie LoRA Injector] Node '{node_name}' in chain's output_map not found. Skipping.")
                continue
            node_id = assembler.node_map[node_name]
            start_output_idx = int(idx_str)
            current_connections[type_name] = [node_id, start_output_idx]
        else:
            print(f"Warning: [NewBie LoRA Injector] output_map key '{key}' is not in 'node:index' format. Skipping this connection.")

    template_name = chain_definition.get('template')
    if not template_name:
        print(f"Warning: [NewBie LoRA Injector] No 'template' defined for chain. Skipping.")
        return

    for item_data in chain_items:
        template = assembler._get_node_template(template_name)
        node_data = deepcopy(template)
        
        node_data['inputs']['lora_name'] = item_data.get('lora_name')
        node_data['inputs']['strength'] = item_data.get('strength_model', 1.0)
        node_data['inputs']['enabled'] = True
        
        if 'model' in current_connections:
            node_data['inputs']['model'] = current_connections['model']
        if 'clip' in current_connections:
            node_data['inputs']['clip'] = current_connections['clip']

        new_node_id = assembler._get_unique_id()
        assembler.workflow[new_node_id] = node_data
        
        current_connections['model'] = [new_node_id, 0]
        current_connections['clip'] = [new_node_id, 1]

    end_input_map = chain_definition.get('end_input_map', {})
    for type_name, targets in end_input_map.items():
        if type_name in current_connections:
            if not isinstance(targets, list):
                targets = [targets]
            
            for target_str in targets:
                try:
                    end_node_name, end_input_name = target_str.split(':')
                    if end_node_name in assembler.node_map:
                        end_node_id = assembler.node_map[end_node_name]
                        assembler.workflow[end_node_id]['inputs'][end_input_name] = current_connections[type_name]
                    else:
                        print(f"Warning: [NewBie LoRA Injector] End node '{end_node_name}' for dynamic chain not found. Skipping connection.")
                except ValueError:
                    print(f"Warning: [NewBie LoRA Injector] Invalid target format '{target_str}' in end_input_map. Skipping.")

    if chain_items:
        print(f"NewBie LoRA injector applied. Re-routed model and clip through {len(chain_items)} LoRA(s).")