Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.
C++ 3d3d-sculptingcppdear-imguidigital-sculptingfbxgame-developmentgaussian-splattinggltfimage-to-3dmcpopenglopenusdsculptingtext-to-3dunreal-engine
ComfyUI as a Generate 3D service: run any workflow on your own server
islandmed135447e33b57
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 4e157fb6..d9ddbb9c 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -79,7 +79,10 @@ endif()# --- Bring-your-own-AI (brush assistant). HTTPS via WinHTTP or libcurl. ---add_library(sculptforge_ai STATIC src/ai/Http.cpp src/ai/BrushAssistant.cpp src/ai/Generator3D.cpp- src/ai/ImageTo3DService.cpp src/ai/GradioClient.cpp src/ai/AiCommands.cpp src/link/Socket.cpp src/link/UnrealLink.cpp)+ src/ai/ImageTo3DService.cpp src/ai/GradioClient.cpp src/ai/ComfyUI.cpp src/ai/AiCommands.cpp src/link/Socket.cpp+ src/link/UnrealLink.cpp)+# Lets a build run from its tree find the bundled ComfyUI workflows (integrations/comfyui/workflows).+target_compile_definitions(sculptforge_ai PRIVATE SF_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")find_package(Threads REQUIRED)target_link_libraries(sculptforge_ai PUBLIC sculptforge_core Threads::Threads)if(WIN32)@@ -198,7 +201,16 @@ else()set(SF_DOC_DIR ${CMAKE_INSTALL_DOCDIR})endif()install(FILES LICENSE THIRD_PARTY_NOTICES.md README.md CHANGELOG.md DESTINATION ${SF_DOC_DIR})-install(FILES docs/ai-services.md docs/unreal.md docs/generators.md DESTINATION ${SF_DOC_DIR}/docs)+install(FILES docs/ai-services.md docs/unreal.md docs/generators.md docs/comfyui.md DESTINATION ${SF_DOC_DIR}/docs)+# Bundled ComfyUI workflows, where the Generate 3D window looks for them (see src/ai/ComfyUI.cpp).+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/integrations/comfyui/workflows)+ if(APPLE)+ set(SF_COMFY_DIR SculptForge.app/Contents/Resources/comfyui)+ else()+ set(SF_COMFY_DIR ${CMAKE_INSTALL_DATADIR}/sculptforge/comfyui)+ endif()+ install(DIRECTORY integrations/comfyui/workflows DESTINATION ${SF_COMFY_DIR})+endif()set(CPACK_PACKAGE_NAME "sculptforge")set(CPACK_PACKAGE_VENDOR "The SculptForge contributors")diff --git a/docs/ai-services.md b/docs/ai-services.mdindex e20cb36c..3cfaaff7 100644--- a/docs/ai-services.md+++ b/docs/ai-services.md@@ -9,8 +9,8 @@ telemetry, no account and no servers of its own. When you use an AI feature, Scu| Feature | Service (your choice) | What is sent | Key ||---|---|---|---|| Brush Lab | Free (default): Pollinations.ai's hosted open models, falling back to a running local Ollama. Or Anthropic (Claude), or any OpenAI-compatible endpoint such as a local Ollama / LM Studio | Your brush description and the current brush settings (text only) | None for Free and local models; your API key otherwise |-| Generate 3D (image) | Hugging Face Spaces (free), fal.ai, Meshy, or your own command | The image you chose, cropped and resized to at most 1024 px | Optional HF token; your fal.ai / Meshy key |-| Generate 3D (text) | Hugging Face Spaces (free), fal.ai, Meshy | Your text description | As above |+| Generate 3D (image) | Hugging Face Spaces (free), fal.ai, Meshy, your own ComfyUI, or your own command | The image you chose, cropped and resized to at most 1024 px | Optional HF token; your fal.ai / Meshy key; none for ComfyUI |+| Generate 3D (text) | Hugging Face Spaces (free), fal.ai, Meshy, your own ComfyUI | Your text description | As above || Send to Unreal | Your own Unreal Editor (local network) | The exported FBX | None |Keys are stored in your local settings file (`ai.json`, owner-only permissions) or read from@@ -45,6 +45,15 @@ These summaries are for convenience only. Read the licences linked above beforecommercially, and check where you are. If you're in the EU, UK or South Korea, choose TRELLIS.2,Stable Fast 3D or a paid service instead of Hunyuan3D.+## Your own ComfyUI++With **ComfyUI - your server**, the image or description goes only to the ComfyUI address you+enter: your own computer, or a GPU server at your studio. It runs there with whatever models that+server has, and nothing is sent to anyone else. The picture is saved in ComfyUI's `input` folder and+the result in its `output` folder, as ComfyUI does with any job. The licences of the models in your+workflow apply as usual; the Hunyuan3D terms above apply to the bundled Hunyuan3D workflows too.+See [Generate 3D with your own ComfyUI](comfyui.md).+## Paid servicesfal.ai and Meshy are paid services with their own terms of service and pricing; generations arediff --git a/docs/comfyui.md b/docs/comfyui.mdnew file mode 100644index 00000000..34cdc5a0--- /dev/null+++ b/docs/comfyui.md@@ -0,0 +1,111 @@+# Generate 3D with your own ComfyUI++If you already run [ComfyUI](https://github.com/comfyanonymous/ComfyUI), SculptForge can use it as its+image-to-3D and text-to-3D generator. ComfyUI can run on your own computer or on a GPU server at your+studio. It runs **any workflow you like**: Hunyuan3D, TRELLIS, a custom node pack, or your own graph.+SculptForge uploads the picture (or sends the description), queues the workflow, waits for it to finish,+downloads the mesh it saved and opens it ready to sculpt, remeshed exactly like the other services.++Nothing goes anywhere except your ComfyUI server. There's no account, no key and no cost per model.++## Setup++1. **Start ComfyUI.** By default it listens on `http://127.0.0.1:8188`. If SculptForge runs on a+ different computer, start ComfyUI with `--listen` (for example `python main.py --listen 0.0.0.0`)+ so other machines can reach it. Only do that on a network you trust: ComfyUI has no login.+2. **Install the models your workflow needs.** For the bundled Hunyuan3D 2.1 workflow, that's+ `hunyuan_3d_v2.1.safetensors` in `ComfyUI/models/checkpoints`. The workflows README+ (`integrations/comfyui/workflows/README.md`) lists every file, its size and its download link.+3. In SculptForge, open **Generate 3D** (File > Generate 3D..., or drop a picture onto the window) and+ pick **ComfyUI - your server**.+4. Enter the **Server** address and press **Test connection**. You should see the ComfyUI version and+ your GPU, for example `Connected: ComfyUI 0.42.0 - NVIDIA GeForce RTX 4090 (24.0 GB)`.+5. Pick a **Workflow**: one of the bundled ones, or **Other file (API format)...** for your own.+6. Press **Generate 3D model**. The progress bar shows your place in ComfyUI's queue and how long the+ workflow has been running. **Cancel** removes the job from the queue, or stops it if it's already+ running.++**Save settings** stores the server address and your chosen workflows in `ai.json`, so they're still+set next time you open SculptForge.++### Bundled workflows++SculptForge ships these in `integrations/comfyui/workflows/` (installed under+`share/sculptforge/comfyui/workflows`):++| Workflow | Tab | Needs |+|---|---|---|+| `hunyuan3d-image-to-3d` | From image | The Hunyuan3D 2.1 checkpoint (7.4 GB) and a GPU (Tencent quotes about 10 GB of VRAM) |+| `text-to-3d` | From text | FLUX.1 schnell, BiRefNet and Hunyuan3D 2.1 (about 25 GB of downloads) and a GPU |+| `sculptforge-inflate` | From image | The [ComfyUI-SculptForge](../integrations/comfyui/ComfyUI-SculptForge) node pack. No GPU: a quick inflated blockout, handy for checking your setup |++When you haven't chosen a workflow, SculptForge uses the first bundled one that fits the tab.++### From the command line, Python or an MCP agent++```sh+sculptforge-cli -c 'comfyui_check' # is it up? version and GPU+sculptforge-cli -c 'image_to_3d path=hero.png service=comfyui workflow=my-hunyuan-api.json; export_glb path=hero.glb'+sculptforge-cli -c 'text_to_3d prompt="a carved stone owl" service=comfyui url=http://gpu-box:8188 seed=42'+```++`workflow=` and `url=` override your saved settings for that one run. `seed=` makes a run+repeatable; without it, every run gets a fresh seed.++## Workflows: API format and the sf_* contract++SculptForge runs workflows saved in ComfyUI's **API format**. That's not the file ComfyUI's ordinary+Save writes. To export one:++- **Current ComfyUI:** open the workflow, then use **Workflow > Export (API)**.+- **Older ComfyUI:** in the settings, turn on **Enable Dev mode Options**, then use **Save (API Format)**.++If you choose an ordinary editor save by mistake, SculptForge says so and tells you how to export it.++To tell SculptForge where things go, give nodes these **titles** (right-click a node > Title):++| Title | Node | What SculptForge does |+|---|---|---|+| `sf_image` | Load Image | Sets `image` to the uploaded picture. Every node with this title gets it. **Without one, the first Load Image node is used.** |+| `sf_prompt` | Any node with a `text`, `prompt` or `string` input (CLIP Text Encode, String, Primitive String...) | Sets that input to your description. **Without one, the first CLIP Text Encode that isn't titled "negative" is used.** |+| `sf_seed` | A Primitive Int or similar | Sets its value to the seed. Any input named `seed` or `noise_seed` gets it too. |++Titles are not case-sensitive. Inputs that are wired to another node are left alone, apart from those+on the titled nodes above.++**The result:** SculptForge imports the **first mesh file** (`.glb`, `.gltf`, `.obj`, `.ply`, `.stl`,+`.fbx`, `.usdz`) that the workflow saves. Core **Save GLB**, **Preview 3D** and the SculptForge+**Export** node all work, and so do most 3D node packs. End your workflow with one of them. If the+workflow finishes without saving a mesh, SculptForge lists what it did produce.++The picture is prepared the same way as for the hosted services: the subject is cropped out of a+transparent background, centred on a square and scaled to at most 1024 px. It's uploaded to ComfyUI's+`input` folder as `sculptforge_<random>.png`.++### Make your own workflow++1. Build and test the graph in ComfyUI, starting from a **Load Image** node (or a text node for text to+ 3D) and ending with a node that saves the mesh.+2. Title the Load Image node `sf_image` (or the text node `sf_prompt`).+3. Export it with **Workflow > Export (API)** and choose it in SculptForge.++`tools/comfyui-validate-workflow.py --server http://127.0.0.1:8188 my-workflow.json` checks the workflow+against your server before you run it: missing nodes and models, bad links, and which nodes the contract+will use.++## Troubleshooting++| Message | What to do |+|---|---|+| *Nothing is answering at http://...* | ComfyUI isn't running at that address, or a firewall blocks it. Check the address and port (8188 by default). For another computer, start ComfyUI with `--listen`. |+| *...answered, but not like ComfyUI* | Something else is on that port, or a proxy is in the way. Use the address ComfyUI prints when it starts. |+| *This workflow was saved in ComfyUI's editor format* | Export it again with **Workflow > Export (API)**. |+| *This ComfyUI is missing the nodes ...* | The workflow uses custom nodes this server doesn't have. Install them (ComfyUI Manager > **Install Missing Custom Nodes**) and restart ComfyUI. |+| *...names a model file this server doesn't have* | Download the model into the folder named in the message (for example `models/checkpoints`). |+| *ComfyUI rejected the workflow: node N ...* | An input is out of range or not allowed. Open the workflow in ComfyUI: it shows the same error on that node. |+| *The workflow finished but saved no mesh file* | End the workflow with Save GLB (or another node that saves a mesh). The message lists what it did produce. |+| *... failed: CUDA out of memory* | The GPU ran out of memory. Try a lower resolution or octree size, or a smaller model. |+| *The job disappeared from ComfyUI's queue* | Someone cleared the queue, or ComfyUI restarted. Generate again. |++On a shared studio server, your job waits in the same queue as everyone else's, and the progress bar+shows your place. **Cancel** only removes or stops your own job.diff --git a/docs/generators.md b/docs/generators.mdindex b6d20293..968afca0 100644--- a/docs/generators.md+++ b/docs/generators.md@@ -1,12 +1,13 @@# Image-to-3D generators-**Image to 3D → Full 3D model** offers five sources:+**Image to 3D → Full 3D model** offers six sources:| Service | What it is | Setup ||---|---|---|| **Hugging Face** (default, free) | The official public demos of Hunyuan3D 2.1 / 2 / 2 mini Turbo (Tencent), TRELLIS.2 (Microsoft) and Stable Fast 3D (Stability AI), on Hugging Face's shared ZeroGPU hardware | Nothing. A free token from huggingface.co/settings/tokens (read access), or `HF_TOKEN`, raises the daily GPU allowance; TRELLIS.2 needs one || **fal.ai** | Hosted reconstruction models, billed per generation: TRELLIS.2 (Microsoft), Hunyuan3D 3.1 Pro/Rapid (Tencent), Meshy 6 - or any other fal model id | Key from fal.ai/dashboard/keys, or `FAL_KEY` || **Meshy** | Meshy's own API (`meshy-7.1` by default) | Key from meshy.ai, or `MESHY_API_KEY` |+| **ComfyUI** | Any workflow on your own ComfyUI, on this computer or a studio GPU server: Hunyuan3D, TRELLIS, your own graph. See [comfyui.md](comfyui.md) | The server address and an API-format workflow (bundled ones included) || **My generator command** | Anything you run locally (see below) | A command line || **Offline blockout** | Inflates the outline into a rounded blob. Fine for simple shapes, not characters | Nothing |diff --git a/src/ai/AiCommands.cpp b/src/ai/AiCommands.cppindex 412144bd..603fe218 100644--- a/src/ai/AiCommands.cpp+++ b/src/ai/AiCommands.cpp@@ -2,6 +2,7 @@// SPDX-License-Identifier: GPL-3.0-or-later#include "ai/AiCommands.h"+#include "ai/ComfyUI.h"#include "ai/ImageTo3DService.h"#include "core/EnginePresets.h"#include "core/Voxel.h"@@ -10,9 +11,19 @@#include <algorithm>#include <chrono>#include <filesystem>+#include <optional>namespace sf::ai {+namespace {++std::optional<uint64_t> comfySeed(const Args& a) {+ if (!a.contains("seed")) return std::nullopt;+ return uint64_t(std::max(0.0, argNumber(a, "seed", 0.0)));+}++} // namespace+void registerAiCommands(CommandRegistry& registry, std::function<AiSettings()> settings) {using T = ParamSpec::Type;registry.add({"brush_ai",@@ -39,16 +50,20 @@ void registerAiCommands(CommandRegistry& registry, std::function<AiSettings()> sregistry.add({"image_to_3d","Reconstruct a full 3D model from one picture (character, creature, prop) and make it sculptable. ""Free by default via Hugging Face Spaces (Hunyuan3D, TRELLIS.2, Stable Fast 3D; HF_TOKEN raises the "- "daily quota), or paid via fal.ai (FAL_KEY) / Meshy (MESHY_API_KEY). Replaces the scene. "- "A transparent background works best.",+ "daily quota), paid via fal.ai (FAL_KEY) / Meshy (MESHY_API_KEY), or any workflow on your own "+ "ComfyUI (service=comfyui workflow=FILE). Replaces the scene. A transparent background works best.",{{"path", T::String, "Image file (png, jpg...).", true},- {"service", T::String, "'huggingface' (free, default), 'fal', 'meshy', 'command' (your generator) or 'inflate' (offline blockout).", false},+ {"service", T::String, "'huggingface' (free, default), 'fal', 'meshy', 'comfyui' (your ComfyUI server), 'command' (your generator) or 'inflate' (offline blockout).", false},{"model", T::String, "huggingface: hunyuan3d-2.1, trellis-2, hunyuan3d-2-turbo, hunyuan3d-2, stable-fast-3d; fal: a model id such as fal-ai/trellis-2.", false},+ {"workflow", T::String, "comfyui: API-format workflow file (default: your saved one, else the bundled one).", false},+ {"url", T::String, "comfyui: server address. Default http://127.0.0.1:8188 or your saved one.", false},+ {"seed", T::Number, "comfyui: seed for the workflow's sf_seed / seed inputs (default: random).", false},{"remesh", T::Bool, "Voxel-remesh the result into a clean watertight sculpting surface. Default true.", false},{"resolution", T::Number, "Remesh voxels across the model (64-512). Default 256.", false}},[settings](Document& doc, const Args& a) -> CommandResult {AiSettings s = settings();if (a.contains("service")) s.generatorService = argString(a, "service");+ if (a.contains("url")) s.comfyUrl = argString(a, "url");if (a.contains("model")) {if (s.generatorService == "meshy") s.meshyModel = argString(a, "model");else if (s.generatorService == "fal") s.falModel = argString(a, "model");@@ -58,7 +73,10 @@ void registerAiCommands(CommandRegistry& registry, std::function<AiSettings()> s("sculptforge_generated_" +std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));const auto stemU8 = stem.u8string();- GeneratorResult r = generate3D(s, argString(a, "path"), std::string(stemU8.begin(), stemU8.end()));+ const std::string out(stemU8.begin(), stemU8.end());+ GeneratorResult r = s.generatorService == "comfyui"+ ? comfy::run(s, argString(a, "workflow"), argString(a, "path"), {}, out, nullptr, comfySeed(a))+ : generate3D(s, argString(a, "path"), out);if (!r.ok) return {false, r.error};CommandResult opened = CommandRegistry().execute(doc, "open", {{"path", r.outputPath}});std::error_code ec;@@ -82,15 +100,19 @@ void registerAiCommands(CommandRegistry& registry, std::function<AiSettings()> sregistry.add({"text_to_3d","Create a 3D model from a description and make it sculptable. Free by default via Hugging Face ""(Hunyuan3D 2 / 2 mini Turbo; HF_TOKEN raises the daily quota), or paid via fal.ai (FAL_KEY) / "- "Meshy (MESHY_API_KEY). Replaces the scene.",+ "Meshy (MESHY_API_KEY), or a workflow on your own ComfyUI (service=comfyui workflow=FILE). Replaces the scene.",{{"prompt", T::String, "One object, e.g. 'a stylised dragon head with curled horns'.", true},- {"service", T::String, "'huggingface' (free, default), 'fal' or 'meshy'.", false},+ {"service", T::String, "'huggingface' (free, default), 'fal', 'meshy' or 'comfyui' (your ComfyUI server).", false},{"model", T::String, "huggingface: hunyuan3d-2-turbo or hunyuan3d-2.", false},+ {"workflow", T::String, "comfyui: API-format workflow file (default: your saved one, else the bundled one).", false},+ {"url", T::String, "comfyui: server address. Default http://127.0.0.1:8188 or your saved one.", false},+ {"seed", T::Number, "comfyui: seed for the workflow's sf_seed / seed inputs (default: random).", false},{"remesh", T::Bool, "Voxel-remesh the result into a clean watertight sculpting surface. Default true.", false},{"resolution", T::Number, "Remesh voxels across the model (64-512). Default 256.", false}},[settings](Document& doc, const Args& a) -> CommandResult {AiSettings s = settings();if (a.contains("service")) s.generatorService = argString(a, "service");+ if (a.contains("url")) s.comfyUrl = argString(a, "url");if (s.generatorService == "command" || s.generatorService == "inflate") s.generatorService = "huggingface";if (a.contains("model")) {if (s.generatorService == "meshy") s.meshyModel = argString(a, "model");@@ -100,7 +122,14 @@ void registerAiCommands(CommandRegistry& registry, std::function<AiSettings()> s("sculptforge_text_" +std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));const auto stemU8 = stem.u8string();- GeneratorResult r = generate3DFromText(s, argString(a, "prompt"), std::string(stemU8.begin(), stemU8.end()));+ const std::string out(stemU8.begin(), stemU8.end());+ GeneratorResult r;+ if (s.generatorService == "comfyui") {+ if (argString(a, "prompt").find_first_not_of(" \t\r\n") == std::string::npos) return {false, "Describe the model you want"};+ r = comfy::run(s, argString(a, "workflow"), {}, argString(a, "prompt"), out, nullptr, comfySeed(a));+ } else {+ r = generate3DFromText(s, argString(a, "prompt"), out);+ }if (!r.ok) return {false, r.error};CommandResult opened = CommandRegistry().execute(doc, "open", {{"path", r.outputPath}});std::error_code ec;@@ -117,6 +146,17 @@ void registerAiCommands(CommandRegistry& registry, std::function<AiSettings()> s" triangles) - ready to sculpt"};}});+ registry.add({"comfyui_check",+ "Check that a ComfyUI server answers (for service=comfyui) and show its version and GPU.",+ {{"url", T::String, "Server address. Default http://127.0.0.1:8188 or your saved one.", false}},+ [settings](Document&, const Args& a) -> CommandResult {+ comfy::ServerInfo info;+ std::string err;+ const std::string url = comfy::normalizeUrl(argString(a, "url", settings().comfyUrl));+ if (!comfy::checkServer(url, info, err)) return {false, err};+ return {true, info.summary() + " at " + url};+ }});+registry.add({"unreal_editors", "List running Unreal Editors that SculptForge can send assets to (remote execution).",{{"network", T::Bool, "Also look on other computers (Unreal needs Multicast TTL 1 and bind address 0.0.0.0).", false}},[](Document&, const Args& a) -> CommandResult {diff --git a/src/ai/BrushAssistant.cpp b/src/ai/BrushAssistant.cppindex e4c4fd17..da2b5c0d 100644--- a/src/ai/BrushAssistant.cpp+++ b/src/ai/BrushAssistant.cpp@@ -138,6 +138,9 @@ Json AiSettings::toJson() const {j.set("meshyKey", meshyKey);j.set("meshyModel", meshyModel);j.set("meshyEndpoint", meshyEndpoint);+ j.set("comfyUrl", comfyUrl);+ j.set("comfyImageWorkflow", comfyImageWorkflow);+ j.set("comfyTextWorkflow", comfyTextWorkflow);return j;}@@ -167,6 +170,9 @@ AiSettings AiSettings::fromJson(const Json& j) {text("meshyKey", s.meshyKey);text("meshyModel", s.meshyModel);text("meshyEndpoint", s.meshyEndpoint);+ text("comfyUrl", s.comfyUrl);+ s.comfyImageWorkflow = j["comfyImageWorkflow"].asString();+ s.comfyTextWorkflow = j["comfyTextWorkflow"].asString();return s;}diff --git a/src/ai/BrushAssistant.h b/src/ai/BrushAssistant.hindex 1122e47a..55908c42 100644--- a/src/ai/BrushAssistant.h+++ b/src/ai/BrushAssistant.h@@ -25,7 +25,8 @@ struct AiSettings {std::string apiKey; // Free: unused; empty = use ANTHROPIC_API_KEY / OPENAI_API_KEY from the environmentstd::string effort = "low";// Image to 3D. service: "huggingface" (free Spaces), "fal", "meshy",- // "command" (generatorCommand with {input}/{output}) or "inflate" (offline).+ // "comfyui" (your ComfyUI server), "command" (generatorCommand with+ // {input}/{output}) or "inflate" (offline).std::string generatorService = "huggingface";std::string hfToken; // empty = HF_TOKEN from the environment (optional; raises the free quota)std::string hfModel = "hunyuan3d-2.1";@@ -36,6 +37,9 @@ struct AiSettings {std::string meshyKey; // empty = MESHY_API_KEY from the environmentstd::string meshyModel = "latest";std::string meshyEndpoint = "https://api.meshy.ai";+ std::string comfyUrl = "http://127.0.0.1:8188";+ std::string comfyImageWorkflow; // API-format workflow files; empty = the bundled default+ std::string comfyTextWorkflow;std::string resolvedFalKey() const;std::string resolvedHfToken() const;std::string resolvedMeshyKey() const;diff --git a/src/ai/ComfyUI.cpp b/src/ai/ComfyUI.cppnew file mode 100644index 00000000..b68aa5d0--- /dev/null+++ b/src/ai/ComfyUI.cpp@@ -0,0 +1,784 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+#include "ai/ComfyUI.h"++#include "ai/Http.h"+#include "ai/ImageTo3DService.h"++#include <algorithm>+#include <cctype>+#include <chrono>+#include <filesystem>+#include <fstream>+#include <functional>+#include <random>+#include <set>+#include <sstream>+#include <thread>++#if defined(_WIN32)+#define WIN32_LEAN_AND_MEAN+#include <windows.h>+#elif defined(__APPLE__)+#include <mach-o/dyld.h>+#endif++namespace sf::ai::comfy {++namespace {++constexpr int kMaxMinutes = 60; // a studio queue can be long+constexpr int kGiveUpSeconds = 30; // unreachable, or the prompt vanished from queue and history++std::string lower(std::string s) {+ std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return char(std::tolower(c)); });+ return s;+}++std::string trim(const std::string& s) {+ const size_t a = s.find_first_not_of(" \t\r\n");+ if (a == std::string::npos) return {};+ return s.substr(a, s.find_last_not_of(" \t\r\n") - a + 1);+}++std::filesystem::path u8path(const std::string& s) { return std::filesystem::path(std::u8string(s.begin(), s.end())); }++std::string pathString(const std::filesystem::path& p) {+ const auto s = p.u8string();+ return std::string(s.begin(), s.end());+}++std::string titleOf(const Json& node) { return lower(trim(node["_meta"]["title"].asString())); }++std::string nodeLabel(const Json& workflow, const std::string& id) {+ const Json& node = workflow[id];+ std::string label = "node " + id;+ const std::string title = trim(node["_meta"]["title"].asString());+ const std::string type = node["class_type"].asString();+ // Titles that only restate the node's display name ("Save Image" for SaveImage) are noise.+ std::string squashed;+ for (char c : title)+ if (c != ' ') squashed += c;+ if (!title.empty() && lower(squashed) != lower(type)) label += " '" + title + "'";+ if (!type.empty()) label += " (" + type + ")";+ return label;+}++// A literal input value, not a link ([node_id, output_index]).+bool isLink(const Json& v) { return v.isArray() && v.size() == 2 && v[0].isString(); }++void setInput(Json& workflow, const std::string& id, const std::string& key, Json value) {+ Json node = workflow[id];+ Json inputs = node["inputs"].isObject() ? node["inputs"] : Json::object();+ inputs.set(key, std::move(value));+ node.set("inputs", inputs);+ workflow.set(id, node);+}++std::string urlEncode(const std::string& s) {+ std::string out;+ for (unsigned char c : s) {+ if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {+ out += char(c);+ } else {+ char buf[4];+ std::snprintf(buf, sizeof buf, "%%%02X", c);+ out += buf;+ }+ }+ return out;+}++const char* const kMeshExtensions[] = {".glb", ".gltf", ".obj", ".ply", ".stl", ".fbx", ".usdz", ".usda", ".usd"};++bool hasMeshExtension(const std::string& name) {+ const std::string l = lower(name);+ for (const char* e : kMeshExtensions)+ if (l.ends_with(e)) return true;+ return false;+}++// A file named by a plain string: "mesh/out_00001_.glb [output]", or an absolute path inside ComfyUI's folders.+std::optional<OutputFile> fileFromString(std::string s) {+ std::string type = "output";+ if (const size_t b = s.rfind(" ["); b != std::string::npos && s.ends_with("]")) {+ type = s.substr(b + 2, s.size() - b - 3);+ s = s.substr(0, b);+ }+ if (!hasMeshExtension(s)) return std::nullopt;+ std::replace(s.begin(), s.end(), '\\', '/');+ const bool absolute = s.starts_with("/") || (s.size() > 2 && s[1] == ':');+ if (absolute) {+ bool inFolder = false;+ for (const char* folder : {"output", "temp", "input"}) {+ const std::string marker = std::string("/") + folder + "/";+ if (const size_t at = s.rfind(marker); at != std::string::npos) {+ type = folder;+ s = s.substr(at + marker.size());+ inFolder = true;+ break;+ }+ }+ if (!inFolder) s = s.substr(s.rfind('/') + 1);+ }+ OutputFile f;+ f.type = type;+ const size_t slash = s.rfind('/');+ f.filename = slash == std::string::npos ? s : s.substr(slash + 1);+ f.subfolder = slash == std::string::npos ? std::string() : s.substr(0, slash);+ return f;+}++void collectFiles(const Json& v, const std::string& nodeId, const std::string& key, std::vector<OutputFile>& out) {+ if (v.isObject() && v["filename"].isString()) {+ OutputFile f;+ f.nodeId = nodeId;+ f.key = key;+ f.filename = v["filename"].asString();+ f.subfolder = v["subfolder"].asString();+ if (v["type"].isString() && !v["type"].asString().empty()) f.type = v["type"].asString();+ out.push_back(f);+ } else if (v.isString()) {+ if (auto f = fileFromString(v.asString())) {+ f->nodeId = nodeId;+ f->key = key;+ out.push_back(*f);+ }+ } else if (v.isArray()) {+ for (const auto& item : v.items()) collectFiles(item, nodeId, key, out);+ } else if (v.isObject()) {+ for (const auto& [k, item] : v.members()) collectFiles(item, nodeId, key, out);+ }+}++void status(GenerationProgress* p, const std::string& text, int percent = -1) {+ if (!p) return;+ p->setStatus(text);+ p->percent = percent;+}++bool wait(GenerationProgress* p, int milliseconds) {+ for (int i = 0; i < milliseconds / 50; ++i) {+ if (p && p->cancel) return false;+ std::this_thread::sleep_for(std::chrono::milliseconds(50));+ }+ return !(p && p->cancel);+}++std::string unreachable(const std::string& url, const std::string& transportError) {+ return "Nothing is answering at " + url + " (" + transportError ++ "). Start ComfyUI there, or check the address and port; for another computer start ComfyUI with --listen";+}++std::string randomId() {+ std::random_device rd;+ std::mt19937_64 rng((uint64_t(rd()) << 32) ^ rd() ^ uint64_t(std::chrono::steady_clock::now().time_since_epoch().count()));+ std::string id;+ for (int i = 0; i < 16; ++i) id += "0123456789abcdef"[rng() % 16];+ return id;+}++uint64_t randomSeed() {+ std::random_device rd;+ std::mt19937_64 rng((uint64_t(rd()) << 32) ^ rd());+ return rng() % 2147483647u; // within every seed widget's range+}++std::string executableDir() {+#if defined(_WIN32)+ wchar_t buf[MAX_PATH * 4];+ const DWORD n = GetModuleFileNameW(nullptr, buf, DWORD(std::size(buf)));+ if (n == 0 || n >= std::size(buf)) return {};+ return pathString(std::filesystem::path(std::wstring(buf, n)).parent_path());+#elif defined(__APPLE__)+ char buf[4096];+ uint32_t size = sizeof buf;+ if (_NSGetExecutablePath(buf, &size) != 0) return {};+ std::error_code ec;+ auto p = std::filesystem::weakly_canonical(buf, ec);+ return pathString((ec ? std::filesystem::path(buf) : p).parent_path());+#else+ std::error_code ec;+ auto p = std::filesystem::read_symlink("/proc/self/exe", ec);+ return ec ? std::string() : pathString(p.parent_path());+#endif+}++} // namespace++std::string normalizeUrl(const std::string& url) {+ std::string u = trim(url);+ if (u.empty()) u = kDefaultUrl;+ if (u.find("://") == std::string::npos) u = "http://" + u;+ while (u.size() > 8 && u.back() == '/') u.pop_back();+ return u;+}++// --- Server -----------------------------------------------------------------++std::string ServerInfo::summary() const {+ std::string s = comfyVersion.empty() ? "ComfyUI" : "ComfyUI " + comfyVersion;+ for (size_t i = 0; i < devices.size(); ++i) s += (i ? ", " : " - ") + devices[i];+ return s;+}++bool parseSystemStats(const Json& stats, ServerInfo& info) {+ const Json& system = stats["system"];+ if (!system.isObject()) return false;+ info = {};+ info.comfyVersion = system["comfyui_version"].asString();+ info.pythonVersion = trim(system["python_version"].asString().substr(0, system["python_version"].asString().find(' ')));+ info.pytorchVersion = system["pytorch_version"].asString();+ for (const auto& d : stats["devices"].items()) {+ // "cuda:0 NVIDIA GeForce RTX 4090 : cudaMallocAsync" -> "NVIDIA GeForce RTX 4090"+ std::string name = d["name"].asString();+ if (const size_t colon = name.find(" : "); colon != std::string::npos) name = name.substr(0, colon);+ const std::string type = d["type"].asString();+ if (!type.empty() && type != "cpu" && name.starts_with(type + ":")) name = name.substr(name.find(' ') + 1);+ name = trim(name);+ if (name.empty()) name = type;+ const double vram = d["vram_total"].asNumber(0);+ if (type != "cpu" && vram > 0) {+ char buf[32];+ std::snprintf(buf, sizeof buf, " (%.1f GB)", vram / (1024.0 * 1024.0 * 1024.0));+ name += buf;+ }+ if (!name.empty()) info.devices.push_back(name);+ }+ return true;+}++bool checkServer(const std::string& url, ServerInfo& info, std::string& error, int timeoutSeconds) {+ const std::string base = normalizeUrl(url);+ HttpResponse r = httpRequest("GET", base + "/system_stats", {}, {}, timeoutSeconds);+ if (!r.error.empty()) {+ error = unreachable(base, r.error);+ return false;+ }+ if (r.status == 401 || r.status == 403) {+ error = base + " refused the request (HTTP " + std::to_string(r.status) ++ ") - the server needs a login SculptForge cannot provide; ask for direct access to ComfyUI's port";+ return false;+ }+ Json stats;+ std::string err;+ if (r.status != 200 || !Json::parse(r.body, stats, err) || !parseSystemStats(stats, info)) {+ error = base + " answered, but not like ComfyUI (HTTP " + std::to_string(r.status) ++ ") - check the address and port (ComfyUI's default is 8188)";+ return false;+ }+ return true;+}++// --- Workflows ---------------------------------------------------------------++bool isUiFormat(const Json& workflow) { return workflow.isObject() && workflow["nodes"].isArray(); }++bool parseWorkflow(const std::string& text, Json& workflow, std::string& error) {+ Json j;+ std::string err;+ if (!Json::parse(text, j, err)) {+ error = "The workflow is not valid JSON (" + err + ")";+ return false;+ }+ if (isUiFormat(j)) {+ error = "This workflow was saved in ComfyUI's editor format, which can't be queued. Open it in ComfyUI and use "+ "Workflow > Export (API) (older versions: enable 'Dev mode' in the settings, then 'Save (API Format)'), "+ "then pick that file";+ return false;+ }+ if (j["prompt"].isObject() && !j["prompt"]["class_type"].isString()) j = Json(j["prompt"]); // {"prompt": {...}} as sent to /prompt+ if (!j.isObject() || j.members().empty()) {+ error = "Not a ComfyUI API workflow (expected {node_id: {class_type, inputs}})";+ return false;+ }+ for (const auto& [id, node] : j.members()) {+ if (!node.isObject() || !node["class_type"].isString()) {+ error = "Not a ComfyUI API workflow: '" + id + "' has no class_type (export it with Workflow > Export (API))";+ return false;+ }+ }+ workflow = std::move(j);+ return true;+}++bool loadWorkflow(const std::string& path, Json& workflow, std::string& error) {+ std::ifstream f(u8path(path), std::ios::binary);+ if (!f) {+ error = "Cannot open the workflow " + path;+ return false;+ }+ std::stringstream ss;+ ss << f.rdbuf();+ if (!parseWorkflow(ss.str(), workflow, error)) {+ error = pathString(u8path(path).filename()) + ": " + error;+ return false;+ }+ return true;+}++bool applyContract(Json& workflow, const ContractInputs& in, std::string& error, ContractReport* report) {+ ContractReport local;+ ContractReport& rep = report ? *report : local;+ rep = {};+ std::vector<std::string> ids;+ for (const auto& [id, node] : workflow.members()) ids.push_back(id);++ if (!in.image.empty()) {+ for (const auto& id : ids)+ if (titleOf(workflow[id]) == "sf_image") rep.imageNodes.push_back(id);+ if (rep.imageNodes.empty()) {+ // Fallback: the first Load Image node (the stock one first, then look-alikes from node packs).+ for (const auto& id : ids)+ if (workflow[id]["class_type"].asString() == "LoadImage") {+ rep.imageNodes.push_back(id);+ break;+ }+ if (rep.imageNodes.empty())+ for (const auto& id : ids)+ if (workflow[id]["class_type"].asString().find("LoadImage") != std::string::npos &&+ workflow[id]["inputs"].has("image")) {+ rep.imageNodes.push_back(id);+ break;+ }+ }+ if (rep.imageNodes.empty()) {+ error = "The workflow has no Load Image node for the picture - add one (title it sf_image), or pick an "+ "image-to-3D workflow";+ return false;+ }+ for (const auto& id : rep.imageNodes) setInput(workflow, id, "image", in.image);+ }++ if (!in.prompt.empty()) {+ auto promptKey = [&](const std::string& id) -> std::string {+ const Json& inputs = workflow[id]["inputs"];+ for (const char* key : {"text", "prompt", "string", "value"})+ if (inputs.has(key) && (std::string(key) != "value" || inputs[key].isString())) return key;+ return {};+ };+ std::vector<std::pair<std::string, std::string>> targets; // node id, input name+ for (const auto& id : ids) {+ if (titleOf(workflow[id]) != "sf_prompt") continue;+ const std::string key = promptKey(id);+ if (key.empty()) {+ error = "The " + nodeLabel(workflow, id) + " is titled sf_prompt but has no text, prompt or string input";+ return false;+ }+ targets.emplace_back(id, key);+ }+ if (targets.empty()) {+ // Fallback: the first text encoder that isn't the negative prompt.+ for (const auto& id : ids) {+ const Json& node = workflow[id];+ if (node["class_type"].asString().find("TextEncode") == std::string::npos) continue;+ if (titleOf(node).find("negative") != std::string::npos) continue;+ const std::string key = promptKey(id);+ if (key.empty() || isLink(node["inputs"][key])) continue;+ targets.emplace_back(id, key);+ break;+ }+ }+ if (targets.empty()) {+ error = "The workflow has no node for the description - title the node that takes the text sf_prompt "+ "(in ComfyUI: right-click it > Title)";+ return false;+ }+ for (const auto& [id, key] : targets) {+ setInput(workflow, id, key, in.prompt);+ rep.promptNodes.push_back(id);+ }+ }++ if (in.seed) {+ const double seed = double(*in.seed);+ for (const auto& id : ids) {+ const Json& inputs = workflow[id]["inputs"];+ bool set = false;+ for (const char* key : {"seed", "noise_seed"})+ if (inputs[key].isNumber()) {+ setInput(workflow, id, key, seed);+ set = true;+ }+ if (!set && titleOf(workflow[id]) == "sf_seed")+ for (const char* key : {"value", "int", "number"})+ if (workflow[id]["inputs"][key].isNumber()) {+ setInput(workflow, id, key, seed);+ set = true;+ break;+ }+ if (set) rep.seedNodes.push_back(id);+ }+ }+ return true;+}++std::string workflowKind(const Json& workflow) {+ bool text = false;+ for (const auto& [id, node] : workflow.members()) {+ const std::string title = titleOf(node), type = node["class_type"].asString();+ if (title == "sf_image" || type == "LoadImage") return "image";+ if (title == "sf_prompt" || type.find("TextEncode") != std::string::npos) text = true;+ }+ return text ? "text" : "";+}++// --- Results -----------------------------------------------------------------++bool OutputFile::isMesh() const { return hasMeshExtension(filename); }++HistoryEntry parseHistory(const Json& history, const std::string& promptId) {+ HistoryEntry h;+ const Json& e = history[promptId];+ if (!e.isObject()) return h;+ h.found = true;+ const Json& st = e["status"];+ for (const auto& [nodeId, outputs] : e["outputs"].members())+ for (const auto& [key, value] : outputs.members()) collectFiles(value, nodeId, key, h.files);+ for (const auto& m : st["messages"].items()) {+ const std::string kind = m[0].asString();+ const Json& d = m[1];+ if (kind == "execution_error") {+ std::string where = d["node_type"].asString();+ if (d["node_id"].isString()) where += (where.empty() ? "node " : " (node ") + d["node_id"].asString() + (where.empty() ? "" : ")");+ h.error = (where.empty() ? std::string() : where + " failed: ") + trim(d["exception_message"].asString());+ if (d["exception_type"].isString() && d["exception_type"].asString().find("OutOfMemory") != std::string::npos)+ h.error += " - the GPU ran out of memory; try a smaller model or resolution";+ } else if (kind == "execution_interrupted" && h.error.empty()) {+ h.error = "The workflow was interrupted on the ComfyUI server";+ }+ }+ const std::string s = st["status_str"].asString();+ h.failed = s == "error" || !h.error.empty();+ if (h.failed && h.error.empty()) h.error = "The workflow failed on the ComfyUI server (see its console)";+ // Old servers have no status block: being in the history means it finished.+ h.completed = !h.failed && (st["completed"].asBool() || !st.isObject() || s == "success");+ return h;+}++const OutputFile* firstMesh(const std::vector<OutputFile>& files) {+ for (const auto& f : files)+ if (f.isMesh()) return &f;+ return nullptr;+}++std::string describeOutputs(const Json& history, const std::string& promptId) {+ const Json& e = history[promptId];+ // e.prompt = [number, id, workflow, extra, outputs to execute]: names the nodes.+ const Json& workflow = e["prompt"][2];+ std::string out;+ for (const auto& [nodeId, outputs] : e["outputs"].members()) {+ for (const auto& [key, value] : outputs.members()) {+ std::vector<std::string> names;+ std::function<void(const Json&)> walk = [&](const Json& v) {+ if (v.isObject() && v["filename"].isString()) names.push_back(v["filename"].asString());+ else if (v.isString() && v.asString().size() < 120) names.push_back(v.asString());+ else if (v.isArray())+ for (const auto& item : v.items()) walk(item);+ };+ walk(value);+ std::string item = (workflow.isObject() ? nodeLabel(workflow, nodeId) : "node " + nodeId) + " '" + key + "'";+ for (size_t i = 0; i < names.size() && i < 3; ++i) item += (i ? ", " : ": ") + names[i];+ if (names.size() > 3) item += " (+" + std::to_string(names.size() - 3) + " more)";+ out += (out.empty() ? "" : "; ") + item;+ }+ }+ return out.empty() ? "none" : out;+}++std::string viewUrl(const std::string& baseUrl, const OutputFile& file) {+ return normalizeUrl(baseUrl) + "/view?filename=" + urlEncode(file.filename) + "&subfolder=" + urlEncode(file.subfolder) ++ "&type=" + urlEncode(file.type);+}++std::vector<std::string> missingNodeTypes(const Json& workflow, const Json& objectInfo) {+ std::vector<std::string> missing;+ for (const auto& [id, node] : workflow.members()) {+ const std::string type = node["class_type"].asString();+ if (!objectInfo.has(type) && std::find(missing.begin(), missing.end(), type) == missing.end()) missing.push_back(type);+ }+ return missing;+}++std::string promptError(const std::string& body, const Json& workflow) {+ Json j;+ std::string err;+ if (!Json::parse(body, j, err) || !j.isObject()) return "ComfyUI rejected the workflow: " + trim(body.substr(0, 300));+ const Json& e = j["error"];+ const std::string type = e["type"].asString();+ if (type == "missing_node_type") {+ const Json& x = e["extra_info"];+ std::string node = x["class_type"].asString();+ if (x["node_id"].isString() && workflow.has(x["node_id"].asString())) node = nodeLabel(workflow, x["node_id"].asString());+ return "This ComfyUI does not have the node " + (node.empty() ? e["message"].asString() : node) ++ ". Install the custom node pack that provides it (ComfyUI Manager > Install Missing Custom Nodes) and "+ "restart ComfyUI";+ }+ if (type == "prompt_no_outputs")+ return "The workflow has no output node - end it with a node that saves the mesh (Save GLB, or SculptForge's "+ "Save Mesh)";+ std::vector<std::string> problems;+ bool missingModel = false;+ for (const auto& [id, info] : j["node_errors"].members()) {+ for (const auto& ne : info["errors"].items()) {+ std::string msg = trim(ne["message"].asString());+ std::string details = trim(ne["details"].asString());+ // "ckpt_name: 'hunyuan3d-dit-v2.safetensors' not in [...]": a model the server doesn't have.+ const std::string input = lower(details.substr(0, details.find(':')));+ const std::string d = lower(details);+ if (ne["type"].asString() == "value_not_in_list" &&+ (input.ends_with("_name") || input.find("model") != std::string::npos ||+ d.find(".safetensors") != std::string::npos || d.find(".ckpt") != std::string::npos ||+ d.find(".gguf") != std::string::npos || d.find(".pth") != std::string::npos))+ missingModel = true;+ if (details.size() > 160) details = details.substr(0, 157) + "...";+ if (!details.empty()) msg += ": " + details;+ std::string label = workflow.has(id) ? nodeLabel(workflow, id) : "node " + id;+ problems.push_back(label + " - " + msg);+ }+ }+ if (!problems.empty()) {+ std::string out = "ComfyUI rejected the workflow: ";+ for (size_t i = 0; i < problems.size() && i < 4; ++i) out += (i ? "; " : "") + problems[i];+ if (problems.size() > 4) out += " (+" + std::to_string(problems.size() - 4) + " more)";+ if (missingModel) out += ". The workflow names a model file this server doesn't have - download it into ComfyUI's models folder";+ return out;+ }+ std::string msg = trim(e["message"].asString());+ if (msg.empty() && e.isString()) msg = e.asString();+ if (e["details"].isString() && !e["details"].asString().empty()) msg += " (" + trim(e["details"].asString()) + ")";+ return "ComfyUI rejected the workflow: " + (msg.empty() ? trim(body.substr(0, 300)) : msg);+}++int queuePosition(const Json& queue, const std::string& promptId) {+ for (const auto& item : queue["queue_running"].items())+ if (item[1].asString() == promptId) return 0;+ double ours = -1;+ for (const auto& item : queue["queue_pending"].items())+ if (item[1].asString() == promptId) ours = item[0].asNumber();+ if (ours < 0) return -1;+ int position = 1;+ for (const auto& item : queue["queue_pending"].items())+ if (item[0].asNumber() < ours) ++position;+ return position;+}++// --- Running -----------------------------------------------------------------++GeneratorResult run(const AiSettings& s, const std::string& workflowPath, const std::string& imagePath,+ const std::string& prompt, const std::string& outputStem, GenerationProgress* p,+ std::optional<uint64_t> seed) {+ GeneratorResult result;+ const bool text = imagePath.empty();+ const std::string kind = text ? "text" : "image";+ const std::string base = normalizeUrl(s.comfyUrl);++ std::string path = workflowPath;+ if (path.empty()) path = text ? s.comfyTextWorkflow : s.comfyImageWorkflow;+ if (path.empty()) path = defaultWorkflow(kind);+ if (path.empty()) {+ result.error = std::string("Choose a ComfyUI workflow for ") + (text ? "text" : "image") ++ " to 3D (exported with Workflow > Export (API)) - see docs/comfyui.md";+ return result;+ }+ Json workflow;+ if (!loadWorkflow(path, workflow, result.error)) return result;++ status(p, "Connecting to ComfyUI at " + base + "...");+ ServerInfo info;+ if (!checkServer(base, info, result.error, 10)) return result;++ ContractInputs in;+ in.prompt = prompt;+ in.seed = seed ? seed : std::optional<uint64_t>(randomSeed());+ if (!text) {+ std::string png;+ if (!prepareImagePng(imagePath, png, result.error)) return result;+ status(p, "Uploading the image...");+ std::string contentType;+ const std::string body = multipartBody({{"image", png, "sculptforge_" + randomId() + ".png", "image/png"},+ {"type", "input", {}, {}},+ {"overwrite", "true", {}, {}}},+ contentType);+ HttpResponse r = httpRequest("POST", base + "/upload/image", {contentType}, body, 120);+ Json up;+ std::string err;+ if (!r.error.empty()) {+ result.error = "Image upload to ComfyUI failed: " + r.error;+ return result;+ }+ if (r.status != 200 || !Json::parse(r.body, up, err) || !up["name"].isString()) {+ result.error = "Image upload to ComfyUI failed (HTTP " + std::to_string(r.status) + "): " + trim(r.body.substr(0, 200));+ return result;+ }+ in.image = up["subfolder"].asString().empty() ? up["name"].asString()+ : up["subfolder"].asString() + "/" + up["name"].asString();+ }+ if (p && p->cancel) {+ result.error = "Cancelled";+ return result;+ }+ if (!applyContract(workflow, in, result.error)) return result;++ Json request = Json::object();+ request.set("prompt", workflow);+ request.set("client_id", "sculptforge-" + randomId());+ status(p, "Queueing the workflow...");+ HttpResponse r = httpPostJson(base + "/prompt", {"Content-Type: application/json"}, request.dump(), 60);+ if (!r.error.empty()) {+ result.error = "Lost the connection to ComfyUI: " + r.error;+ return result;+ }+ Json queued;+ std::string err;+ if (r.status != 200 || !Json::parse(r.body, queued, err) || !queued["prompt_id"].isString()) {+ result.error = promptError(r.body, workflow);+ // List every missing node type at once, not only the first one ComfyUI complained about.+ if (r.body.find("missing_node_type") != std::string::npos) {+ HttpResponse oi = httpRequest("GET", base + "/object_info", {}, {}, 60);+ Json objectInfo;+ if (oi.status == 200 && Json::parse(oi.body, objectInfo, err)) {+ const auto missing = missingNodeTypes(workflow, objectInfo);+ if (missing.size() > 1) {+ std::string list;+ for (const auto& m : missing) list += (list.empty() ? "" : ", ") + m;+ result.error = "This ComfyUI is missing the nodes " + list ++ ". Install the custom node packs that provide them (ComfyUI Manager > Install "+ "Missing Custom Nodes) and restart ComfyUI";+ }+ }+ }+ return result;+ }+ const std::string id = queued["prompt_id"].asString();++ const auto start = std::chrono::steady_clock::now();+ auto lastSeen = start;+ int lastPosition = -1;+ HistoryEntry done;+ Json history;+ while (true) {+ if (!wait(p, 1000)) {+ // Free the server: drop it from the queue, or stop it if it is the one running.+ Json del = Json::object();+ Json ids = Json::array();+ ids.push(id);+ del.set("delete", ids);+ httpPostJson(base + "/queue", {"Content-Type: application/json"}, del.dump(), 10);+ HttpResponse q = httpRequest("GET", base + "/queue", {}, {}, 10);+ Json queue;+ if (lastPosition == 0 || (Json::parse(q.body, queue, err) && queuePosition(queue, id) == 0)) {+ Json body = Json::object();+ body.set("prompt_id", id);+ httpPostJson(base + "/interrupt", {"Content-Type: application/json"}, body.dump(), 10);+ }+ result.error = "Cancelled";+ return result;+ }+ const auto now = std::chrono::steady_clock::now();+ const int secs = int(std::chrono::duration_cast<std::chrono::seconds>(now - start).count());+ if (now - start > std::chrono::minutes(kMaxMinutes)) {+ result.error = "Timed out after " + std::to_string(kMaxMinutes) + " minutes waiting for ComfyUI";+ return result;+ }+ HttpResponse h = httpRequest("GET", base + "/history/" + id, {}, {}, 30);+ if (h.status == 200 && Json::parse(h.body, history, err)) {+ done = parseHistory(history, id);+ if (done.found && (done.completed || done.failed)) break;+ }+ HttpResponse q = httpRequest("GET", base + "/queue", {}, {}, 30);+ Json queue;+ if (q.error.empty() && q.status == 200 && Json::parse(q.body, queue, err)) {+ lastPosition = queuePosition(queue, id);+ if (lastPosition >= 0 || done.found) lastSeen = now;+ }+ if (now - lastSeen > std::chrono::seconds(kGiveUpSeconds)) {+ result.error = !q.error.empty() ? "Lost the connection to ComfyUI at " + base + ": " + q.error+ : "The job disappeared from ComfyUI's queue (cancelled there, or the server restarted)";+ return result;+ }+ if (lastPosition == 0) status(p, "Running the workflow on ComfyUI... " + std::to_string(secs) + "s");+ else if (lastPosition > 0)+ status(p, "Waiting in ComfyUI's queue (position " + std::to_string(lastPosition) + ")... " + std::to_string(secs) + "s");+ }+ if (done.failed) {+ result.error = "ComfyUI: " + done.error;+ return result;+ }+ const OutputFile* mesh = firstMesh(done.files);+ if (!mesh) {+ result.error = "The workflow finished but saved no mesh file (.glb, .obj, .ply, .stl, .fbx...). It produced: " ++ describeOutputs(history, id) + ". End it with a node that saves the mesh, such as Save GLB";+ return result;+ }++ status(p, "Downloading " + mesh->filename + "...");+ HttpResponse d = httpRequest("GET", viewUrl(base, *mesh), {}, {}, 300);+ if (!d.error.empty() || d.status != 200 || d.body.empty()) {+ result.error = "Could not download " + mesh->filename + " from ComfyUI: " ++ (d.error.empty() ? "HTTP " + std::to_string(d.status) : d.error);+ return result;+ }+ std::string ext = lower(u8path(mesh->filename).extension().string());+ const std::string out = outputStem + ext;+ std::ofstream f(u8path(out), std::ios::binary);+ if (!f || !f.write(d.body.data(), std::streamsize(d.body.size()))) {+ result.error = "Cannot write " + out;+ return result;+ }+ result.ok = true;+ result.outputPath = out;+ return result;+}++std::vector<BundledWorkflow> bundledWorkflows() {+ std::vector<std::filesystem::path> roots;+ if (const std::string exe = executableDir(); !exe.empty()) {+ const auto dir = u8path(exe);+ roots.push_back(dir / ".." / "share" / "sculptforge" / "comfyui");+ roots.push_back(dir / ".." / "Resources" / "comfyui"); // macOS app bundle+ roots.push_back(dir / "comfyui"); // Windows zip+ roots.push_back(dir / ".." / "integrations" / "comfyui"); // build/ inside a checkout+ }+#ifdef SF_SOURCE_DIR+ roots.push_back(std::filesystem::path(SF_SOURCE_DIR) / "integrations" / "comfyui");+#endif+ std::vector<BundledWorkflow> out;+ std::set<std::string> names;+ for (const auto& root : roots) {+ for (const auto& dir : {root / "workflows", root}) {+ std::error_code ec;+ if (!std::filesystem::is_directory(dir, ec)) continue;+ std::vector<std::filesystem::path> files;+ for (const auto& entry : std::filesystem::directory_iterator(dir, ec))+ if (entry.is_regular_file(ec) && lower(entry.path().extension().string()) == ".json") files.push_back(entry.path());+ std::sort(files.begin(), files.end());+ for (const auto& file : files) {+ const std::string name = pathString(file.stem());+ if (names.count(name)) continue;+ Json wf;+ std::string err;+ if (!loadWorkflow(pathString(file), wf, err)) continue; // UI-format copies, notes...+ const std::string kind = workflowKind(wf);+ if (kind.empty()) continue;+ names.insert(name);+ std::error_code cec;+ const auto canonical = std::filesystem::weakly_canonical(file, cec);+ out.push_back({name, pathString(cec ? file : canonical), kind});+ }+ }+ }+ return out;+}++std::string defaultWorkflow(const std::string& kind, const std::vector<std::string>& preferred) {+ const auto all = bundledWorkflows();+ for (const auto& want : preferred)+ for (const auto& w : all)+ if (w.kind == kind && lower(w.name).find(lower(want)) != std::string::npos) return w.path;+ for (const auto& w : all)+ if (w.kind == kind) return w.path;+ return {};+}++} // namespace sf::ai::comfydiff --git a/src/ai/ComfyUI.h b/src/ai/ComfyUI.hnew file mode 100644index 00000000..432b454a--- /dev/null+++ b/src/ai/ComfyUI.h@@ -0,0 +1,125 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// ComfyUI as an image-to-3D / text-to-3D generator: the artist's own ComfyUI+// (this computer, or a studio GPU box) runs any workflow they like - Hunyuan3D,+// TRELLIS, their own node graph - and SculptForge imports the mesh it writes.+//+// Workflows are ComfyUI's API format ("Export (API)"): {node_id: {class_type,+// inputs, _meta: {title}}}. The contract, by node title:+// sf_image - LoadImage node(s) that get the picture (fallback: the first LoadImage)+// sf_prompt - node whose text / prompt / string input gets the description+// sf_seed - node whose seed gets a fresh value (as do inputs named seed / noise_seed)+// The result is the first mesh file (.glb .gltf .obj .ply .stl .fbx .usdz) in+// the prompt's /history outputs, downloaded through /view.+#pragma once++#include "ai/Generator3D.h"+#include "core/Json.h"++#include <cstdint>+#include <optional>+#include <string>+#include <vector>++namespace sf::ai {++struct AiSettings;+struct GenerationProgress;++namespace comfy {++constexpr const char* kDefaultUrl = "http://127.0.0.1:8188";++// "localhost:8188/" -> "http://localhost:8188"+std::string normalizeUrl(const std::string& url);++// What /system_stats reports, for "Test connection".+struct ServerInfo {+ std::string comfyVersion; // empty on old servers+ std::string pythonVersion;+ std::string pytorchVersion;+ std::vector<std::string> devices; // "NVIDIA GeForce RTX 4090 (24.0 GB)", "cpu"+ std::string summary() const; // "ComfyUI 0.3.40 - NVIDIA GeForce RTX 4090 (24.0 GB)"+};+bool parseSystemStats(const Json& stats, ServerInfo& info);+// Blocking. On failure `error` says what to do (start ComfyUI, --listen...).+bool checkServer(const std::string& url, ServerInfo& info, std::string& error, int timeoutSeconds = 5);++// --- Workflows -------------------------------------------------------------++// True for the editor's own save format ({nodes: [...], links: [...]}), which cannot be queued.+bool isUiFormat(const Json& workflow);+// Accepts a bare API workflow or one wrapped as {"prompt": {...}}; explains UI-format files.+bool parseWorkflow(const std::string& text, Json& workflow, std::string& error);+bool loadWorkflow(const std::string& path, Json& workflow, std::string& error);++struct ContractInputs {+ std::string image; // uploaded image name ("sub/name.png"); empty = no image+ std::string prompt; // description; empty = leave prompts alone+ std::optional<uint64_t> seed; // set on sf_seed and seed / noise_seed inputs+};+// Which node ids got which input (for status lines and tests).+struct ContractReport {+ std::vector<std::string> imageNodes, promptNodes, seedNodes;+};+// Edits `workflow` in place. Fails when the image or prompt has nowhere to go.+bool applyContract(Json& workflow, const ContractInputs& in, std::string& error, ContractReport* report = nullptr);+// Whether a workflow takes a picture or a description ("image", "text" or "" if neither).+std::string workflowKind(const Json& workflow);++// --- Results ---------------------------------------------------------------++struct OutputFile {+ std::string nodeId, key; // where it was found in outputs ("9", "3d")+ std::string filename, subfolder, type = "output";+ bool isMesh() const;+};+struct HistoryEntry {+ bool found = false; // the prompt is in /history (finished one way or another)+ bool completed = false;+ bool failed = false;+ std::string error; // execution_error / interrupted, as the artist should read it+ std::vector<OutputFile> files;+};+// `history` is the /history/<id> reply.+HistoryEntry parseHistory(const Json& history, const std::string& promptId);+const OutputFile* firstMesh(const std::vector<OutputFile>& files);+// "node 9 'images': preview_00001_.png; node 12 'text'" - what a workflow produced, for errors.+std::string describeOutputs(const Json& history, const std::string& promptId);+std::string viewUrl(const std::string& baseUrl, const OutputFile& file);++// A rejected /prompt (HTTP 400) as a readable message: missing custom nodes,+// per-node input errors (node_errors), "no outputs"... `workflow` names the nodes.+std::string promptError(const std::string& body, const Json& workflow);+// Node types the workflow uses that /object_info does not list.+std::vector<std::string> missingNodeTypes(const Json& workflow, const Json& objectInfo);++// Queue position from /queue: 0 = running now, n = n prompts ahead, -1 = not queued.+int queuePosition(const Json& queue, const std::string& promptId);++// --- Running ----------------------------------------------------------------++// Blocking; run on a worker thread. Uses settings.comfyUrl and `workflowPath`+// (empty = settings.comfyImageWorkflow / comfyTextWorkflow). `imagePath` or+// `prompt` (text to 3D) drives the workflow; writes `<outputStem><ext>`.+// Cancelling removes the prompt from the queue, or interrupts it if running.+GeneratorResult run(const AiSettings& settings, const std::string& workflowPath, const std::string& imagePath,+ const std::string& prompt, const std::string& outputStem, GenerationProgress* progress,+ std::optional<uint64_t> seed = std::nullopt);++// Workflows shipped with SculptForge (integrations/comfyui/workflows), found+// next to the executable (../share/sculptforge/comfyui, ../Resources/comfyui,+// ./comfyui) or in the source tree it was built from.+struct BundledWorkflow {+ std::string name; // file name without .json+ std::string path;+ std::string kind; // "image" or "text"+};+std::vector<BundledWorkflow> bundledWorkflows();+// The bundled workflow of `kind` to use when none is chosen (names containing+// one of `preferred` win, in that order); empty if none is installed.+std::string defaultWorkflow(const std::string& kind, const std::vector<std::string>& preferred = {});++} // namespace comfy+} // namespace sf::aidiff --git a/src/ai/Http.cpp b/src/ai/Http.cppindex 510569a5..ca7e8d07 100644--- a/src/ai/Http.cpp+++ b/src/ai/Http.cpp@@ -2,6 +2,9 @@// SPDX-License-Identifier: GPL-3.0-or-later#include "ai/Http.h"+#include <chrono>+#include <random>+#if defined(_WIN32)#define WIN32_LEAN_AND_MEAN#include <windows.h>@@ -24,6 +27,21 @@ HttpResponse httpRequest(const std::string& method, const std::string& url, consreturn r;}+std::string multipartBody(const std::vector<MultipartField>& fields, std::string& contentTypeHeader) {+ std::random_device rd;+ std::mt19937_64 rng((uint64_t(rd()) << 32) ^ rd() ^ uint64_t(std::chrono::steady_clock::now().time_since_epoch().count()));+ std::string boundary = "----sculptforge";+ for (int i = 0; i < 16; ++i) boundary += "0123456789abcdef"[rng() % 16];+ contentTypeHeader = "Content-Type: multipart/form-data; boundary=" + boundary;+ std::string body;+ for (const auto& f : fields) {+ body += "--" + boundary + "\r\nContent-Disposition: form-data; name=\"" + f.name + "\"";+ if (!f.fileName.empty()) body += "; filename=\"" + f.fileName + "\"\r\nContent-Type: " + f.contentType;+ body += "\r\n\r\n" + f.value + "\r\n";+ }+ return body + "--" + boundary + "--\r\n";+}+#if defined(_WIN32)namespace {diff --git a/src/ai/Http.h b/src/ai/Http.hindex 80050417..b6ff2d46 100644--- a/src/ai/Http.h+++ b/src/ai/Http.h@@ -28,6 +28,16 @@ HttpResponse httpRequestStream(const std::string& method, const std::string& urlconst std::string& body, int timeoutSeconds,const std::function<bool(const char* data, size_t size)>& onData);+// One part of a multipart/form-data body: a plain field, or a file when `fileName` is set.+struct MultipartField {+ std::string name;+ std::string value;+ std::string fileName;+ std::string contentType = "application/octet-stream";+};+// Builds the body; `contentTypeHeader` receives the matching "Content-Type: multipart/form-data; boundary=..." header.+std::string multipartBody(const std::vector<MultipartField>& fields, std::string& contentTypeHeader);+inline HttpResponse httpPostJson(const std::string& url, const std::vector<std::string>& headers, const std::string& body,int timeoutSeconds = 120) {return httpRequest("POST", url, headers, body, timeoutSeconds);diff --git a/src/ai/ImageTo3DService.cpp b/src/ai/ImageTo3DService.cppindex 63ed6670..789886e5 100644--- a/src/ai/ImageTo3DService.cpp+++ b/src/ai/ImageTo3DService.cpp@@ -2,6 +2,7 @@// SPDX-License-Identifier: GPL-3.0-or-later#include "ai/ImageTo3DService.h"+#include "ai/ComfyUI.h"#include "ai/GradioClient.h"#include "ai/Http.h"#include "core/Image.h"@@ -503,6 +504,7 @@ GeneratorResult generate3D(const AiSettings& s, const std::string& imagePath, co}if (s.generatorService == "inflate") return runInflate(imagePath, outputStem, progress);if (s.generatorService == "fal") return runFal(s, imagePath, outputStem, progress);+ if (s.generatorService == "comfyui") return comfy::run(s, {}, imagePath, {}, outputStem, progress);return runHuggingFace(s, imagePath, {}, outputStem, progress);}@@ -531,8 +533,9 @@ GeneratorResult generate3DFromText(const AiSettings& s, const std::string& prompif (s.generatorService == "meshy") return runMeshyRequest(s, "/openapi/v2/text-to-3d", meshyTextBody(s.meshyModel, prompt), outputStem, progress);if (s.generatorService == "fal")return runFalRequest(s, "fal-ai/hunyuan-3d/v3.1/rapid/text-to-3d", falTextBody(prompt), outputStem, progress);+ if (s.generatorService == "comfyui") return comfy::run(s, {}, {}, prompt, outputStem, progress);if (s.generatorService == "command" || s.generatorService == "inflate") {- result.error = "Text to 3D needs Hugging Face (free), fal.ai or Meshy - pick one of those services";+ result.error = "Text to 3D needs Hugging Face (free), fal.ai, Meshy or ComfyUI - pick one of those services";return result;}return runHuggingFace(s, {}, prompt, outputStem, progress);diff --git a/src/app/App.cpp b/src/app/App.cppindex 1cf8bd80..60dd6574 100644--- a/src/app/App.cpp+++ b/src/app/App.cpp@@ -48,6 +48,7 @@ const SDL_DialogFileFilter kFbxFilters[] = {{"FBX", "fbx"}, {"All files", "*"}};const SDL_DialogFileFilter kUsdFilters[] = {{"USDZ package", "usdz"}, {"USD text", "usda"}};const SDL_DialogFileFilter kSplatFilters[] = {{"Gaussian splats (PLY)", "ply"}, {"glTF splats", "glb"}, {"OpenUSD", "usda;usdz"}};const SDL_DialogFileFilter kImageFilters[] = {{"Images", "png;jpg;jpeg;bmp;tga;psd;gif"}, {"All files", "*"}};+const SDL_DialogFileFilter kWorkflowFilters[] = {{"ComfyUI API workflow", "json"}, {"All files", "*"}};const char* kExamplePrompts[] = {"Sharp wrinkles that fade out at the ends",@@ -168,6 +169,7 @@ bool App::init(const AppOptions& options) {std::snprintf(falModel_, sizeof falModel_, "%s", aiSettings_.falModel.c_str());std::snprintf(meshyKey_, sizeof meshyKey_, "%s", aiSettings_.meshyKey.c_str());std::snprintf(hfToken_, sizeof hfToken_, "%s", aiSettings_.hfToken.c_str());+ std::snprintf(comfyUrl_, sizeof comfyUrl_, "%s", aiSettings_.comfyUrl.c_str());// Pixel density (Retina / Wayland scaling) is handled by the backend via the// framebuffer scale; only scale the UI for any additional content scaling.float density = SDL_GetWindowPixelDensity(window_);@@ -833,7 +835,7 @@ void SDLCALL onFileChosen(void* userdata, const char* const* files, int) {void App::queueFile(FileAction action, std::string path) {if (action != FileAction::OpenObj && action != FileAction::LoadBrush && action != FileAction::OpenImage &&- action != FileAction::AppendSubtool && !path.empty() &&+ action != FileAction::AppendSubtool && action != FileAction::ComfyWorkflow && !path.empty() &&std::filesystem::path(std::u8string(path.begin(), path.end())).extension().empty())path += action == FileAction::SaveObj ? ".obj": action == FileAction::SaveBrush ? ".sfbrush"@@ -940,6 +942,7 @@ void App::processPendingFiles() {runCommand("subtool_append", {{"path", f.path}});if (lastCommandOk_) camera_.frame(sceneBounds()); // a model can be any size, anywherebreak;+ case FileAction::ComfyWorkflow: setComfyWorkflow(f.path, comfyPickingText_); break;case FileAction::Count: break;}}@@ -1683,13 +1686,18 @@ void App::startGenerator() {aiSettings_.falModel = falModel_;aiSettings_.meshyKey = meshyKey_;aiSettings_.hfToken = hfToken_;+ aiSettings_.comfyUrl = comfyUrl_;+ // With no workflow chosen, ComfyUI runs the bundled one the panel shows (not saved, so updates reach it).+ ai::AiSettings settings = aiSettings_;+ if (settings.comfyImageWorkflow.empty()) settings.comfyImageWorkflow = comfyDefaultWorkflow(false);+ if (settings.comfyTextWorkflow.empty()) settings.comfyTextWorkflow = comfyDefaultWorkflow(true);auto stem = (std::filesystem::temp_directory_path() / ("sculptforge_generated_" + std::to_string(SDL_GetTicksNS()))).u8string();generatorTime_ = 0.0f;generatorProgress_ = std::make_shared<ai::GenerationProgress>();generatorProgress_->setStatus("Starting...");auto promise = std::make_shared<std::promise<ai::GeneratorResult>>();pendingGenerator_ = promise->get_future();- std::thread([promise, settings = aiSettings_, in = refImagePath_, text = textMode_ ? std::string(textPrompt_) : std::string(),+ std::thread([promise, settings, in = refImagePath_, text = textMode_ ? std::string(textPrompt_) : std::string(),out = std::string(stem.begin(), stem.end()), progress = generatorProgress_] {promise->set_value(text.empty() ? ai::generate3D(settings, in, out, progress.get()): ai::generate3DFromText(settings, text, out, progress.get()));@@ -1716,6 +1724,92 @@ void App::pollGenerator() {updateTitle();}+std::string App::comfyDefaultWorkflow(bool text) {+ if (!comfyWorkflowsScanned_) {+ comfyWorkflows_ = ai::comfy::bundledWorkflows();+ comfyWorkflowsScanned_ = true;+ }+ for (const auto& w : comfyWorkflows_)+ if (w.kind == (text ? "text" : "image")) return w.path;+ return {};+}++void App::setComfyWorkflow(const std::string& path, bool text) {+ (text ? aiSettings_.comfyTextWorkflow : aiSettings_.comfyImageWorkflow) = path;+ comfyWorkflowError_.clear();+ Json workflow;+ if (!ai::comfy::loadWorkflow(path, workflow, comfyWorkflowError_)) return;+ const std::string kind = ai::comfy::workflowKind(workflow);+ if (!text && kind != "image")+ comfyWorkflowError_ = "This workflow has no Load Image node, so it can't take the picture - is it a text-to-3D workflow?";+ else if (text && kind == "image")+ comfyWorkflowError_ = "This workflow starts from a picture - use it from the From image tab";+}++void App::drawComfyUi() {+ auto hint = [](const char* text) {+ ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetStyleColorVec4(ImGuiCol_TextDisabled));+ ImGui::TextWrapped("%s", text);+ ImGui::PopStyleColor();+ };+ hint("Runs a workflow on your own ComfyUI - this computer or a studio GPU server - with whatever models it has "+ "(Hunyuan3D, TRELLIS and more). Nothing goes anywhere else.");+ ImGui::InputText("Server", comfyUrl_, sizeof comfyUrl_);+ ImGui::BeginDisabled(pendingComfyCheck_.valid());+ if (ImGui::Button(pendingComfyCheck_.valid() ? "Connecting..." : "Test connection", ImVec2(-1, 0))) {+ auto promise = std::make_shared<std::promise<ComfyCheck>>();+ pendingComfyCheck_ = promise->get_future();+ std::thread([promise, url = std::string(comfyUrl_)] {+ ComfyCheck check;+ ai::comfy::ServerInfo info;+ check.ok = ai::comfy::checkServer(url, info, check.message);+ if (check.ok) check.message = "Connected: " + info.summary();+ promise->set_value(std::move(check));+ }).detach();+ }+ ImGui::EndDisabled();+ if (pendingComfyCheck_.valid() && pendingComfyCheck_.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {+ comfyCheck_ = pendingComfyCheck_.get();+ comfyChecked_ = true;+ }+ if (comfyChecked_) {+ ImGui::PushStyleColor(ImGuiCol_Text, comfyCheck_.ok ? ImVec4(0.45f, 0.8f, 0.5f, 1) : ImVec4(0.95f, 0.6f, 0.4f, 1));+ ImGui::TextWrapped("%s", comfyCheck_.message.c_str());+ ImGui::PopStyleColor();+ }++ // Workflow: a bundled one, or any API-format file.+ const std::string& chosen = textMode_ ? aiSettings_.comfyTextWorkflow : aiSettings_.comfyImageWorkflow;+ const std::string current = chosen.empty() ? comfyDefaultWorkflow(textMode_) : chosen;+ std::string preview = "Choose a workflow file...";+ if (!current.empty()) {+ const auto name = std::filesystem::path(std::u8string(current.begin(), current.end())).stem().u8string();+ preview.assign(name.begin(), name.end());+ }+ auto browse = [this] {+ comfyPickingText_ = textMode_;+ SDL_ShowOpenFileDialog(onFileChosen, &dialogRequests_[int(FileAction::ComfyWorkflow)], window_, kWorkflowFilters, 2,+ nullptr, false);+ };+ if (ImGui::BeginCombo("Workflow", preview.c_str())) {+ for (const auto& w : comfyWorkflows_) {+ if (w.kind != (textMode_ ? "text" : "image")) continue;+ if (ImGui::Selectable((w.name + " (bundled)").c_str(), current == w.path)) setComfyWorkflow(w.path, textMode_);+ }+ if (ImGui::Selectable("Other file (API format)...")) browse();+ ImGui::EndCombo();+ }+ if (!comfyWorkflowError_.empty()) {+ ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.95f, 0.6f, 0.4f, 1));+ ImGui::TextWrapped("%s", comfyWorkflowError_.c_str());+ ImGui::PopStyleColor();+ }+ hint(textMode_ ? "In ComfyUI use Workflow > Export (API). The node titled sf_prompt gets the description; the "+ "first mesh it saves is imported. See docs/comfyui.md."+ : "In ComfyUI use Workflow > Export (API). The Load Image node (title it sf_image) gets the picture; "+ "the first mesh it saves is imported. See docs/comfyui.md.");+}+void App::drawImageTo3D() {const float scale = uiScale_;const ImGuiViewport* vp = ImGui::GetMainViewport();@@ -1725,6 +1819,7 @@ void App::drawImageTo3D() {return;}if (ImGui::BeginTabBar("##source")) {+ const bool wasText = textMode_;if (ImGui::BeginTabItem("From image")) {textMode_ = false;ImGui::EndTabItem();@@ -1733,6 +1828,7 @@ void App::drawImageTo3D() {textMode_ = true;ImGui::EndTabItem();}+ if (textMode_ != wasText) comfyWorkflowError_.clear(); // it was about the other tab's workflowImGui::EndTabBar();}if (textMode_) {@@ -1770,18 +1866,21 @@ void App::drawImageTo3D() {const float labelW = ImGui::CalcTextSize("Resolution").x + ImGui::GetStyle().ItemInnerSpacing.x;ImGui::SeparatorText(textMode_ ? "Text to 3D (AI)" : "Full 3D model (AI reconstruction)");- hint("Builds the whole model - limbs, face, back - from one picture with a reconstruction model, then makes it "- "sculptable. Characters on a transparent background work best.");+ hint(textMode_ ? "Builds a whole model from the description with a generative 3D model, then makes it sculptable."+ : "Builds the whole model - limbs, face, back - from one picture with a reconstruction model, then "+ "makes it sculptable. Characters on a transparent background work best.");static const char* services[] = {"Hugging Face - free models", "fal.ai - paid, your key", "Meshy - paid, your key",- "My generator command", "Offline blockout (inflate silhouette)"};- static const char* serviceIds[] = {"huggingface", "fal", "meshy", "command", "inflate"};+ "ComfyUI - your server", "My generator command", "Offline blockout (inflate silhouette)"};+ static const char* serviceIds[] = {"huggingface", "fal", "meshy", "comfyui", "command", "inflate"};+ constexpr int kServices = int(std::size(serviceIds));int service = 0;- for (int i = 0; i < 5; ++i)+ for (int i = 0; i < kServices; ++i)if (aiSettings_.generatorService == serviceIds[i]) service = i;ImGui::SetNextItemWidth(-1);- if (ImGui::Combo("##service", &service, services, 5)) aiSettings_.generatorService = serviceIds[service];+ if (ImGui::Combo("##service", &service, services, kServices)) aiSettings_.generatorService = serviceIds[service];+ const std::string serviceId = serviceIds[service];ImGui::PushItemWidth(-labelW);- if (service == 0) {+ if (serviceId == "huggingface") {const auto& presets = ai::hfPresets();const char* preview = presets[0].label;for (const auto& p : presets)@@ -1796,7 +1895,7 @@ void App::drawImageTo3D() {? "Runs on Hugging Face's shared GPUs with your account's free daily allowance.": "Runs free on Hugging Face's shared GPUs with a small daily allowance. A free token ""(huggingface.co/settings/tokens, read access) raises it; TRELLIS.2 needs one.");- } else if (service == 1) {+ } else if (serviceId == "fal") {const auto& models = ai::falModels();const char* preview = falModel_;for (const auto& m : models)@@ -1810,11 +1909,13 @@ void App::drawImageTo3D() {ImGui::InputText("Model id", falModel_, sizeof falModel_);ImGui::InputText("API key", falKey_, sizeof falKey_, ImGuiInputTextFlags_Password);if (!falKey_[0]) hint(std::getenv("FAL_KEY") ? "Using FAL_KEY from the environment." : "Get a key at fal.ai/dashboard/keys");- } else if (service == 2) {+ } else if (serviceId == "meshy") {ImGui::InputText("API key", meshyKey_, sizeof meshyKey_, ImGuiInputTextFlags_Password);if (!meshyKey_[0])hint(std::getenv("MESHY_API_KEY") ? "Using MESHY_API_KEY from the environment." : "Get a key at meshy.ai (API settings)");- } else if (service == 3) {+ } else if (serviceId == "comfyui") {+ drawComfyUi();+ } else if (serviceId == "command") {hint("Any local model or script: {input} = image, {output} = .glb/.obj to write. See docs/generators.md.");ImGui::SetNextItemWidth(-1);ImGui::InputTextMultiline("##generator", generatorCommand_, sizeof generatorCommand_,@@ -1822,7 +1923,7 @@ void App::drawImageTo3D() {} else {hint("Offline and instant, but only a puffy blockout of the outline - not a real reconstruction.");}- if (service != 4) {+ if (serviceId != "inflate") {ImGui::Checkbox("Remesh for sculpting", &remeshGenerated_);if (remeshGenerated_) ImGui::SliderInt("Resolution", &remeshResolution_, 96, 400);}@@ -1842,6 +1943,7 @@ void App::drawImageTo3D() {aiSettings_.falModel = falModel_;aiSettings_.meshyKey = meshyKey_;aiSettings_.hfToken = hfToken_;+ aiSettings_.comfyUrl = comfyUrl_;std::string err;status_ = !aiSettingsPath_.empty() && aiSettings_.save(aiSettingsPath_, err) ? "Image to 3D settings saved": "Save failed " + err;diff --git a/src/app/App.h b/src/app/App.hindex 3eefa891..6f19bf46 100644--- a/src/app/App.h+++ b/src/app/App.h@@ -3,6 +3,7 @@#pragma once#include "ai/BrushAssistant.h"+#include "ai/ComfyUI.h"#include "ai/ImageTo3DService.h"#include "core/Commands.h"#include "core/Image.h"@@ -56,7 +57,7 @@ public:int run(); // returns the process exit codevoid shutdown();- enum class FileAction { OpenObj, SaveObj, ExportGlb, ExportLods, SaveBrush, LoadBrush, OpenImage, ExportUsd, SaveSplats, ExportUnreal, SaveProject, AppendSubtool, Count };+ enum class FileAction { OpenObj, SaveObj, ExportGlb, ExportLods, SaveBrush, LoadBrush, OpenImage, ExportUsd, SaveSplats, ExportUnreal, SaveProject, AppendSubtool, ComfyWorkflow, Count };struct DialogRequest {App* app;FileAction action;@@ -102,6 +103,9 @@ private:void drawGameExportPanel();void drawBrushLab();void drawImageTo3D();+ void drawComfyUi();+ std::string comfyDefaultWorkflow(bool text);+ void setComfyWorkflow(const std::string& path, bool text);bool loadReferenceImage(const std::string& path);void projectImageFromView();void startGenerator();@@ -216,6 +220,19 @@ private:bool projectInvert_ = false;char generatorCommand_[1024] = "";char falKey_[256] = "", falModel_[256] = "", meshyKey_[256] = "", hfToken_[256] = "";+ // ComfyUI: "Test connection" result, bundled workflows, the chosen file's problem (UI format...)+ char comfyUrl_[256] = "";+ struct ComfyCheck {+ bool ok = false;+ std::string message;+ };+ std::future<ComfyCheck> pendingComfyCheck_;+ ComfyCheck comfyCheck_;+ bool comfyChecked_ = false;+ std::vector<ai::comfy::BundledWorkflow> comfyWorkflows_;+ bool comfyWorkflowsScanned_ = false;+ bool comfyPickingText_ = false;+ std::string comfyWorkflowError_;bool remeshGenerated_ = true;int voxelResolution_ = 200;int splatMeshResolution_ = 192;diff --git a/src/app/Demo.h b/src/app/Demo.hindex 5527a272..041ae50c 100644--- a/src/app/Demo.h+++ b/src/app/Demo.h@@ -114,6 +114,7 @@ private:bool testing_ = false;std::vector<std::string> failures_;std::string frameText_, shotRequest_, shotDir_, scriptName_, lineText_;+ std::string scriptDir_; // relative `drop` paths start hereSDL_Keymod held_ = SDL_KMOD_NONE; // modifiers held by `hold` (Ctrl to mask, Ctrl+Shift to hide...)};diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cppindex 4d4e9c29..146a7f2b 100644--- a/src/app/UiTest.cpp+++ b/src/app/UiTest.cpp@@ -14,6 +14,7 @@// drag X0 Y0 X1 Y1 [S] left-drag in window fractions (rectangle hide, sliders...)// orbit DX DY right-drag on the background, in pixels// run COMMANDS sculptforge-cli commands, e.g. run new_sphere subdivisions=4+// drop FILE as if FILE were dropped onto the window (relative to the script's folder)// wait SECONDS / wait_idle [TIMEOUT] (AI requests and background jobs) / wait_for LABEL [TIMEOUT]// expect LABEL / expect_not LABEL// expect_text TEXT / expect_no_text TEXT any text drawn this frame contains TEXT@@ -409,6 +410,12 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& e} else if (verb == "orbit") {if (!need(2)) return false;orbit(num(1, 0), num(2, 0), 0.4f);+ } else if (verb == "drop") {+ if (!need(1)) return false;+ auto file = std::filesystem::path(std::u8string(w[1].begin(), w[1].end()));+ if (file.is_relative()) file = std::filesystem::path(std::u8string(scriptDir_.begin(), scriptDir_.end())) / file;+ const auto u8 = file.lexically_normal().u8string();+ run([this, path = std::string(u8.begin(), u8.end())] { app_.openPathConfirmed(path); });} else if (verb == "run") {if (!need(1)) return false;std::vector<ScriptStatement> statements;@@ -429,7 +436,7 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& econst float timeout = num(1, 120.0f);steps_.push_back([this, line, timeout](float t) {const bool idle = !busy(app_.pendingBrush_) && !busy(app_.pendingGenerator_) && !busy(app_.pendingModels_) &&- !busy(app_.pendingUnreal_);+ !busy(app_.pendingUnreal_) && !busy(app_.pendingComfyCheck_);if (idle) return true;SDL_Delay(10); // real work runs in real timeif (t > timeout) fail(line, "still busy after " + number(timeout) + " s");@@ -530,6 +537,8 @@ bool Demo::loadScript(const std::string& path, std::string& error) {}testing_ = true;scriptName_ = std::filesystem::path(std::u8string(path.begin(), path.end())).filename().string();+ const auto dir = std::filesystem::path(std::u8string(path.begin(), path.end())).parent_path().u8string();+ scriptDir_.assign(dir.begin(), dir.end());shotDir_ = app_.options_.uiOutDir;if (!shotDir_.empty()) {std::error_code ec;diff --git a/src/core/Json.cpp b/src/core/Json.cppindex 8abff8d5..1451cc16 100644--- a/src/core/Json.cpp+++ b/src/core/Json.cpp@@ -252,7 +252,7 @@ void Json::dumpTo(std::string& out, bool pretty, int indent) const {case Type::Number: {if (!std::isfinite(number_)) {out += "null";- } else if (number_ == std::floor(number_) && std::abs(number_) < 1e15) {+ } else if (number_ == std::floor(number_) && std::abs(number_) < 9007199254740992.0) { // 2^53: exact in a doubleout += std::to_string(static_cast<long long>(number_));} else {char buf[32];diff --git a/tests/test_comfyui.cpp b/tests/test_comfyui.cppnew file mode 100644index 00000000..f306e0c9--- /dev/null+++ b/tests/test_comfyui.cpp@@ -0,0 +1,320 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// ComfyUI client: the sf_* workflow contract, format detection, /history and+// /prompt reply parsing. No server needed.+#include "TestHarness.h"++#include "ai/BrushAssistant.h"+#include "ai/ComfyUI.h"+#include "ai/Http.h"++#include <fstream>++using namespace sf;+using namespace sf::ai;++namespace {++Json parsed(const std::string& text) {+ Json j;+ std::string err;+ CHECK(Json::parse(text, j, err));+ return j;+}++// Hunyuan3D-style image-to-3D graph, as "Export (API)" writes it.+const char* kImageWorkflow = R"j({+ "3": {"class_type": "KSampler", "inputs": {"seed": 1125899906842624, "steps": 30, "model": ["4", 0]}, "_meta": {"title": "KSampler"}},+ "7": {"class_type": "LoadImage", "inputs": {"image": "example.png"}, "_meta": {"title": "Load Image"}},+ "8": {"class_type": "LoadImage", "inputs": {"image": "mask.png"}, "_meta": {"title": " SF_Image "}},+ "9": {"class_type": "SaveGLB", "inputs": {"filename_prefix": "mesh/ComfyUI", "mesh": ["3", 0]}, "_meta": {"title": "Save GLB"}}+})j";++} // namespace++TEST(comfy_url_normalised) {+ CHECK(comfy::normalizeUrl("") == "http://127.0.0.1:8188");+ CHECK(comfy::normalizeUrl(" localhost:8188/ ") == "http://localhost:8188");+ CHECK(comfy::normalizeUrl("https://gpu.studio.lan/comfy//") == "https://gpu.studio.lan/comfy");+}++TEST(comfy_image_goes_to_titled_nodes_only) {+ Json wf = parsed(kImageWorkflow);+ comfy::ContractInputs in;+ in.image = "sculptforge_abc.png";+ comfy::ContractReport rep;+ std::string err;+ CHECK(comfy::applyContract(wf, in, err, &rep));+ CHECK(rep.imageNodes.size() == 1 && rep.imageNodes[0] == "8"); // title match is case and space tolerant+ CHECK(wf["8"]["inputs"]["image"].asString() == "sculptforge_abc.png");+ CHECK(wf["7"]["inputs"]["image"].asString() == "example.png"); // untitled LoadImage left alone+ CHECK(wf["9"]["inputs"]["mesh"][0].asString() == "3"); // links survive+}++TEST(comfy_image_falls_back_to_first_load_image) {+ Json wf = parsed(R"j({+ "2": {"class_type": "LoadImageMask", "inputs": {"image": "m.png", "channel": "alpha"}},+ "5": {"class_type": "LoadImage", "inputs": {"image": "a.png"}},+ "6": {"class_type": "LoadImage", "inputs": {"image": "b.png"}}+ })j");+ comfy::ContractInputs in;+ in.image = "up/in.png";+ comfy::ContractReport rep;+ std::string err;+ CHECK(comfy::applyContract(wf, in, err, &rep));+ CHECK(rep.imageNodes.size() == 1 && rep.imageNodes[0] == "5");+ CHECK(wf["5"]["inputs"]["image"].asString() == "up/in.png");+ CHECK(wf["6"]["inputs"]["image"].asString() == "b.png");+ CHECK(wf["2"]["inputs"]["image"].asString() == "m.png");++ // Node-pack look-alikes count when there is no stock LoadImage.+ Json custom = parsed(R"j({"1": {"class_type": "LoadImageFromUrlOrPath", "inputs": {"image": "x"}}})j");+ CHECK(comfy::applyContract(custom, in, err, &rep) && rep.imageNodes[0] == "1");++ Json none = parsed(R"j({"1": {"class_type": "CLIPTextEncode", "inputs": {"text": "x"}}})j");+ CHECK(!comfy::applyContract(none, in, err));+ CHECK(err.find("Load Image") != std::string::npos);+}++TEST(comfy_prompt_contract) {+ // sf_prompt: text, then prompt, then string inputs.+ Json wf = parsed(R"j({+ "1": {"class_type": "CLIPTextEncode", "inputs": {"text": "old", "clip": ["4", 1]}, "_meta": {"title": "Positive"}},+ "2": {"class_type": "MyPromptNode", "inputs": {"prompt": "old"}, "_meta": {"title": "sf_prompt"}},+ "3": {"class_type": "StringConcat", "inputs": {"string": "old", "other": "x"}, "_meta": {"title": "sf_prompt"}},+ "4": {"class_type": "PrimitiveStringMultiline", "inputs": {"value": "old"}, "_meta": {"title": "sf_prompt"}}+ })j");+ comfy::ContractInputs in;+ in.prompt = "a stylised dragon head";+ comfy::ContractReport rep;+ std::string err;+ CHECK(comfy::applyContract(wf, in, err, &rep));+ CHECK(rep.promptNodes.size() == 3);+ CHECK(wf["2"]["inputs"]["prompt"].asString() == "a stylised dragon head");+ CHECK(wf["3"]["inputs"]["string"].asString() == "a stylised dragon head");+ CHECK(wf["3"]["inputs"]["other"].asString() == "x");+ CHECK(wf["4"]["inputs"]["value"].asString() == "a stylised dragon head");+ CHECK(wf["1"]["inputs"]["text"].asString() == "old"); // titled nodes win over the fallback++ // No sf_prompt: the first text encoder that isn't the negative prompt.+ Json plain = parsed(R"j({+ "6": {"class_type": "CLIPTextEncode", "inputs": {"text": "blurry"}, "_meta": {"title": "Negative Prompt"}},+ "7": {"class_type": "CLIPTextEncode", "inputs": {"text": "a cat"}, "_meta": {"title": "CLIP Text Encode (Prompt)"}}+ })j");+ CHECK(comfy::applyContract(plain, in, err, &rep));+ CHECK(rep.promptNodes.size() == 1 && rep.promptNodes[0] == "7");+ CHECK(plain["6"]["inputs"]["text"].asString() == "blurry");++ Json bad = parsed(R"j({"1": {"class_type": "Note", "inputs": {"width": 3}, "_meta": {"title": "sf_prompt"}}})j");+ CHECK(!comfy::applyContract(bad, in, err));+ CHECK(err.find("sf_prompt") != std::string::npos && err.find("node 1") != std::string::npos);++ Json nowhere = parsed(R"j({"1": {"class_type": "LoadImage", "inputs": {"image": "a.png"}}})j");+ CHECK(!comfy::applyContract(nowhere, in, err));+ CHECK(err.find("sf_prompt") != std::string::npos);+}++TEST(comfy_seed_contract) {+ Json wf = parsed(R"j({+ "1": {"class_type": "KSampler", "inputs": {"seed": 5, "steps": 20}},+ "2": {"class_type": "SamplerCustom", "inputs": {"noise_seed": 5}},+ "3": {"class_type": "PrimitiveInt", "inputs": {"value": 5}, "_meta": {"title": "sf_seed"}},+ "4": {"class_type": "KSampler", "inputs": {"seed": ["3", 0]}},+ "5": {"class_type": "PrimitiveInt", "inputs": {"value": 5}}+ })j");+ comfy::ContractInputs in;+ in.seed = 123456789012345ull; // above 1e15 would print as 1.2e+15 and fail ComfyUI's INT check+ comfy::ContractReport rep;+ std::string err;+ CHECK(comfy::applyContract(wf, in, err, &rep));+ CHECK(rep.seedNodes.size() == 3);+ CHECK(wf["1"]["inputs"]["seed"].asNumber() == 123456789012345.0);+ CHECK(wf["2"]["inputs"]["noise_seed"].asNumber() == 123456789012345.0);+ CHECK(wf["3"]["inputs"]["value"].asNumber() == 123456789012345.0);+ CHECK(wf["4"]["inputs"]["seed"].isArray()); // linked seed stays linked+ CHECK(wf["5"]["inputs"]["value"].asNumber() == 5.0);+ CHECK(wf.dump().find("123456789012345") != std::string::npos);+ CHECK(parsed(kImageWorkflow).dump().find("1125899906842624") != std::string::npos); // big exported seeds stay integers++ // Nothing requested: nothing touched.+ Json same = parsed(kImageWorkflow);+ CHECK(comfy::applyContract(same, {}, err));+ CHECK(same.dump() == parsed(kImageWorkflow).dump());+}++TEST(comfy_detects_ui_format) {+ const char* ui = R"j({"last_node_id": 9, "last_link_id": 9, "nodes": [{"id": 7, "type": "LoadImage", "widgets_values": ["a.png", "image"]}],+ "links": [], "version": 0.4})j";+ CHECK(comfy::isUiFormat(parsed(ui)));+ CHECK(!comfy::isUiFormat(parsed(kImageWorkflow)));+ Json wf;+ std::string err;+ CHECK(!comfy::parseWorkflow(ui, wf, err));+ CHECK(err.find("Export (API)") != std::string::npos);++ CHECK(comfy::parseWorkflow(kImageWorkflow, wf, err));+ CHECK(wf["9"]["class_type"].asString() == "SaveGLB");+ // A /prompt request body works too.+ CHECK(comfy::parseWorkflow(std::string("{\"prompt\": ") + kImageWorkflow + ", \"client_id\": \"x\"}", wf, err));+ CHECK(wf["7"]["class_type"].asString() == "LoadImage");++ CHECK(!comfy::parseWorkflow("{\"a\": 1}", wf, err) && err.find("class_type") != std::string::npos);+ CHECK(!comfy::parseWorkflow("{}", wf, err));+ CHECK(!comfy::parseWorkflow("not json", wf, err) && err.find("JSON") != std::string::npos);++ const std::string path = tempPath("sf_comfy_ui_format.json");+ std::ofstream(path) << ui;+ CHECK(!comfy::loadWorkflow(path, wf, err));+ CHECK(err.find("sf_comfy_ui_format.json") != std::string::npos && err.find("Export (API)") != std::string::npos);+ CHECK(!comfy::loadWorkflow(tempPath("sf_comfy_missing.json"), wf, err));+}++TEST(comfy_workflow_kind) {+ CHECK(comfy::workflowKind(parsed(kImageWorkflow)) == "image");+ CHECK(comfy::workflowKind(parsed(R"j({"1": {"class_type": "CLIPTextEncode", "inputs": {"text": "x"}}})j")) == "text");+ CHECK(comfy::workflowKind(parsed(R"j({"1": {"class_type": "Foo", "inputs": {}, "_meta": {"title": "sf_prompt"}}})j")) == "text");+ CHECK(comfy::workflowKind(parsed(R"j({"1": {"class_type": "SaveGLB", "inputs": {}}})j")).empty());+}++TEST(comfy_history_output_shapes) {+ // SaveGLB ("3d" objects), Preview3D ("result" strings), images, text.+ const char* history = R"j({"abc": {+ "prompt": [3, "abc", {"9": {"class_type": "SaveImage", "inputs": {}}, "12": {"class_type": "SaveGLB", "inputs": {}, "_meta": {"title": "Save Mesh"}}}, {}, ["9", "12"]],+ "outputs": {+ "9": {"images": [{"filename": "ComfyUI_00001_.png", "subfolder": "", "type": "output"}]},+ "11": {"text": ["hello"]},+ "12": {"3d": [{"filename": "ComfyUI_00003_.glb", "subfolder": "mesh", "type": "output"}], "camera_info": [null], "model_3d_info": []}+ },+ "status": {"status_str": "success", "completed": true, "messages": [["execution_start", {"prompt_id": "abc"}]]}}})j";+ comfy::HistoryEntry h = comfy::parseHistory(parsed(history), "abc");+ CHECK(h.found && h.completed && !h.failed);+ CHECK(h.files.size() == 2);+ const comfy::OutputFile* mesh = comfy::firstMesh(h.files);+ CHECK(mesh && mesh->filename == "ComfyUI_00003_.glb" && mesh->subfolder == "mesh" && mesh->nodeId == "12" && mesh->key == "3d");+ CHECK(comfy::viewUrl("localhost:8188/", *mesh) ==+ "http://localhost:8188/view?filename=ComfyUI_00003_.glb&subfolder=mesh&type=output");+ CHECK(!comfy::parseHistory(parsed(history), "other").found);++ // Preview3D-style strings, an annotated name, an absolute path inside output/, and nested lists.+ comfy::HistoryEntry s = comfy::parseHistory(parsed(R"j({"p": {"outputs": {+ "4": {"result": ["mesh/out 1.OBJ", {"position": [1, 2]}, null]},+ "5": {"meshes": [["sub/a.ply [temp]"]]},+ "6": {"files": ["C:\\ComfyUI\\output\\3d\\b.stl", "/srv/comfy/notes.txt"]}},+ "status": {"status_str": "success", "completed": true, "messages": []}}})j"), "p");+ CHECK(s.files.size() == 3);+ CHECK(s.files[0].filename == "out 1.OBJ" && s.files[0].subfolder == "mesh" && s.files[0].type == "output" && s.files[0].isMesh());+ CHECK(s.files[1].filename == "a.ply" && s.files[1].subfolder == "sub" && s.files[1].type == "temp");+ CHECK(s.files[2].filename == "b.stl" && s.files[2].subfolder == "3d" && s.files[2].type == "output");+ CHECK(comfy::viewUrl("http://h:1", s.files[0]) == "http://h:1/view?filename=out%201.OBJ&subfolder=mesh&type=output");++ // Old servers: no status block, outputs mean done.+ CHECK(comfy::parseHistory(parsed(R"j({"q": {"outputs": {}}})j"), "q").completed);+}++TEST(comfy_history_without_mesh_lists_outputs) {+ const char* history = R"j({"abc": {+ "prompt": [3, "abc", {"9": {"class_type": "SaveImage", "inputs": {}, "_meta": {"title": "Save Image"}}}, {}, ["9"]],+ "outputs": {"9": {"images": [{"filename": "a.png", "subfolder": "", "type": "output"},+ {"filename": "b.png"}, {"filename": "c.png"}, {"filename": "d.png"}]}},+ "status": {"status_str": "success", "completed": true, "messages": []}}})j";+ comfy::HistoryEntry h = comfy::parseHistory(parsed(history), "abc");+ CHECK(h.completed && h.files.size() == 4 && !comfy::firstMesh(h.files));+ const std::string d = comfy::describeOutputs(parsed(history), "abc");+ CHECK(d == "node 9 (SaveImage) 'images': a.png, b.png, c.png (+1 more)");+ CHECK(comfy::describeOutputs(parsed(R"j({"x": {"outputs": {}}})j"), "x") == "none");+}++TEST(comfy_history_errors) {+ comfy::HistoryEntry e = comfy::parseHistory(parsed(R"j({"abc": {"outputs": {}, "status": {"status_str": "error", "completed": false,+ "messages": [["execution_start", {}], ["execution_error", {"node_id": "5", "node_type": "Hy3DGenerateMesh",+ "exception_message": "CUDA out of memory.\n", "exception_type": "torch.OutOfMemoryError"}]]}}})j"), "abc");+ CHECK(e.found && e.failed && !e.completed);+ CHECK(e.error.find("Hy3DGenerateMesh (node 5) failed: CUDA out of memory.") == 0);+ CHECK(e.error.find("ran out of memory") != std::string::npos);++ comfy::HistoryEntry i = comfy::parseHistory(parsed(R"j({"abc": {"outputs": {}, "status": {"status_str": "error", "completed": false,+ "messages": [["execution_interrupted", {"node_id": "3"}]]}}})j"), "abc");+ CHECK(i.failed && i.error.find("interrupted") != std::string::npos);+}++TEST(comfy_prompt_errors) {+ Json wf = parsed(kImageWorkflow);+ const std::string missing = comfy::promptError(R"j({"error": {"type": "missing_node_type",+ "message": "Node 'Hy3DGenerateMesh' not found. The custom node may not be installed.", "details": "",+ "extra_info": {"node_id": "9", "class_type": "SaveGLB", "node_title": "Save GLB"}}, "node_errors": {}})j", wf);+ CHECK(missing.find("node 9 (SaveGLB)") != std::string::npos);+ CHECK(missing.find("Install Missing Custom Nodes") != std::string::npos);++ const std::string nodeErrors = comfy::promptError(R"j({"error": {"type": "prompt_outputs_failed_validation",+ "message": "Prompt outputs failed validation", "details": ""}, "node_errors": {"7": {"errors": [{"type": "value_not_in_list",+ "message": "Value not in list", "details": "ckpt_name: 'hunyuan3d-dit-v2.safetensors' not in []", "extra_info": {}}],+ "dependent_outputs": ["9"], "class_type": "LoadImage"}}})j", wf);+ CHECK(nodeErrors.find("node 7 (LoadImage) - Value not in list: ckpt_name") != std::string::npos);+ CHECK(nodeErrors.find("model file") != std::string::npos);+ const std::string badChoice = comfy::promptError(R"j({"error": {"type": "prompt_outputs_failed_validation", "message": "x"},+ "node_errors": {"2": {"errors": [{"type": "value_not_in_list", "message": "Value not in list",+ "details": "upscale_method: 'bogus' not in ['nearest-exact', 'bilinear']"}], "class_type": "ImageScale"}}})j", wf);+ CHECK(badChoice == "ComfyUI rejected the workflow: node 2 - Value not in list: upscale_method: 'bogus' not in ['nearest-exact', 'bilinear']");++ CHECK(comfy::promptError(R"j({"error": {"type": "prompt_no_outputs", "message": "Prompt has no outputs", "details": ""},+ "node_errors": []})j", wf).find("no output node") != std::string::npos);+ CHECK(comfy::promptError("<html>Bad gateway</html>", wf).find("Bad gateway") != std::string::npos);+ CHECK(comfy::promptError(R"j({"error": {"type": "invalid_prompt", "message": "Cannot execute", "details": "Node ID '#5'"}})j", wf) ==+ "ComfyUI rejected the workflow: Cannot execute (Node ID '#5')");++ const auto list = comfy::missingNodeTypes(wf, parsed(R"j({"LoadImage": {}, "SaveGLB": {}})j"));+ CHECK(list.size() == 1 && list[0] == "KSampler");+}++TEST(comfy_queue_position) {+ const Json q = parsed(R"j({"queue_running": [[10, "run", {}, {}, []]],+ "queue_pending": [[13, "c", {}, {}, []], [11, "a", {}, {}, []], [12, "b", {}, {}, []]]})j");+ CHECK(comfy::queuePosition(q, "run") == 0);+ CHECK(comfy::queuePosition(q, "a") == 1);+ CHECK(comfy::queuePosition(q, "c") == 3);+ CHECK(comfy::queuePosition(q, "zzz") == -1);+}++TEST(comfy_system_stats) {+ comfy::ServerInfo info;+ CHECK(comfy::parseSystemStats(parsed(R"j({"system": {"os": "posix", "comfyui_version": "0.3.40", "python_version": "3.12.3 (main) [GCC]",+ "pytorch_version": "2.7.0+cu128"}, "devices": [{"name": "cuda:0 NVIDIA GeForce RTX 4090 : cudaMallocAsync", "type": "cuda",+ "index": 0, "vram_total": 25757220864}]})j"), info));+ CHECK(info.summary() == "ComfyUI 0.3.40 - NVIDIA GeForce RTX 4090 (24.0 GB)");+ CHECK(info.pythonVersion == "3.12.3");+ CHECK(comfy::parseSystemStats(parsed(R"j({"system": {}, "devices": [{"name": "cpu", "type": "cpu", "vram_total": 3e10}]})j"), info));+ CHECK(info.summary() == "ComfyUI - cpu");+ CHECK(!comfy::parseSystemStats(parsed(R"j({"detail": "Not Found"})j"), info));+}++TEST(comfy_unreachable_server_is_explained) {+ comfy::ServerInfo info;+ std::string err;+ CHECK(!comfy::checkServer("127.0.0.1:9", info, err, 3));+ CHECK(err.find("http://127.0.0.1:9") != std::string::npos);+#if !defined(_WIN32)+ CHECK(err.find("Nothing is answering") != std::string::npos);+#endif+}++TEST(comfy_settings_round_trip) {+ AiSettings s;+ CHECK(s.comfyUrl == "http://127.0.0.1:8188");+ s.generatorService = "comfyui";+ s.comfyUrl = "http://gpu-box:8188";+ s.comfyImageWorkflow = "/w/hunyuan.json";+ s.comfyTextWorkflow = "/w/text.json";+ AiSettings r = AiSettings::fromJson(s.toJson());+ CHECK(r.generatorService == "comfyui" && r.comfyUrl == "http://gpu-box:8188");+ CHECK(r.comfyImageWorkflow == "/w/hunyuan.json" && r.comfyTextWorkflow == "/w/text.json");+}++TEST(multipart_body_layout) {+ std::string header;+ const std::string body = multipartBody({{"image", "PNGDATA", "in.png", "image/png"}, {"overwrite", "true", {}, {}}}, header);+ const std::string boundary = header.substr(header.find("boundary=") + 9);+ CHECK(header.starts_with("Content-Type: multipart/form-data; boundary=----sculptforge"));+ CHECK(body == "--" + boundary + "\r\nContent-Disposition: form-data; name=\"image\"; filename=\"in.png\"\r\nContent-Type: image/png\r\n\r\n"+ "PNGDATA\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"overwrite\"\r\n\r\ntrue\r\n--" + boundary + "--\r\n");+}diff --git a/tests/ui/comfyui.sfui b/tests/ui/comfyui.sfuinew file mode 100644index 00000000..db911782--- /dev/null+++ b/tests/ui/comfyui.sfui@@ -0,0 +1,31 @@+# Generate 3D with ComfyUI: pick the service, test a server that isn't there (the error must be+# readable and wrapped inside the panel), and see the workflow picker in both tabs.+drop ../../assets/demo/teddy-sketch.png+wait_for "Generate 3D" 5+click "Generate 3D/##service"+click "ComfyUI - your server"+expect "Generate 3D/Server"+expect "Generate 3D/Test connection"+expect "Generate 3D/Workflow"+expect_text Export (API)+shot comfyui-panel++# Nothing listens on port 9.+type "Generate 3D/Server" http://127.0.0.1:9+click "Generate 3D/Test connection"+wait_idle 15+expect_text Nothing is answering at http://127.0.0.1:9+expect_no_text Connected:+shot comfyui-dead-server++click "Generate 3D/Workflow"+expect_text Other file (API format)...+shot comfyui-workflows+click "Generate 3D/Workflow"+expect_no_text Other file (API format)...++click "Generate 3D/From text"+wait 0.2+expect "Generate 3D/Workflow"+expect_text sf_prompt+shot comfyui-text