Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Image to 3D: Refine detail also runs when the photo comes from an earlier step

Claude7501bdbc4abe
diff --git a/src/server/chains.ts b/src/server/chains.ts
index 9be2a3c2..f1a45f77 100644
--- a/src/server/chains.ts
+++ b/src/server/chains.ts
@@ -431,13 +431,21 @@ export function createStillTo3d(original: GenerationRequest, model: ModelDef, ro
* the coarse mesh from the photo, then Hi3D refines that mesh's geometry with the same photo (chain slot model3d) at the
* resolution asked for. Both results stay (the refined one is geometry only), in one batch.
*/
-export function createRefinedModel(original: GenerationRequest, model: ModelDef): Generation[] {
+export function createRefinedModel(original: GenerationRequest, model: ModelDef, waits: { batchId?: string; dependsOn?: string | string[] } = {}): Generation[] {
const resolution = chainedRefine(original);
if (resolution === null) throw new Error("No refine step was asked for");
// Hi3D runs on your GPU only (nobody hosts the open model): the model before it runs there too.
if (model.engine !== "open") throw new Error("Refine detail runs on your GPU only: pick an open model, or turn Refine detail off");
- const photo = original.inputs?.images?.[0];
- if (!photo) throw new Error("Add a photo of the object: Hi3D refines the model from it");
+ // The model's photo may come from an earlier step (a Canvas image node, an Assist plan's still): Hi3D then waits for
+ // that step too, and reads the same photo the model was built from.
+ const deps = waits.dependsOn === undefined ? [] : Array.isArray(waits.dependsOn) ? waits.dependsOn : [waits.dependsOn];
+ const slots: ChainSlot[] = deps.map((_, i) => original.chain?.[i] ?? { slot: "images", position: original.chainFrom ?? "last" });
+ const own = original.inputs?.images?.[0];
+ const fed = slots.findIndex((s) => s.slot === "images");
+ const fedFirst = fed >= 0 && (slots[fed].at === 0 || (slots[fed].at === undefined && slots[fed].position === "first"));
+ const photoStep = fed >= 0 && (!own || fedFirst) ? deps[fed] : undefined;
+ const photo = photoStep ? undefined : own;
+ if (!photo && !photoStep) throw new Error("Add a photo of the object: Hi3D refines the model from it");
const op = original.op ?? "image-to-3d";
assertOneMode([
{ label: model.name, modelId: model.id, op, engine: "open" },
@@ -447,10 +455,10 @@ export function createRefinedModel(original: GenerationRequest, model: ModelDef)
delete extra[HI3D_CHAIN_PARAM];
const params: GenParams = { ...original.params, extra };
if (!Object.keys(extra).length) delete params.extra;
- const batchId = nanoid(10);
+ const batchId = waits.batchId ?? nanoid(10);
const built = createGenerations(
{ ...original, op, ...(Object.keys(params).length ? { params } : { params: undefined }), step: { label: "3D model", index: 0, of: 2 } },
- { batchId },
+ { batchId, ...(deps.length && { dependsOn: deps }) },
)[0];
try {
const seed = original.params?.seed;
@@ -460,12 +468,12 @@ export function createRefinedModel(original: GenerationRequest, model: ModelDef)
modelId: HI3D_MODEL,
op: HI3D_OP,
engine: "open",
- inputs: { images: [photo] },
+ ...(photo && { inputs: { images: [photo] } }),
params: { extra: { [HI3D_PARAM]: resolution }, ...(seed !== undefined && { seed }) },
- chain: [{ slot: "model3d" }],
+ chain: photoStep ? [{ slot: "model3d" }, { slot: "images" }] : [{ slot: "model3d" }],
step: { label: "Refined detail", index: 1, of: 2 },
},
- { batchId, dependsOn: built.id },
+ { batchId, dependsOn: photoStep ? [built.id, photoStep] : built.id },
)[0];
return withAsked([refined, built], original, "open");
} catch (e) {
diff --git a/src/server/generations.ts b/src/server/generations.ts
index f947f438..732303b4 100644
--- a/src/server/generations.ts
+++ b/src/server/generations.ts
@@ -924,8 +924,9 @@ export function createGenerations(given: GenerationRequest, opts: CreateOptions
return createKeyframeFilm(original, shotModel, filmRoute, { batchId: opts.batchId, dependsOn: opts.dependsOn });
}
// Image to 3D's "Refine detail" (open engine): the model, then Hi3D's refine of its mesh with the same photo.
- if (!opts.dependsOn && shotModel && shotModel.kind === "model3d" && original.op !== "refine-3d" && chainedRefine(original) !== null) {
- return createRefinedModel(original, shotModel);
+ // Its photo may come from an earlier step (a Canvas image node, an Assist plan's still): the refine then waits for it too.
+ if (shotModel && shotModel.kind === "model3d" && original.op !== "refine-3d" && chainedRefine(original) !== null) {
+ return createRefinedModel(original, shotModel, { batchId: opts.batchId, dependsOn: opts.dependsOn });
}
// Text to 3D on the open engine: a still first, then the 3D model of it. The operation is read as the planner
// reads it (Assist, MCP and API clients usually leave op out).
diff --git a/tests/hi3d-refine.test.ts b/tests/hi3d-refine.test.ts
index 3fe53164..4aebfe02 100644
--- a/tests/hi3d-refine.test.ts
+++ b/tests/hi3d-refine.test.ts
@@ -128,6 +128,42 @@ describe("the chain per engine", () => {
expect(refined.request.inputs?.images).toEqual(["/api/files/f.png"]);
});
+ const deps = (id: string) => {
+ const v = (db().prepare("SELECT depends_on FROM generations WHERE id = ?").get(id) as { depends_on: string | null }).depends_on;
+ return !v ? [] : v.startsWith("[") ? (JSON.parse(v) as string[]) : [v];
+ };
+
+ it("open, with the photo from an earlier step: the model waits for it, and Hi3D waits for the model and the same photo", async () => {
+ const [still] = gens.createGenerations({ feature: "image", modelId: "oss-z-image-turbo", prompt: "A brass desk lamp" });
+ const asked = { feature: "canvas", modelId: "oss-pixal3d", op: "image-to-3d" as const, params: { extra: { hi3dRefine: 512 } } };
+ const [refined, built] = gens.createGenerations(asked, { batchId: "b-hi3d-up", dependsOn: [still.id] });
+ expect(built).toMatchObject({ modelId: "oss-pixal3d", op: "image-to-3d", batchId: "b-hi3d-up" });
+ expect(built.request.params?.extra?.hi3dRefine).toBeUndefined();
+ expect(deps(built.id)).toEqual([still.id]);
+ expect(refined).toMatchObject({ modelId: "oss-hi3d-refine", op: "refine-3d", batchId: "b-hi3d-up" });
+ expect(refined.request.chain).toEqual([{ slot: "model3d" }, { slot: "images" }]);
+ expect(refined.request.inputs?.images ?? []).toEqual([]);
+ expect(refined.request.params?.extra).toEqual({ meshResolution: 512 });
+ expect(deps(refined.id)).toEqual([built.id, still.id]);
+ // Demo mode runs the three steps through: Hi3D gets the model's mesh and the still.
+ const until = Date.now() + 40_000;
+ while (!["succeeded", "failed", "canceled"].includes(gens.getGeneration(refined.id)!.status) && Date.now() < until) await new Promise((r) => setTimeout(r, 300));
+ const done = gens.getGeneration(refined.id)!;
+ expect(done.status, done.message ?? "").toBe("succeeded");
+ expect(done.request.inputs?.model3d).toBe(gens.getGeneration(built.id)!.outputs[0].url);
+ expect(done.request.inputs?.images).toEqual([gens.getGeneration(still.id)!.outputs[0].url]);
+ }, 60_000);
+
+ it("open, with its own photo and an earlier step feeding something else: Hi3D reads the photo it was given", () => {
+ const [still] = gens.createGenerations({ feature: "image", modelId: "oss-z-image-turbo", prompt: "A brass desk lamp, side" });
+ const asked = { feature: "canvas", modelId: "oss-pixal3d", op: "image-to-3d" as const, inputs: { images: [photo] }, params: { extra: { hi3dRefine: 1024 } } };
+ const [refined, built] = gens.createGenerations(asked, { dependsOn: [still.id] });
+ expect(deps(built.id)).toEqual([still.id]);
+ expect(refined.request.chain).toEqual([{ slot: "model3d" }]);
+ expect(refined.request.inputs?.images).toEqual([photo]);
+ expect(deps(refined.id)).toEqual([built.id]);
+ });
+
it("off: one step, as before", () => {
const steps = gens.createGenerations({ ...m3d.model3dRequest({ engine: "open", mode: "image", photo }), params: { extra: { hi3dRefine: "off" } } });
expect(steps.map((g) => g.modelId)).toEqual(["oss-pixal3d"]);
@@ -353,4 +389,27 @@ describe("Assist and MCP", () => {
const [r] = JSON.parse(out.content as string) as { started: string[] }[];
expect(r.started.map((id) => gens.getGeneration(id)!.modelId).sort()).toEqual(["oss-hi3d-refine", "oss-trellis2"]);
});
+
+ it("a still, then an open image-to-3D job with mesh_resolution: the refine still runs, on the still", async () => {
+ const tools = await import("@/server/assist/tools");
+ const out = await tools.executeTool(
+ "generate",
+ { summary: "Lamp", jobs: [{ model_id: "oss-z-image-turbo", prompt: "A brass desk lamp" }, { model_id: "oss-pixal3d", prompt: "", mesh_resolution: 1536, after: 0 }] },
+ { engine: "open" },
+ );
+ expect(out.isError, String(out.content)).toBeFalsy();
+ const { results } = JSON.parse(out.content as string) as { results: { started: string[] }[] };
+ const still = gens.getGeneration(results[0].started[0])!;
+ // The job's result is the refined model; the model it refines is the step before it.
+ const refined = gens.getGeneration(results[1].started[0])!;
+ expect(still.modelId).toBe("oss-z-image-turbo");
+ expect(refined).toMatchObject({ modelId: "oss-hi3d-refine", op: "refine-3d" });
+ expect(refined.request.params?.extra).toEqual({ meshResolution: 1536 });
+ expect(refined.request.chain).toEqual([{ slot: "model3d" }, { slot: "images" }]);
+ const dep = (id: string) => (db().prepare("SELECT depends_on FROM generations WHERE id = ?").get(id) as { depends_on: string }).depends_on;
+ const [builtId, photoId] = JSON.parse(dep(refined.id)) as string[];
+ expect(photoId).toBe(still.id);
+ expect(gens.getGeneration(builtId)).toMatchObject({ modelId: "oss-pixal3d", op: "image-to-3d" });
+ expect(dep(builtId)).toBe(still.id);
+ });
});