feature multi document for SD

This commit is contained in:
zombieyang
2024-07-24 19:37:27 +08:00
parent bbb2541bca
commit 85a44c8dc7
8 changed files with 131 additions and 43 deletions
Binary file not shown.
+63 -18
View File
@@ -31,20 +31,30 @@
initDOM() {
this.insertedPop = true;
this.$inputGetDocument = document
.getElementById("sdppp_get_document")
.getElementsByTagName("input")[0];
this.$inputLayer = document
.getElementById("sdppp_layers")
.getElementsByTagName("input")[0];
this.$inputBound = document
.getElementById("sdppp_bounds")
.getElementsByTagName("input")[0];
this.$inputSendDocument = document
.getElementById("sdppp_send_document")
.getElementsByTagName("input")[0];
this.$inputSendLayer = document
.getElementById("sdppp_send_layers")
.getElementsByTagName("input")[0];
this.$refreshGetDocument = document
.getElementById("refresh_sdppp_get_document");
this.$refreshLayer = document
.getElementById("refresh_sdppp_layers");
this.$refreshBound = document
.getElementById("refresh_sdppp_bounds");
this.$refreshSendDocument = document
.getElementById("refresh_sdppp_send_document");
this.$refreshSendLayer = document
.getElementById("refresh_sdppp_send_layers");
@@ -54,6 +64,7 @@
.getElementById("sdppp_sender_dialog_close");
this.$getterSaveAndClose.addEventListener("click", () => {
this.currentWidget.documentValue = this.$inputGetDocument.value;
this.currentWidget.layerValue = this.$inputLayer.value;
this.currentWidget.boundValue = this.$inputBound.value;
this.currentWidget.render();
@@ -61,40 +72,62 @@
closePopup();
});
this.$senderSaveAndClose.addEventListener("click", () => {
this.currentWidget.sendLayerValue = this.$inputSendLayer.value;
this.currentWidget.layerValue = this.$inputSendLayer.value;
this.currentWidget.documentValue = this.$inputSendDocument.value;
this.currentWidget.render();
this.currentWidget.action();
closePopup();
});
}
async fetchDocumentData() {
const data = await new Promise(resolve => {
this.socket.emit('c_get_documents', resolve)
});
await new Promise(resolve => {
this.socket.emit('c_set_sd_options', {
document_data: data,
special_get_layer_options: SDPPPSpeicialIDManager.getSpecialLayerForGet(),
special_get_bound_layer_options: SDPPPSpeicialIDManager.getSpecialLayerForGetBounds(),
special_send_layer_options: SDPPPSpeicialIDManager.getSpecialLayerForSend()
}, resolve)
})
}
getterConfig(widget) {
async getterConfig(widget) {
popupId("sdppp_getter_dialog")
callGlobalGradioFunction("show_getter_dialog")
if (!this.insertedPop) {
this.initDOM();
}
this.currentWidget = widget;
if (widget.documentValue) {
this.$inputGetDocument.value = widget.documentValue;
}
if (widget.layerValue) {
this.$inputLayer.value = widget.layerValue;
}
if (widget.boundValue) {
this.$inputBound.value = widget.boundValue;
}
this.$refreshLayer.click();
this.$refreshBound.click();
await this.fetchDocumentData();
this.$refreshGetDocument.click();
}
senderConfig(widget) {
async senderConfig(widget) {
popupId("sdppp_sender_dialog")
callGlobalGradioFunction("show_sender_dialog")
if (!this.insertedPop) {
this.initDOM();
}
this.currentWidget = widget;
if (widget.sendLayerValue) {
this.$inputSendLayer.value = widget.sendLayerValue;
if (widget.documentValue) {
this.$inputSendDocument.value = widget.documentValue;
}
this.$refreshSendLayer.click();
if (widget.layerValue) {
this.$inputSendLayer.value = widget.layerValue;
}
await this.fetchDocumentData();
this.$refreshSendDocument.click();
}
async initSocket() {
@@ -121,7 +154,7 @@
const actions = []
for (let getWidgetRef of insertedGetWidgets) {
const widget = getWidgetRef.deref();
if (widget && widget.layerValue && widget.boundValue) {
if (widget && widget.isReady()) {
actions.push(widget.action());
}
}
@@ -153,7 +186,7 @@
for (let sendWidgetRef of insertedSendWidgets) {
const widget = sendWidgetRef.deref();
if (widget && widget.sendLayerValue) {
if (widget.isReady()) {
widget.action()
}
}
@@ -165,7 +198,7 @@
this.socket.emit('c_reset_instance_name', {
name: document.title
})
}, 3000)
}, 1000)
});
}
@@ -219,6 +252,10 @@
}
class GetterWidget extends SDPPPWidget {
isReady() {
return this.layerValue && this.boundValue && this.documentValue;
}
openConfig() {
SDPPP.instance.getterConfig(this);
}
@@ -232,7 +269,7 @@
$getButton
.addEventListener('click', async () => {
if (!this.layerValue || !this.boundValue)
if (!this.isReady())
this.openConfig();
else {
this.action();
@@ -241,6 +278,7 @@
this.layerValue = '';
this.boundValue = '';
this.documentValue = '';
this.render();
}
@@ -249,7 +287,7 @@
super.render();
this.$el.className = "sdppp-get-widget";
this.$getButton.className = "sdppp-get-button";
if (this.layerValue && this.boundValue) {
if (this.isReady()) {
this.$settingButton.style.display = 'flex';
this.$getButton.textContent = "SDPPP get";
@@ -262,7 +300,8 @@
async action() {
await SDPPP.instance.getImage({
'layer_id': this.layerValue,
'document': this.documentValue,
'layer': this.layerValue,
'use_layer_bounds': this.boundValue,
'sd': {
elem_id: this.$gradioImage.id
@@ -274,6 +313,10 @@
}
class SenderWidget extends SDPPPWidget{
isReady() {
return this.layerValue && this.documentValue
}
openConfig() {
SDPPP.instance.senderConfig(this);
}
@@ -287,13 +330,14 @@
$sendButton
.addEventListener('click', () => {
if (!this.sendLayerValue)
if (!this.isReady())
this.openConfig();
else
this.action();
});
this.sendLayerValue = '';
this.layerValue = '';
this.documentValue = '';
this.render();
}
@@ -303,7 +347,7 @@
this.$el.className = "sdppp-send-widget";
this.$sendButton.className = "sdppp-send-button";
if (this.sendLayerValue) {
if (this.isReady()) {
this.$settingButton.style.display = 'flex';
this.$sendButton.textContent = "SDPPP send";
@@ -324,7 +368,8 @@
return button.querySelector('img').src
});
await SDPPP.instance.sendImage({
'layer': this.sendLayerValue,
'document': this.documentValue,
'layer': this.layerValue,
'sd': {
image_urls: selected_image
}
+1 -1
View File
@@ -844,7 +844,7 @@ async function sendImages(comfyURL, params) {
replace: true
};
if (!newLayerName) {
let bounds = layer.bounds;
let bounds = layerOrGroup.bounds;
if (bounds.width != jimp.bitmap.width || bounds.height != jimp.bitmap.height) {
if (bounds.width <= 1 && bounds.height <= 1) {
bounds.left = jimp.bitmap.width / 2;
+2 -2
View File
@@ -39,7 +39,7 @@ function autocrop(jimp) {
export default async function sendImages(comfyURL, params) {
const imageIds = params.image_ids
const documentIdentify = params.document_identify
const layerIdentify = params.layer_identify
const layerIdentify = params.layer_identify
let document = documentIdentify == SpeicialIDManager.SPECIAL_DOCUMENT_CURRENT ?
app.activeDocument :
@@ -104,7 +104,7 @@ export default async function sendImages(comfyURL, params) {
replace: true,
}
if (!newLayerName) {
let bounds = layer.bounds
let bounds = layerOrGroup.bounds
if (bounds.width != jimp.bitmap.width || bounds.height != jimp.bitmap.height) {
if (bounds.width <= 1 && bounds.height <= 1) {
bounds.left = jimp.bitmap.width / 2
+28 -16
View File
@@ -3,6 +3,7 @@ from modules import script_callbacks
from modules.ui_components import ToolButton
from sdppp_python.sdppp import SDPPP
from sdppp_python.apis import consumeImageCache
from sdppp_python.sd_data import get_sd_document_data, get_sd_special_get_bound_layer_options, get_sd_special_get_layer_options, get_sd_special_send_layer_options
import gradio as gr
refresh_symbol = '\U0001f504' # 🔄
@@ -13,27 +14,31 @@ def on_app_started(blocks: gr.Blocks, app):
sdppp = SDPPP()
sdppp.attach_to_SD(app)
def update_layers_dropdown(origin):
def update_document_dropdown(origin):
if not sdppp.has_ps_instance():
return ['==please connect SD in photoshop by sdppp first==']
instance = sdppp.get_ps_instance()
choices = instance.get_base_layers()
choices = list(get_sd_document_data().keys())
value = choices[0] if origin not in choices else origin
return gr.Dropdown.update(choices=choices, value=value, interactive=True)
def update_bounds_dropdown(origin):
def update_layers_dropdown(origin, document):
if not sdppp.has_ps_instance():
return ['==please connect SD in photoshop by sdppp first==']
instance = sdppp.get_ps_instance()
choices=instance.get_bounds_layers()
choices = [*get_sd_special_get_layer_options(), *(x['name'] for x in get_sd_document_data()[document]['layers'])]
value = choices[0] if origin not in choices else origin
return gr.Dropdown.update(choices=choices, value=value, interactive=True)
def update_send_layers_dropdown(origin):
def update_bounds_dropdown(origin, document):
if not sdppp.has_ps_instance():
return ['==please connect SD in photoshop by sdppp first==']
instance = sdppp.get_ps_instance()
choices = instance.get_set_layers()
choices = [*get_sd_special_get_bound_layer_options(), *(x['name'] for x in get_sd_document_data()[document]['layers'])]
value = choices[0] if origin not in choices else origin
return gr.Dropdown.update(choices=choices, value=value, interactive=True)
def update_send_layers_dropdown(origin, document):
if not sdppp.has_ps_instance():
return ['==please connect SD in photoshop by sdppp first==']
choices = [*get_sd_special_send_layer_options(), *(x['name'] for x in get_sd_document_data()[document]['layers'])]
value = choices[0] if origin not in choices else origin
return gr.Dropdown.update(choices=choices, value=value, interactive=True)
@@ -53,22 +58,33 @@ def on_app_started(blocks: gr.Blocks, app):
with blocks:
with gr.Box(elem_id=f"sdppp_getter_dialog", elem_classes="popup-dialog") as sdppp_getter_dialog:
with gr.Row():
get_document = gr.Dropdown(choices=['==please connect SD in photoshop by sdppp first=='], label="sdppp_get_document", elem_id=f'sdppp_get_document', interactive=True)
update_get_document = ToolButton(value=refresh_symbol, elem_id="refresh_sdppp_get_document", tooltip=f"refresh sdppp documents")
update_get_document.click(update_document_dropdown, inputs=[get_document], outputs=[get_document])
with gr.Row():
layers = gr.Dropdown(choices=['==please connect SD in photoshop by sdppp first=='], label="sdppp_layers", elem_id=f'sdppp_layers', interactive=True)
update_layers = ToolButton(value=refresh_symbol, elem_id="refresh_sdppp_layers", tooltip=f"refresh sdppp layers")
update_layers.click(update_layers_dropdown, inputs=[layers], outputs=[layers])
update_layers.click(update_layers_dropdown, inputs=[layers, get_document], outputs=[layers])
get_document.change(update_layers_dropdown, inputs=[layers, get_document], outputs=[layers])
with gr.Row():
bounds = gr.Dropdown(choices=['==please connect SD in photoshop by sdppp first=='], label="sdppp_bounds", elem_id=f'sdppp_bounds', interactive=True)
update_bounds = ToolButton(value=refresh_symbol, elem_id="refresh_sdppp_bounds", tooltip=f"refresh sdppp bounds")
update_bounds.click(update_bounds_dropdown, inputs=[bounds], outputs=[bounds])
update_bounds.click(update_bounds_dropdown, inputs=[bounds, get_document], outputs=[bounds])
get_document.change(update_bounds_dropdown, inputs=[bounds, get_document], outputs=[bounds])
with gr.Row():
close = gr.Button('Save And Run Get', variant='secondary', elem_id=f'sdppp_getter_dialog_close')
with gr.Box(elem_id=f"sdppp_sender_dialog", elem_classes="popup-dialog") as sdppp_sender_dialog:
with gr.Row():
send_document = gr.Dropdown(choices=['==please connect SD in photoshop by sdppp first=='], label="sdppp_send_document", elem_id=f'sdppp_send_document', interactive=True)
update_send_document = ToolButton(value=refresh_symbol, elem_id="refresh_sdppp_send_document", tooltip=f"refresh sdppp documents")
update_send_document.click(update_document_dropdown, inputs=[send_document], outputs=[send_document])
with gr.Row():
send_layers = gr.Dropdown(choices=['==please connect SD in photoshop by sdppp first=='], label="sdppp_send_layers", elem_id=f'sdppp_send_layers', interactive=True)
update_send_layers = ToolButton(value=refresh_symbol, elem_id="refresh_sdppp_send_layers", tooltip=f"refresh sdppp send layers")
update_send_layers.click(update_send_layers_dropdown, inputs=[send_layers], outputs=[send_layers])
update_send_layers.click(update_send_layers_dropdown, inputs=[send_layers, send_document], outputs=[send_layers])
send_document.change(update_send_layers_dropdown, inputs=[send_layers, send_document], outputs=[send_layers])
with gr.Row():
close = gr.Button('Save And Run Send', variant='secondary', elem_id=f'sdppp_sender_dialog_close')
sdppp_getter_dialog.visible = False
@@ -84,9 +100,5 @@ def on_app_started(blocks: gr.Blocks, app):
registerGradioGlobalJSFunction(f'show_getter_dialog', lambda i: sdppp_getter_dialog.update(visible=True), [sdppp_getter_dialog])
registerGradioGlobalJSFunction(f'show_sender_dialog', lambda i: sdppp_sender_dialog.update(visible=True), [sdppp_sender_dialog])
script_callbacks.on_app_started(on_app_started)
+8 -3
View File
@@ -64,7 +64,15 @@ def registerSDHTTPEndpoints(sdppp, app):
name = addImageCache(Image.open(BytesIO(image.file.read())))
return {'name': name}
from .sd_data import get_sd_document_data, set_sd_document_data, set_sd_special_get_bound_layer_options, set_sd_special_get_layer_options, set_sd_special_send_layer_options
def registerSocketEvents(sdppp, sio):
@sio.event
async def c_set_sd_options(sid, data={}):
set_sd_document_data(data['document_data'])
set_sd_special_get_layer_options(data['special_get_layer_options'])
set_sd_special_get_bound_layer_options(data['special_get_bound_layer_options'])
set_sd_special_send_layer_options(data['special_send_layer_options'])
# only emit by sd webui instance
@sio.event
async def c_get_image(sid, data={}):
@@ -76,9 +84,6 @@ def registerSocketEvents(sdppp, sio):
use_layer_bounds = data['use_layer_bounds']
sd_elem_id = data['sd']['elem_id']
# id = photoshopInstance.layer_name_to_id(layer)
# bounds_id = photoshopInstance.layer_name_to_id(use_layer_bounds, id)
upload_name, opacity = await photoshopInstance.get_image(
document_identify=document, layer_identify=layer, bound_layer_identify=use_layer_bounds
)
-3
View File
@@ -48,14 +48,11 @@ def define_comfyui_nodes(sdppp):
return np.random.rand()
else:
photoshopInstance = sdppp.get_ps_instance()
print(1)
is_changed, history_state_id = call_async_func_in_server_thread(
photoshopInstance.check_layer_bounds_combo_changed(layer, use_layer_bounds)
)
print(2)
if is_changed and history_state_id is None:
return np.random.rand()
print(3)
photoshopInstance.update_comfyui_last_value(layer, use_layer_bounds, history_state_id)
return history_state_id
+29
View File
@@ -0,0 +1,29 @@
# need refactor
sd_document_data = dict()
def set_sd_document_data(data):
global sd_document_data
sd_document_data = data
def get_sd_document_data():
return sd_document_data
sd_special_get_layer_options = []
def set_sd_special_get_layer_options(data):
global sd_special_get_layer_options
sd_special_get_layer_options = data
def get_sd_special_get_layer_options():
return sd_special_get_layer_options
sd_special_get_bound_layer_options = []
def set_sd_special_get_bound_layer_options(data):
global sd_special_get_bound_layer_options
sd_special_get_bound_layer_options = data
def get_sd_special_get_bound_layer_options():
return sd_special_get_bound_layer_options
sd_special_send_layer_options = []
def set_sd_special_send_layer_options(data):
global sd_special_send_layer_options
sd_special_send_layer_options = data
def get_sd_special_send_layer_options():
return sd_special_send_layer_options