Skip to content

Commit

Permalink
feat: support random seed before generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbtucker committed Mar 26, 2023
1 parent 48efade commit ddde8be
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
2 changes: 1 addition & 1 deletion web/extensions/core/widgetInputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ app.registerExtension({
}

if (widget.type === "number") {
addRandomizeWidget(this, widget, "Random after every gen");
addRandomizeWidget(this, widget, "randomize", "off");
}

// When our value changes, update other widgets to reflect our changes
Expand Down
17 changes: 16 additions & 1 deletion web/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,22 @@ class ComfyApp {

async queuePrompt(number, batchCount = 1) {
for (let i = 0; i < batchCount; i++) {
const p = await this.graphToPrompt();
let p = await this.graphToPrompt();

for (const n of p.workflow.nodes) {
const node = graph.getNodeById(n.id);
if (node.widgets) {
for (const widget of node.widgets) {
// Allow widgets to run callbacks before a prompt has been queued
// e.g. random seed before every gen
if (widget.beforeQueued) {
widget.beforeQueued();
}
}
}
}

p = await this.graphToPrompt();

try {
await api.queuePrompt(number, p);
Expand Down
2 changes: 1 addition & 1 deletion web/scripts/defaultGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const defaultGraph = {
],
outputs: [{ name: "LATENT", type: "LATENT", links: [7], slot_index: 0 }],
properties: {},
widgets_values: [8566257, true, 20, 8, "euler", "normal", 1],
widgets_values: [8566257, "after generation", 20, 8, "euler", "normal", 1],
},
{
id: 8,
Expand Down
44 changes: 28 additions & 16 deletions web/scripts/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,45 @@ function getNumberDefaults(inputData, defaultStep) {
}

export function addRandomizeWidget(node, targetWidget, name, defaultValue = false) {
const randomize = node.addWidget("toggle", name, defaultValue, function (v) {}, {
on: "enabled",
off: "disabled",
const randomize = node.addWidget("combo", name, defaultValue, function (v) {}, {
values: ["after generation", "before generation", "off"],
serialize: false, // Don't include this in prompt.
});

randomize.afterQueued = () => {
if (randomize.value) {
const min = targetWidget.options?.min;
let max = targetWidget.options?.max;
if (min != null || max != null) {
if (max) {
// limit max to something that javascript can handle
max = Math.min(1125899906842624, max);
}
targetWidget.value = Math.floor(Math.random() * ((max ?? 9999999999) - (min ?? 0) + 1) + (min ?? 0));
} else {
targetWidget.value = Math.floor(Math.random() * 1125899906842624);
const generateSeed = () => {
const min = targetWidget.options?.min;
let max = targetWidget.options?.max;
if (min != null || max != null) {
if (max) {
// limit max to something that javascript can handle
max = Math.min(1125899906842624, max);
}
targetWidget.value = Math.floor(Math.random() * ((max ?? 9999999999) - (min ?? 0) + 1) + (min ?? 0));
} else {
targetWidget.value = Math.floor(Math.random() * 1125899906842624);
}
};

randomize.beforeQueued = () => {
if (randomize.value === "before generation") {
generateSeed()
}
};

randomize.afterQueued = () => {
// Check if value is true for backward compatibilty with the previous toggle
// version of this widget.
if (randomize.value === "after generation" || randomize.value === true) {
generateSeed()
}
};

return randomize;
}

function seedWidget(node, inputName, inputData) {
const seed = ComfyWidgets.INT(node, inputName, inputData);
const randomize = addRandomizeWidget(node, seed.widget, "Random seed after every gen", true);
const randomize = addRandomizeWidget(node, seed.widget, "random seed", true);

seed.widget.linkedWidgets = [randomize];
return { widget: seed, randomize };
Expand Down

0 comments on commit ddde8be

Please sign in to comment.