Randomize seed in a hackish way

This commit is contained in:
space-nuko
2023-04-25 06:26:40 -07:00
parent 76a22c47f6
commit cd0fde0f55
8 changed files with 66 additions and 10 deletions

View File

@@ -116,7 +116,7 @@ export default class ComfyAPI extends EventTarget {
this.dispatchEvent(new CustomEvent("progress", { detail: msg.data })); this.dispatchEvent(new CustomEvent("progress", { detail: msg.data }));
break; break;
case "executing": case "executing":
this.dispatchEvent(new CustomEvent("executing", { detail: msg.data.node })); this.dispatchEvent(new CustomEvent("executing", { detail: msg.data }));
break; break;
case "executed": case "executed":
this.dispatchEvent(new CustomEvent("executed", { detail: msg.data })); this.dispatchEvent(new CustomEvent("executed", { detail: msg.data }));

View File

@@ -206,6 +206,8 @@ export default class ComfyApp {
private registerNodeTypeOverrides() { private registerNodeTypeOverrides() {
ComfyApp.node_type_overrides["SaveImage"] = nodes.ComfySaveImageNode; ComfyApp.node_type_overrides["SaveImage"] = nodes.ComfySaveImageNode;
ComfyApp.node_type_overrides["PreviewImage"] = nodes.ComfyPreviewImageNode; ComfyApp.node_type_overrides["PreviewImage"] = nodes.ComfyPreviewImageNode;
ComfyApp.node_type_overrides["KSampler"] = nodes.ComfyKSamplerNode;
ComfyApp.node_type_overrides["KSamplerAdvanced"] = nodes.ComfyKSamplerAdvancedNode;
} }
private registerWidgetTypeOverrides() { private registerWidgetTypeOverrides() {
@@ -386,7 +388,11 @@ export default class ComfyApp {
}); });
this.api.addEventListener("executing", ({ detail }: CustomEvent) => { this.api.addEventListener("executing", ({ detail }: CustomEvent) => {
queueState.executingUpdated(detail); queueState.executingUpdated(detail.node);
const node = this.lGraph.getNodeById(detail.node) as ComfyGraphNode;
if (node?.onExecuting) {
node.onExecuting();
}
this.lGraph.setDirtyCanvas(true, false); this.lGraph.setDirtyCanvas(true, false);
}); });

View File

@@ -10,5 +10,6 @@ export default class ComfyGraphNode extends LGraphNode {
*/ */
virtualWidgets: ComfyWidget[] = []; virtualWidgets: ComfyWidget[] = [];
onExecuting?(): void;
onExecuted?(output: any): void; onExecuted?(output: any): void;
} }

View File

@@ -0,0 +1,34 @@
import { get } from 'svelte/store';
import ComfyGraphNode from "./ComfyGraphNode";
import widgetState from "$lib/stores/widgetState"
/*
* Autorefreshes seed (until bangs/main inlets are implemented)
*/
class ComfyBaseKSamplerNode extends ComfyGraphNode {
constructor(title?: any) {
super(title)
}
override onExecuting() {
console.log(this);
const widget = widgetState.findWidgetByName(this.id, "seed")
if (!widget)
return;
// TODO cleanup&remove
let min = widget.widget.options.min;
let max = widget.widget.options.max;
// limit to something that javascript can handle
max = Math.min(1125899906842624, max);
min = Math.max(-1125899906842624, min);
const range = (max - min) / (widget.widget.options.step);
const v = Math.floor(Math.floor(Math.random() * range) * (widget.widget.options.step) + min);
widget.widget.value = v;
widgetState.widgetStateChanged(this.id, widget.widget);
}
}
export class ComfyKSamplerNode extends ComfyBaseKSamplerNode {}
export class ComfyKSamplerAdvancedNode extends ComfyBaseKSamplerNode {}

View File

@@ -1,2 +1,3 @@
export { default as ComfyReroute } from "./ComfyReroute" export { default as ComfyReroute } from "./ComfyReroute"
export { ComfySaveImageNode, ComfyPreviewImageNode } from "./ComfyImageNodes" export { ComfySaveImageNode, ComfyPreviewImageNode } from "./ComfyImageNodes"
export { ComfyKSamplerNode, ComfyKSamplerAdvancedNode } from "./ComfyKSamplerNodes"

View File

@@ -34,7 +34,8 @@ type WidgetStateOps = {
nodeAdded: (node: LGraphNode) => void, nodeAdded: (node: LGraphNode) => void,
nodeRemoved: (node: LGraphNode) => void, nodeRemoved: (node: LGraphNode) => void,
configureFinished: (graph: LGraph) => void, configureFinished: (graph: LGraph) => void,
widgetStateChanged: (widget: ComfyWidget<any, any>) => void, widgetStateChanged: (nodeId: number, widget: IWidget<any, any>) => void,
findWidgetByName: (nodeId: number, name: string) => WidgetUIState | null,
clear: () => void, clear: () => void,
} }
@@ -78,9 +79,9 @@ function nodeRemoved(node: LGraphNode) {
store.set(state) store.set(state)
} }
function widgetStateChanged(widget: ComfyWidget<any, any>) { function widgetStateChanged(nodeId: number, widget: IWidget<any, any>) {
const state = get(store) const state = get(store)
const entries = state[widget.node.id] const entries = state[nodeId]
if (entries) { if (entries) {
let widgetState = entries.find(e => e.widget === widget); let widgetState = entries.find(e => e.widget === widget);
if (widgetState) { if (widgetState) {
@@ -116,6 +117,15 @@ function configureFinished(graph: LGraph) {
store.set(state) store.set(state)
} }
function findWidgetByName(nodeId: number, name: string): WidgetUIState | null {
let state = get(store);
if (!(nodeId in state))
return null;
return state[nodeId].find((v) => v.widget.name === name);
}
const widgetStateStore: WritableWidgetStateStore = const widgetStateStore: WritableWidgetStateStore =
{ {
...store, ...store,
@@ -123,6 +133,7 @@ const widgetStateStore: WritableWidgetStateStore =
nodeRemoved, nodeRemoved,
widgetStateChanged, widgetStateChanged,
configureFinished, configureFinished,
findWidgetByName,
clear clear
} }
export default widgetStateStore; export default widgetStateStore;

View File

@@ -26,7 +26,7 @@ export default abstract class ComfyWidget<T = any, V = any> implements IWidget<T
setValue(value: V) { setValue(value: V) {
this.value = value; this.value = value;
widgetState.widgetStateChanged(this); widgetState.widgetStateChanged(this.node.id, this);
} }
draw?(ctx: CanvasRenderingContext2D, node: LGraphNode, width: number, posY: number, height: number): void; draw?(ctx: CanvasRenderingContext2D, node: LGraphNode, width: number, posY: number, height: number): void;

View File

@@ -6,10 +6,13 @@
let itemValue: WidgetUIStateStore | null = null; let itemValue: WidgetUIStateStore | null = null;
let option: number | null = null; let option: number | null = null;
$: if (item && !option) { $: if (item) {
if (!itemValue)
itemValue = item.value; itemValue = item.value;
option = get(item.value) updateOption(); // don't react on option
}
function updateOption() {
option = get(itemValue);
} }
function onRelease(e: Event) { function onRelease(e: Event) {