-- UNIVERSAL FUSION AUTO CONNECT + MULTIMERGE + BACKGROUND -- -- Select source nodes first, then run. -- -- PRIORITY: -- 1. Existing compatible multi-input plugin/node (e.g. Offsetizo1) -- 2. Existing MultiMerge -- 3. Create a new MultiMerge only if no suitable destination exists -- -- NEW FEATURES: -- - New MultiMerge is inserted immediately before MediaOut. -- - Existing MediaOut pipeline is preserved as MultiMerge Background. -- - If MediaOut is disconnected, a new Background node is created. -- - All operations are grouped into one Undo. local comp = fu:GetCurrentComp() if not comp then return end local selected = comp:GetToolList(true) local allTools = comp:GetToolList(false) if not selected or #selected == 0 then print("ERROR: Select source nodes first.") return end -- Collect source outputs local sources = {} for _, tool in pairs(selected) do local outputs = tool:GetOutputList() if outputs and outputs[1] then table.insert(sources, { tool = tool, output = outputs[1] }) end end if #sources == 0 then print("ERROR: No valid source outputs.") return end -- Helpers local function isSelected(tool) for _, source in ipairs(sources) do if source.tool == tool then return true end end return false end local function isMediaOut(tool) local attrs = tool:GetAttrs() local regID = tostring(attrs.TOOLS_RegID or "") local toolID = tostring(tool.ID or "") local toolName = tostring(tool.Name or "") return regID == "MediaOut" or toolID == "MediaOut" or string.match(toolName, "^MediaOut") ~= nil end local function isMultiMerge(tool) local attrs = tool:GetAttrs() return attrs.TOOLS_RegID == "MultiMerge" or tool.ID == "MultiMerge" end -- Get compatible empty inputs local function getAvailableInputs(tool) local available = {} for _, input in pairs(tool:GetInputList()) do local attrs = input:GetAttrs() local inputID = tostring(attrs.INPS_ID or "") local valid = attrs.INPS_DataType == "Image" -- MultiMerge: only use foreground layer inputs if isMultiMerge(tool) then valid = valid and string.match(inputID, "^Layer%d+%.Foreground$") ~= nil end if valid and not input:GetConnectedOutput() then table.insert(available, { input = input, id = inputID }) end end table.sort(available, function(a, b) return a.id < b.id end) return available end -- Find existing destinations local candidates = {} local multiMergeCandidates = {} for _, tool in pairs(allTools) do if not isSelected(tool) and not isMediaOut(tool) then local available = getAvailableInputs(tool) if isMultiMerge(tool) then table.insert(multiMergeCandidates, { tool = tool, available = #available }) elseif #available >= #sources then table.insert(candidates, { tool = tool, available = #available }) end end end -- Prefer the plugin with the closest sufficient number of inputs table.sort(candidates, function(a, b) return a.available < b.available end) -- Start Undo before creating or connecting anything comp:StartUndo("Universal Auto Connect + Background") local target = nil local createdNewMultiMerge = false -- PRIORITY 1: Existing compatible plugin if #candidates > 0 then target = candidates[1].tool print("USING EXISTING PLUGIN/NODE: " .. target.Name) -- PRIORITY 2: Existing MultiMerge elseif #multiMergeCandidates > 0 then table.sort(multiMergeCandidates, function(a, b) return a.available > b.available end) target = multiMergeCandidates[1].tool print("USING EXISTING MULTIMERGE: " .. target.Name) -- PRIORITY 3: Create new MultiMerge else print("No suitable existing destination found.") print("Creating a new MultiMerge...") local ok, newTool = pcall(function() return comp:AddTool("MultiMerge", 1, 1) end) if ok then target = newTool end if not target then comp:EndUndo() print("ERROR: Could not create MultiMerge.") return end createdNewMultiMerge = true print("CREATED: " .. target.Name) end -- Connect selected sources local connected = 0 local failed = 0 for _, source in ipairs(sources) do local success = false -- Refresh inputs after every connection local available = getAvailableInputs(target) for _, entry in ipairs(available) do local attrs = entry.input:GetAttrs() local outputAttrs = source.output:GetAttrs() -- Match source output type with destination input type if attrs.INPS_DataType == outputAttrs.OUTS_DataType then local ok, result = pcall(function() return entry.input:ConnectTo(source.output) end) if ok and result then connected = connected + 1 success = true print("CONNECTED: " .. source.tool.Name .. " -> " .. entry.id) break end end end if not success then failed = failed + 1 print("FAILED: " .. source.tool.Name) end end -- INSERT NEW MULTIMERGE BEFORE MEDIAOUT -- Only runs when this script creates a new MultiMerge. local pipelineConnected = false local backgroundCreated = false if createdNewMultiMerge then print("") print("SETTING UP NEW MULTIMERGE PIPELINE...") local mediaOut = nil local mediaOutInput = nil -- Locate MediaOut and its Image input for _, tool in pairs(comp:GetToolList(false)) do if isMediaOut(tool) then mediaOut = tool for _, input in pairs(tool:GetInputList()) do local attrs = input:GetAttrs() if attrs.INPS_DataType == "Image" then mediaOutInput = input break end end if mediaOutInput then break end end end if not mediaOut or not mediaOutInput then print("WARNING: MediaOut or its Image input was not found.") else -- Find MultiMerge Background input local backgroundInput = nil for _, input in pairs(target:GetInputList()) do local attrs = input:GetAttrs() if attrs.INPS_ID == "Background" and attrs.INPS_DataType == "Image" then backgroundInput = input break end end -- Get MultiMerge output local outputs = target:GetOutputList() local mergeOutput = outputs and outputs[1] if not backgroundInput then print("WARNING: MultiMerge Background input not found.") elseif not mergeOutput then print("WARNING: MultiMerge output not found.") else -- Check existing MediaOut connection local previousOutput = mediaOutInput:GetConnectedOutput() local backgroundConnected = false if previousOutput then -- Preserve existing pipeline local ok, result = pcall(function() return backgroundInput:ConnectTo(previousOutput) end) backgroundConnected = ok and result if backgroundConnected then print("PRESERVED: Previous pipeline -> MultiMerge Background") else print("WARNING: Could not preserve previous pipeline.") end else -- MediaOut is disconnected: -- ALWAYS create a Background node. print("MediaOut is disconnected.") print("Creating Background node...") local bg = nil local ok, result = pcall(function() return comp:AddTool("Background", 0, 0) end) if ok then bg = result end if bg then local bgOutputs = bg:GetOutputList() local bgOutput = bgOutputs and bgOutputs[1] if bgOutput then local connectOK, connectResult = pcall(function() return backgroundInput:ConnectTo(bgOutput) end) backgroundConnected = connectOK and connectResult if backgroundConnected then backgroundCreated = true print("BACKGROUND CONNECTED TO MULTIMERGE.") else print("WARNING: Background connection failed.") end end else print("ERROR: Could not create Background node.") end end -- Connect MultiMerge to MediaOut only if -- Background setup succeeded. if backgroundConnected then local ok, result = pcall(function() return mediaOutInput:ConnectTo(mergeOutput) end) if ok and result then pipelineConnected = true print("PIPELINE CONNECTED: " .. target.Name .. " -> " .. mediaOut.Name) else print("WARNING: Could not connect MultiMerge to MediaOut.") end else print("MediaOut left unchanged to protect the pipeline.") end end end end comp:EndUndo() -- FINAL RESULTS print("") print("========== FINAL RESULTS ==========") print("Destination: " .. target.Name) print("Selected sources: " .. #sources) print("Connected: " .. connected) print("Failed: " .. failed) if createdNewMultiMerge then print("New MultiMerge created: YES") print("Background created: " .. tostring(backgroundCreated)) print("Connected before MediaOut: " .. tostring(pipelineConnected)) else print("New MultiMerge created: NO") print("Existing pipeline left untouched.") end print("===================================")