-
-
Notifications
You must be signed in to change notification settings - Fork 400
Allow saving generated ControlNet previews #1364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jtreminio
wants to merge
4
commits into
mcmonkeyprojects:master
Choose a base branch
from
jtreminio:controlnet-save
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+50
−6
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -814,7 +814,7 @@ function genInputs(delay_final = false) { | |
| let controlnetGroup = document.getElementById('input_group_content_controlnet'); | ||
| if (controlnetGroup) { | ||
| let firstGroup = controlnetGroup.querySelector('.input-group'); | ||
| let buttonDiv = createDiv(`controlnet_button_preview`, null, `<button class="basic-button" onclick="controlnetShowPreview()">Preview</button>`); | ||
| let buttonDiv = createDiv(`controlnet_button_preview`, null, `<button class="basic-button" onclick="controlnetShowPreview()">Preview</button> <button class="basic-button" onclick="controlnetSavePreviewToServer()">Save to Server</button>`); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This probably needs a disabled or a display:none? otherwise it's a very odd button with odd behavior. |
||
| if (firstGroup) { | ||
| controlnetGroup.insertBefore(buttonDiv, firstGroup); | ||
| } | ||
|
|
@@ -1451,8 +1451,17 @@ function debugShowHiddenParams() { | |
| } | ||
| } | ||
|
|
||
| /** Clears any shown or stored ControlNet preview. */ | ||
| function controlnetClearPreview(previewArea) { | ||
| for (let result of previewArea.querySelectorAll('.controlnet-preview-result, .controlnet-save-result')) { | ||
| result.remove(); | ||
| } | ||
| delete previewArea.dataset.controlnetPreviewImage; | ||
| delete previewArea.dataset.controlnetPreviewMetadata; | ||
| } | ||
|
|
||
| /** Loads and shows a preview of ControlNet preprocessing to the user. */ | ||
| function controlnetShowPreview() { | ||
| function controlnetShowPreview(callback) { | ||
| let toggler = getRequiredElementById('input_group_content_controlnet_toggle'); | ||
| if (!toggler.checked) { | ||
| toggler.checked = true; | ||
|
|
@@ -1464,18 +1473,12 @@ function controlnetShowPreview() { | |
| return; | ||
| } | ||
| let previewArea = getRequiredElementById('controlnet_button_preview'); | ||
| let clearPreview = () => { | ||
| let lastResult = previewArea.querySelector('.controlnet-preview-result'); | ||
| if (lastResult) { | ||
| lastResult.remove(); | ||
| } | ||
| }; | ||
| clearPreview(); | ||
| controlnetClearPreview(previewArea); | ||
| let imgInput = getRequiredElementById('input_controlnetimageinput'); | ||
| if (!imgInput || !imgInput.dataset.filedata) { | ||
| let secondaryImageOption = getRequiredElementById('input_initimage'); | ||
| if (!secondaryImageOption || !secondaryImageOption.dataset.filedata) { | ||
| clearPreview(); | ||
| controlnetClearPreview(previewArea); | ||
| previewArea.append(createDiv(null, 'controlnet-preview-result', 'Must select an image.')); | ||
| return; | ||
| } | ||
|
|
@@ -1509,12 +1512,95 @@ function controlnetShowPreview() { | |
| imgElem.src = data.image; | ||
| resultBox.append(imgElem); | ||
| } | ||
| clearPreview(); | ||
| controlnetClearPreview(previewArea); | ||
| previewArea.dataset.controlnetPreviewImage = data.image; | ||
| previewArea.dataset.controlnetPreviewMetadata = data.metadata ?? ''; | ||
| previewArea.append(resultBox); | ||
| if (callback) { | ||
| callback(data); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /** Gets the target input folder for saved ControlNet previews. */ | ||
| function controlnetGetPreviewInputFolder(metadata) { | ||
| if (metadata) { | ||
| try { | ||
| let parsed = JSON.parse(metadata); | ||
| let extraData = parsed.sui_extra_data; | ||
| if (extraData && extraData.controlnet_preview_input_folder) { | ||
| return `${extraData.controlnet_preview_input_folder}`.replace(/\/$/, ''); | ||
| } | ||
| } | ||
| catch (ex) { | ||
| } | ||
| } | ||
| let exactBackendInput = document.getElementById('input_exactbackendid'); | ||
| let exactBackendToggle = document.getElementById('input_exactbackendid_toggle'); | ||
| if (exactBackendInput && (!exactBackendToggle || exactBackendToggle.checked)) { | ||
| let parsedId = parseInt(exactBackendInput.value); | ||
| if (!Number.isNaN(parsedId)) { | ||
| return `inputs/_comfy${parsedId}`; | ||
| } | ||
| } | ||
| return 'inputs'; | ||
|
jtreminio marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** Gets a timestamped base file name for a saved ControlNet preview. */ | ||
| function controlnetGetPreviewSaveName() { | ||
| let now = new Date(); | ||
| let pad = (val) => `${val}`.padStart(2, '0'); | ||
| let millis = `${now.getMilliseconds()}`.padStart(3, '0'); | ||
| return `controlnet-preview-${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}_${pad(now.getHours())}-${pad(now.getMinutes())}-${pad(now.getSeconds())}-${millis}`; | ||
| } | ||
|
|
||
| /** Saves a ControlNet preview data URL through the existing history save route. */ | ||
| function controlnetSavePreviewDataToServer(image, metadata) { | ||
| let targetFolder = `${controlnetGetPreviewInputFolder(metadata)}/controlnet`; | ||
| let data = { | ||
| image: image, | ||
| ['Override Outpath Format']: `${targetFolder}/${controlnetGetPreviewSaveName()}`.replaceAll('[', '') | ||
| }; | ||
| let imageFormatsByMimeType = { 'image/png': 'PNG', 'image/jpeg': 'JPG', 'image/webp': 'WEBP' }; | ||
| let mimeType = guessMimeTypeForExtension(image); | ||
| if (mimeType in imageFormatsByMimeType) { | ||
| data['Image Format'] = imageFormatsByMimeType[mimeType]; | ||
| } | ||
| genericRequest('AddImageToHistory', data, res => { | ||
| if (inputBrowserHelper.inputImageBrowser) { | ||
| inputBrowserHelper.inputImageBrowser.lightRefresh(); | ||
|
jtreminio marked this conversation as resolved.
Outdated
|
||
| } | ||
| let previewArea = getRequiredElementById('controlnet_button_preview'); | ||
| let oldSaveResult = previewArea.querySelector('.controlnet-save-result'); | ||
| if (oldSaveResult) { | ||
| oldSaveResult.remove(); | ||
| } | ||
| let saveResult = createDiv(null, 'controlnet-save-result modal_success_bottom', 'Saved ControlNet preview.'); | ||
| previewArea.append(saveResult); | ||
| setTimeout(() => { | ||
| saveResult.remove(); | ||
| }, 5000); | ||
| }); | ||
| } | ||
|
|
||
| /** Saves the current ControlNet preview, generating it first if needed. */ | ||
| function controlnetSavePreviewToServer() { | ||
| let previewArea = getRequiredElementById('controlnet_button_preview'); | ||
| let image = previewArea.dataset.controlnetPreviewImage; | ||
| let metadata = previewArea.dataset.controlnetPreviewMetadata ?? ''; | ||
| if (image) { | ||
| controlnetSavePreviewDataToServer(image, metadata); | ||
| return; | ||
| } | ||
| controlnetShowPreview(data => { | ||
| if (!data || !data.image) { | ||
| return; | ||
| } | ||
| controlnetSavePreviewDataToServer(data.image, data.metadata ?? ''); | ||
| }); | ||
| } | ||
|
|
||
| /** Gets the parameter with a given ID, from either the current param set, or the raw set from server. If unavailable, returns null. */ | ||
| function getParamById(id) { | ||
| if (!gen_param_types) { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.