use filename as workflow name

This commit is contained in:
pythongosssss 2024-03-20 19:14:27 +00:00
parent 0052c6663c
commit c41efe65e1

View File

@ -1740,11 +1740,26 @@ export class ComfyApp {
}); });
} }
async changeWorkflow(callback, workflow = null) {
try {
this.workflowManager.activeWorkflow?.changeTracker?.store()
} catch (error) {
console.error(error);
}
await callback();
try {
this.workflowManager.setWorkflow(workflow);
this.workflowManager.activeWorkflow?.track()
} catch (error) {
console.error(error);
}
}
/** /**
* Populates the graph with the specified workflow data * Populates the graph with the specified workflow data
* @param {*} graphData A serialized graph object * @param {*} graphData A serialized graph object
* @param { boolean } clean If the graph state, e.g. images, should be cleared * @param { boolean } clean If the graph state, e.g. images, should be cleared
* @param { import("./workflows.js").ComfyWorkflowInstance | null } name The workflow * @param { import("./workflows.js").ComfyWorkflowInstance | null } workflow The workflow
*/ */
async loadGraphData(graphData, clean = true, workflow = null) { async loadGraphData(graphData, clean = true, workflow = null) {
try { try {
@ -2133,28 +2148,38 @@ export class ComfyApp {
* @param {File} file * @param {File} file
*/ */
async handleFile(file) { async handleFile(file) {
const removeExt = f => {
if(!f) return f;
const p = f.lastIndexOf(".");
if(p === -1) return f;
return f.substring(0, p);
};
const fileName = removeExt(file.name);
if (file.type === "image/png") { if (file.type === "image/png") {
const pngInfo = await getPngMetadata(file); const pngInfo = await getPngMetadata(file);
if (pngInfo) { if (pngInfo) {
if (pngInfo.workflow) { if (pngInfo.workflow) {
await this.loadGraphData(JSON.parse(pngInfo.workflow)); await this.loadGraphData(JSON.parse(pngInfo.workflow), true, fileName);
} else if (pngInfo.prompt) { } else if (pngInfo.prompt) {
this.loadApiJson(JSON.parse(pngInfo.prompt)); this.loadApiJson(JSON.parse(pngInfo.prompt), fileName);
} else if (pngInfo.parameters) { } else if (pngInfo.parameters) {
importA1111(this.graph, pngInfo.parameters); this.changeWorkflow(() => {
importA1111(this.graph, pngInfo.parameters);
}, fileName)
} }
} }
} else if (file.type === "image/webp") { } else if (file.type === "image/webp") {
const pngInfo = await getWebpMetadata(file); const pngInfo = await getWebpMetadata(file);
if (pngInfo) { if (pngInfo) {
if (pngInfo.workflow) { if (pngInfo.workflow) {
this.loadGraphData(JSON.parse(pngInfo.workflow)); this.loadGraphData(JSON.parse(pngInfo.workflow), true, fileName);
} else if (pngInfo.Workflow) { } else if (pngInfo.Workflow) {
this.loadGraphData(JSON.parse(pngInfo.Workflow)); // Support loading workflows from that webp custom node. this.loadGraphData(JSON.parse(pngInfo.Workflow), true, fileName); // Support loading workflows from that webp custom node.
} else if (pngInfo.prompt) { } else if (pngInfo.prompt) {
this.loadApiJson(JSON.parse(pngInfo.prompt)); this.loadApiJson(JSON.parse(pngInfo.prompt), fileName);
} else if (pngInfo.Prompt) { } else if (pngInfo.Prompt) {
this.loadApiJson(JSON.parse(pngInfo.Prompt)); // Support loading prompts from that webp custom node. this.loadApiJson(JSON.parse(pngInfo.Prompt), fileName); // Support loading prompts from that webp custom node.
} }
} }
} else if (file.type === "application/json" || file.name?.endsWith(".json")) { } else if (file.type === "application/json" || file.name?.endsWith(".json")) {
@ -2164,16 +2189,16 @@ export class ComfyApp {
if (jsonContent?.templates) { if (jsonContent?.templates) {
this.loadTemplateData(jsonContent); this.loadTemplateData(jsonContent);
} else if(this.isApiJson(jsonContent)) { } else if(this.isApiJson(jsonContent)) {
this.loadApiJson(jsonContent); this.loadApiJson(jsonContent, fileName);
} else { } else {
await this.loadGraphData(jsonContent); await this.loadGraphData(jsonContent, true, fileName);
} }
}; };
reader.readAsText(file); reader.readAsText(file);
} else if (file.name?.endsWith(".latent") || file.name?.endsWith(".safetensors")) { } else if (file.name?.endsWith(".latent") || file.name?.endsWith(".safetensors")) {
const info = await getLatentMetadata(file); const info = await getLatentMetadata(file);
if (info.workflow) { if (info.workflow) {
await this.loadGraphData(JSON.parse(info.workflow)); await this.loadGraphData(JSON.parse(info.workflow), true, fileName);
} else if (info.prompt) { } else if (info.prompt) {
this.loadApiJson(JSON.parse(info.prompt)); this.loadApiJson(JSON.parse(info.prompt));
} }
@ -2184,54 +2209,54 @@ export class ComfyApp {
return Object.values(data).every((v) => v.class_type); return Object.values(data).every((v) => v.class_type);
} }
loadApiJson(apiData) { loadApiJson(apiData, fileName) {
const missingNodeTypes = Object.values(apiData).filter((n) => !LiteGraph.registered_node_types[n.class_type]); const missingNodeTypes = Object.values(apiData).filter((n) => !LiteGraph.registered_node_types[n.class_type]);
if (missingNodeTypes.length) { if (missingNodeTypes.length) {
this.showMissingNodesError(missingNodeTypes.map(t => t.class_type), false); this.showMissingNodesError(missingNodeTypes.map(t => t.class_type), false);
return; return;
} }
this.changeWorkflow(() => {
const ids = Object.keys(apiData);
app.graph.clear();
for (const id of ids) {
const data = apiData[id];
const node = LiteGraph.createNode(data.class_type);
node.id = isNaN(+id) ? id : +id;
graph.add(node);
}
const ids = Object.keys(apiData); for (const id of ids) {
app.graph.clear(); const data = apiData[id];
for (const id of ids) { const node = app.graph.getNodeById(id);
const data = apiData[id]; for (const input in data.inputs ?? {}) {
const node = LiteGraph.createNode(data.class_type); const value = data.inputs[input];
node.id = isNaN(+id) ? id : +id; if (value instanceof Array) {
graph.add(node); const [fromId, fromSlot] = value;
} const fromNode = app.graph.getNodeById(fromId);
let toSlot = node.inputs?.findIndex((inp) => inp.name === input);
for (const id of ids) { if (toSlot == null || toSlot === -1) {
const data = apiData[id]; try {
const node = app.graph.getNodeById(id); // Target has no matching input, most likely a converted widget
for (const input in data.inputs ?? {}) { const widget = node.widgets?.find((w) => w.name === input);
const value = data.inputs[input]; if (widget && node.convertWidgetToInput?.(widget)) {
if (value instanceof Array) { toSlot = node.inputs?.length - 1;
const [fromId, fromSlot] = value; }
const fromNode = app.graph.getNodeById(fromId); } catch (error) {}
let toSlot = node.inputs?.findIndex((inp) => inp.name === input); }
if (toSlot == null || toSlot === -1) { if (toSlot != null || toSlot !== -1) {
try { fromNode.connect(fromSlot, node, toSlot);
// Target has no matching input, most likely a converted widget }
const widget = node.widgets?.find((w) => w.name === input); } else {
if (widget && node.convertWidgetToInput?.(widget)) { const widget = node.widgets?.find((w) => w.name === input);
toSlot = node.inputs?.length - 1; if (widget) {
} widget.value = value;
} catch (error) {} widget.callback?.(value);
} }
if (toSlot != null || toSlot !== -1) {
fromNode.connect(fromSlot, node, toSlot);
}
} else {
const widget = node.widgets?.find((w) => w.name === input);
if (widget) {
widget.value = value;
widget.callback?.(value);
} }
} }
} }
} app.graph.arrange();
}, fileName);
app.graph.arrange();
} }
/** /**