diff --git a/comfy/__init__.py b/comfy/__init__.py index fa72c48..d785f0a 100644 --- a/comfy/__init__.py +++ b/comfy/__init__.py @@ -288,6 +288,17 @@ async def websocket_handler(request): PhotoshopInstance(ws) await PhotoshopInstance.instance.run_server_loop() +def get_layers_str(): + layer_strs = [] + bounds_strs = [] + if PhotoshopInstance.instance is not None: + layer_strs = list(map(lambda layer: f"{layer['name']} (id:{layer['id']})", PhotoshopInstance.instance.layers)) + layer_strs.insert(0, 'Use canvas') + bounds_strs = list(layer_strs) + bounds_strs.insert(0, 'Use selection') + bounds_strs.insert(0, 'Same as layer') + return layer_strs, bounds_strs + class GetImageFromPhotoshopLayerNode: @classmethod def VALIDATE_INPUTS(s, layer): @@ -309,14 +320,7 @@ class GetImageFromPhotoshopLayerNode: @classmethod def INPUT_TYPES(cls): - layer_strs = [] - bounds_strs = [] - if PhotoshopInstance.instance is not None: - layer_strs = list(map(lambda layer: f"{layer['name']} (id:{layer['id']})", PhotoshopInstance.instance.layers)) - layer_strs.insert(0, 'Canvas') - bounds_strs = list(layer_strs) - bounds_strs.insert(0, 'Use selection') - bounds_strs.insert(0, 'Same as layer') + layer_strs, bounds_strs = get_layers_str() return { "required": { "layer": (layer_strs, {"default": layer_strs[0] if len(layer_strs) > 0 else None}), @@ -446,6 +450,14 @@ async def reset_changes(request): PhotoshopInstance.instance.reset_change_tracker() return web.json_response({}, content_type='application/json') +@PromptServer.instance.routes.get("/sd-ppp/getlayers") +async def get_layers(request): + layer_strs = [] + bounds_strs = [] + if (PhotoshopInstance.instance is not None): + layer_strs, bounds_strs = get_layers_str() + return web.json_response({'layer_strs': layer_strs, 'bounds_strs': bounds_strs}, content_type='application/json') + NODE_CLASS_MAPPINGS = { 'Get Image From Photoshop Layer': GetImageFromPhotoshopLayerNode, 'Send Images To Photoshop': SendImageToPhotoshopLayerNode, diff --git a/comfy/plugins/sd-ppp.js b/comfy/plugins/sd-ppp.js index ecd3977..b577b87 100644 --- a/comfy/plugins/sd-ppp.js +++ b/comfy/plugins/sd-ppp.js @@ -1,6 +1,9 @@ import { app } from "../../scripts/app.js"; import { api } from "../../scripts/api.js" +let layerStrs = []; +let boundsStrs = []; + console.log("[sd-ppp]", "Loading js extension"); app.registerExtension({ name: "Comfy.SD-PPP", @@ -10,6 +13,24 @@ app.registerExtension({ await api.fetchApi(`/sd-ppp/resetchanges`); setInterval(checkChanges, 1000); }, + async beforeRegisterNodeDef(nodeType, nodeData, app) { + if (nodeType.comfyClass === 'Get Image From Photoshop Layer') { + const onSelected = nodeType.prototype.onSelected; + const onMouseEnter = nodeType.prototype.onMouseEnter; + let this_handler = function() { + this.widgets[0].options.values = layerStrs; + this.widgets[1].options.values = boundsStrs; + } + nodeType.prototype.onSelected = async function(...args) { + if(onSelected) await onSelected.call(this, ...args); + this_handler.call(this); + } + nodeType.prototype.onMouseEnter = async function(...args) { + if(onMouseEnter) await onMouseEnter.call(this, ...args); + this_handler.call(this); + } + } + } }); const SDPPPNodes = [ @@ -18,12 +39,32 @@ const SDPPPNodes = [ 'Send Images To Photoshop Set Layer', ] async function checkChanges() { - const currentState = app.graph.serialize(); - const mode0NodeTypes = currentState.nodes.filter(node => node.mode == 0).map(node => node.type); - const containsSDPPPNodes = mode0NodeTypes.some(nodeType => SDPPPNodes.includes(nodeType)); - if (!containsSDPPPNodes) return; - const res = await api.fetchApi(`/sd-ppp/checkchanges`); - const json = await res.json() - if (!json.is_changed) return; - api.dispatchEvent(new CustomEvent("graphChanged")); + await checkHistoryChanges(); + await refreshLayers(); +} + +async function refreshLayers() { + try { + const res = await api.fetchApi(`/sd-ppp/getlayers`); + const json = await res.json() + layerStrs = json.layer_strs; + boundsStrs = json.bounds_strs; + } catch (e) { + console.error("[sd-ppp]", "Failed to get layers", e); + } +} + +async function checkHistoryChanges() { + try { + const currentState = app.graph.serialize(); + const mode0NodeTypes = currentState.nodes.filter(node => node.mode == 0).map(node => node.type); + const containsSDPPPNodes = mode0NodeTypes.some(nodeType => SDPPPNodes.includes(nodeType)); + if (!containsSDPPPNodes) return; + const res = await api.fetchApi(`/sd-ppp/checkchanges`); + const json = await res.json() + if (!json.is_changed) return; + api.dispatchEvent(new CustomEvent("graphChanged")); + } catch (e) { + console.error("[sd-ppp]", "Failed to check changes", e); + } } \ No newline at end of file