Skip to content

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.

Auto UV from a command runs on the worker: no 10 s frame; Cancel at once

islandmed7c12b8bc7654
diff --git a/changes/fix-texture-create-stall.md b/changes/fix-texture-create-stall.md
new file mode 100644
index 00000000..c05af78d
--- /dev/null
+++ b/changes/fix-texture-create-stall.md
@@ -0,0 +1,17 @@
+---
+whats_new: **Auto UV never freezes the app**: the `uv_auto` command from the console, a script or an AI tool now unwraps
+ in the background like the **Tool > Texture Map > Auto UV** button, which gains a **Cancel** button beside its progress.
+fixed: Auto UV from the console, a script or an AI tool (`uv_auto`) froze the app while it unwrapped: about 10 s on a
+ 327k-triangle sculpt. It now runs on the same worker as the Auto UV button, so frames keep drawing (longest frame
+ under 100 ms, floor tier included). The next statement on the line, the AI tool's reply and a UI test's next step
+ wait for the UVs, so `uv_auto; texture_create` still works in one go. Macros keep running it in place.
+added: **Cancel** for Auto UV (Tool > Texture Map), beside its progress bar.
+changed: The console keeps one line for a job's progress ("Auto UV: 42%") instead of one line per percent.
+testers:
+ - Any OS: open the console (backtick) and run `new_sphere subdivisions=7` (327k triangles), then
+ `uv_auto; texture_create size=1024`. The view still orbits while it unwraps; the texture appears after.
+ - Any OS: `new_sphere subdivisions=7`, then Tool > Texture Map > Auto UV, then Cancel: the progress bar goes away and the Part has no UVs.
+---
+Found by slowtests: its probe saw a 15 s frame "after texture_create", but SF_UI_PROFILE charged a run line's own time
+to the next line. The frame was the synchronous `uv_auto` (xatlas) before it; texture_create itself costs < 15 ms.
+The profiler now charges each step its own time (Demo::update).
diff --git a/src/app/App.cpp b/src/app/App.cpp
index 6bddfecc..357e7969 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -2588,7 +2588,9 @@ void App::runCommand(const std::string& name, const Args& args) {
flushBrushEdits(); // commands may replace brushes: save edits first, then watch what's there afterwards
crashlog::action(name); // (failures reach the log through SDL_Log below)
const auto commandStart = std::chrono::steady_clock::now();
- CommandResult r = commands_.execute(doc_, name, args);
+ // Auto UV unwraps on a worker (seconds on big meshes): the frame never waits for it. A running macro keeps the
+ // synchronous command, as its next steps read the UVs at once.
+ CommandResult r = name == "uv_auto" && macros_.runDepth == 0 ? startAutoUvCommand(args) : commands_.execute(doc_, name, args);
if (perftrace::on()) { // a command that held up the frames (remesh, Divide, micro, open...)
const double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - commandStart).count();
if (ms > perf_.traceLongMs) {
diff --git a/src/app/App.h b/src/app/App.h
index ce5edfe2..42f5afbe 100644
--- a/src/app/App.h
+++ b/src/app/App.h
@@ -72,6 +72,7 @@
#include <set>
#include <string>
#include <unordered_map>
+#include <utility>
#include <vector>
namespace sf::ai {
@@ -1013,9 +1014,21 @@ private:
// After uv_pack / uv_unwrap from anywhere (the panel, a script, the console): the fields show the grid it used.
void uvPackGridUsed(const Args& args);
bool showUvEditor_ = false;
- void startAutoUv();
+ // Auto UV on a worker (xatlas takes seconds on big meshes): the button, New Texture, and the uv_auto command from
+ // the console, an AI tool or a UI test (resolution 0: the texture's size, or the size chosen under Texture Map).
+ void startAutoUv(int resolution = 0, bool keepTexture = true, bool command = false);
+ CommandResult startAutoUvCommand(const Args& args); // runCommand's uv_auto (macros keep the synchronous one)
void startNewTexture(); // New Texture on the top shelf / Color palette: Auto UV first when there are no UVs
void pollAutoUv();
+ // A command that runs on a worker (uv_auto): it returned at once and its result comes later. The console, the AI
+ // link and UI tests wait for it before their next statement, then take its result (once).
+ bool commandRunning() const { return autoUv_ && autoUv_->command; }
+ bool takeCommandResult(CommandResult& out) {
+ if (!commandResult_) return false;
+ out = *std::exchange(commandResult_, std::nullopt);
+ return true;
+ }
+ std::optional<CommandResult> commandResult_;
struct AutoUvJob {
std::future<bool> done;
std::shared_ptr<std::atomic<float>> progress;
@@ -1024,6 +1037,8 @@ private:
std::shared_ptr<std::string> error;
uint32_t part = 0; // Part id it unwraps
size_t triangles = 0; // the mesh must still have this many triangles when it finishes
+ bool keepTexture = true; // a painted texture moves onto the new UVs (uv_auto keep_texture=false: not)
+ bool command = false; // started by the uv_auto command: its result goes to commandResult_
};
std::unique_ptr<AutoUvJob> autoUv_;
bool textureAfterAutoUv_ = false; // the running Auto UV came from New Texture: make the texture after it
diff --git a/src/app/Config.cpp b/src/app/Config.cpp
index f4631743..3d502dd1 100644
--- a/src/app/Config.cpp
+++ b/src/app/Config.cpp
@@ -1306,6 +1306,7 @@ void App::drawHelpConsoleUi() {
}
}
}
+ console_.update(*this);
console_.watchStatus(status_);
if (layout_.consoleOpen) console_.draw(*this, consoleRect());
palette_.draw(*this, paletteArea());
diff --git a/src/app/Console.cpp b/src/app/Console.cpp
index 00025f4c..01ce5b88 100644
--- a/src/app/Console.cpp
+++ b/src/app/Console.cpp
@@ -128,7 +128,18 @@ void Console::echo(const std::string& statement, const CommandResult& result, co
void Console::watchStatus(const std::string& status) {
if (status == lastStatus_) return;
lastStatus_ = status;
- if (!status.empty()) print(Kind::Status, status);
+ if (status.empty()) return;
+ // A job's progress ("Auto UV: 42%") updates its line instead of adding one per percent.
+ const auto progressPrefix = [](const std::string& s) {
+ const size_t sp = s.find_last_of(' ');
+ return !s.empty() && s.back() == '%' && sp != std::string::npos ? s.substr(0, sp) : std::string();
+ };
+ if (const std::string prefix = progressPrefix(status);
+ !prefix.empty() && !lines_.empty() && lines_.back().kind == Kind::Status && progressPrefix(lines_.back().text) == prefix) {
+ lines_.back().text = status;
+ return;
+ }
+ print(Kind::Status, status);
}
void Console::clear() {
@@ -218,15 +229,42 @@ void Console::runLine(App& app, const std::string& raw) {
print(Kind::Error, line + " -> " + err);
return;
}
- for (const auto& st : statements) {
+ runStatements(app, std::move(statements), 0, false);
+}
+
+// Runs statements[from...]; a command that works on a worker (uv_auto) holds the rest until its result is in
+// (update()), as later statements usually depend on it.
+void Console::runStatements(App& app, std::vector<ScriptStatement> statements, size_t from, bool file) {
+ for (size_t i = from; i < statements.size(); ++i) {
+ const ScriptStatement& st = statements[i];
if (!app.commands_.find(st.command)) {
- print(Kind::Error, formatStatement(st.command, st.args) + " -> Unknown command '" + st.command +
- "' (Tab completes, help lists them)");
+ if (file) print(Kind::Error, "Line " + std::to_string(st.line) + ": unknown command '" + st.command + "' - stopped");
+ else
+ print(Kind::Error, formatStatement(st.command, st.args) + " -> Unknown command '" + st.command +
+ "' (Tab completes, help lists them)");
return;
}
app.runCommand(st.command, st.args); // echoes itself
- if (!app.lastCommandOk_) return; // later statements usually depend on this one
+ if (!app.lastCommandOk_) { // later statements usually depend on this one
+ if (file) print(Kind::Error, "Line " + std::to_string(st.line) + " failed - stopped");
+ return;
+ }
+ if (app.commandRunning()) {
+ if (i + 1 < statements.size()) resume_ = Resume{std::move(statements), i + 1, file};
+ return;
+ }
+ }
+}
+
+void Console::update(App& app) {
+ if (!resume_ || app.commandRunning()) return;
+ Resume r = std::move(*resume_);
+ resume_.reset();
+ if (CommandResult done; app.takeCommandResult(done) && !done.ok) {
+ if (r.file) print(Kind::Error, "Line " + std::to_string(r.statements[r.next - 1].line) + " failed - stopped");
+ return;
}
+ runStatements(app, std::move(r.statements), r.next, r.file);
}
void Console::runFile(App& app, const std::string& path) {
@@ -243,17 +281,7 @@ void Console::runFile(App& app, const std::string& path) {
return;
}
print(Kind::Info, "Running " + fsPath(path).filename().string() + " (" + std::to_string(statements.size()) + " statements)");
- for (const auto& st : statements) {
- if (!app.commands_.find(st.command)) {
- print(Kind::Error, "Line " + std::to_string(st.line) + ": unknown command '" + st.command + "' - stopped");
- return;
- }
- app.runCommand(st.command, st.args);
- if (!app.lastCommandOk_) {
- print(Kind::Error, "Line " + std::to_string(st.line) + " failed - stopped");
- return;
- }
- }
+ runStatements(app, std::move(statements), 0, true);
}
void Console::queueScriptFile(std::string path) {
diff --git a/src/app/Console.h b/src/app/Console.h
index 1647fbea..e8414f5a 100644
--- a/src/app/Console.h
+++ b/src/app/Console.h
@@ -8,10 +8,12 @@
#include "app/UiLayout.h"
#include "core/Commands.h"
+#include "core/Script.h"
#include <imgui.h>
#include <mutex>
+#include <optional>
#include <string>
#include <vector>
@@ -41,6 +43,7 @@ public:
void loadHistory(const std::string& path);
void draw(App& app, const CanvasRect& rect);
+ void update(App& app); // every frame: statements held behind a command on a worker go on once it's done
private:
struct Line {
@@ -48,11 +51,18 @@ private:
std::string text;
std::string source;
};
+ void runStatements(App& app, std::vector<ScriptStatement> statements, size_t from, bool file);
void addHistory(const std::string& line);
void complete(App& app, ImGuiInputTextCallbackData* data);
void printHelp(App& app, const std::string& topic);
static int inputCallback(ImGuiInputTextCallbackData* data);
+ struct Resume {
+ std::vector<ScriptStatement> statements;
+ size_t next = 0;
+ bool file = false; // a script file (its errors name the line)
+ };
+ std::optional<Resume> resume_; // the rest of a line or file, waiting on a command running on a worker
std::vector<Line> lines_;
std::vector<std::string> session_;
std::vector<std::string> history_;
diff --git a/src/app/Demo.cpp b/src/app/Demo.cpp
index 21255d9c..ef20df7c 100644
--- a/src/app/Demo.cpp
+++ b/src/app/Demo.cpp
@@ -216,6 +216,12 @@ void Demo::update(float dt) {
mouse_.y, int(buttonDown_));
stride_ = 1;
if (penResting_) penRestNoise(); // (UiTest.cpp)
+ {
+ const auto now = std::chrono::steady_clock::now();
+ if (lastFrame_.time_since_epoch().count() != 0)
+ longestFrameMs_ = std::max(longestFrameMs_, std::chrono::duration<double, std::milli>(now - lastFrame_).count());
+ lastFrame_ = now;
+ }
if (!stepLines_.empty()) { // SF_UI_PROFILE: the frame just drawn belongs to the step that was running
const auto now = std::chrono::steady_clock::now();
if (lastUpdate_.time_since_epoch().count() != 0 && step_ < stepLines_.size())
@@ -227,6 +233,12 @@ void Demo::update(float dt) {
stepTime_ += dt;
return;
}
+ // (a step that finishes here, like a run line, is charged its own time: not the next line's frame)
+ if (step_ < stepLines_.size()) {
+ const auto now = std::chrono::steady_clock::now();
+ lineSeconds_[size_t(stepLines_[step_])] += std::chrono::duration<double>(now - lastUpdate_).count();
+ lastUpdate_ = now;
+ }
++step_;
stepTime_ = 0.0f;
}
diff --git a/src/app/Demo.h b/src/app/Demo.h
index 12df22cf..fee846b3 100644
--- a/src/app/Demo.h
+++ b/src/app/Demo.h
@@ -141,6 +141,9 @@ private:
std::vector<std::string> scriptLines_; // the script's lines, for the report
std::vector<double> lineSeconds_; // real seconds spent on each line
std::chrono::steady_clock::time_point lastUpdate_{};
+ // The longest real time between two frames since it was last read (UI tests: longest_frame_ms; reading resets it).
+ std::chrono::steady_clock::time_point lastFrame_{};
+ mutable double longestFrameMs_ = 0.0;
int stride_ = 1;
ImVec2 mouse_{960, 540};
bool buttonDown_ = false;
diff --git a/src/app/PanelAiLink.cpp b/src/app/PanelAiLink.cpp
index 7db546a9..f908aa8e 100644
--- a/src/app/PanelAiLink.cpp
+++ b/src/app/PanelAiLink.cpp
@@ -188,6 +188,15 @@ struct App::AiLinkState {
std::string reason;
};
std::optional<Pending> confirm;
+ // A call whose command works on a worker (uv_auto: App::commandRunning): its reply, and the next call, wait for it.
+ struct Running {
+ link::AiLinkCall call;
+ uint32_t part = 0;
+ size_t undoCount = 0;
+ uint64_t revision = 0;
+ std::chrono::steady_clock::time_point start;
+ };
+ std::optional<Running> running;
std::string error; // why the listener isn't running
int commands = 0; // tool calls run this connection
@@ -352,6 +361,7 @@ void App::pollAiLink() {
for (auto& e : events) {
if (e.kind == AiLinkState::Event::Kind::Connected) {
s.session = std::make_unique<link::AiLinkSession>(link::makePairingCode());
+ s.running.reset();
s.session->policy.projectDir = macroProjectDir();
for (const std::string& p : aiLinkPickedPaths_) s.session->policy.addPicked(p);
s.waiting.clear();
@@ -366,6 +376,7 @@ void App::pollAiLink() {
s.session.reset();
s.waiting.clear();
s.confirm.reset();
+ s.running.reset();
s.approving = false;
} else if (s.session) {
s.waiting.push_back(std::move(e.line));
@@ -378,8 +389,18 @@ void App::pollAiLink() {
aiLinkDisconnect(tr("AI connection request timed out"));
return;
}
- // One message at a time; none while a dialog waits for the user.
- while (!s.waiting.empty() && !s.confirm && s.session && s.session->state() != link::AiLinkSession::State::Closed) {
+ if (s.running && !commandRunning()) { // its worker is done: the reply goes out now
+ const AiLinkState::Running r = std::move(*s.running);
+ s.running.reset();
+ CommandResult done{lastCommandOk_, status_};
+ takeCommandResult(done);
+ labelAiUndo(r.part, r.undoCount, r.revision);
+ const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - r.start).count();
+ auditLive(s.session->client(), r.call.name, r.call.args, done.ok, done.message, int64_t(ms));
+ s.send(link::AiLinkSession::resultReply(r.call.id, done.ok, done.message));
+ }
+ // One message at a time; none while a dialog waits for the user or a command works on a worker.
+ while (!s.waiting.empty() && !s.confirm && !s.running && s.session && s.session->state() != link::AiLinkSession::State::Closed) {
const bool wasPairing = s.session->state() == link::AiLinkSession::State::Pairing;
const std::string line = std::move(s.waiting.front());
s.waiting.pop_front();
@@ -469,6 +490,10 @@ void App::aiLinkExecute(const link::AiLinkCall& call) {
const size_t before = doc_.undo.undoCount();
const uint64_t revision = doc_.undo.revision();
runCommand(call.name, call.args);
+ if (lastCommandOk_ && commandRunning()) {
+ s.running = AiLinkState::Running{call, part, before, revision, start};
+ return;
+ }
labelAiUndo(part, before, revision);
auditLive(s.session->client(), call.name, call.args, lastCommandOk_, status_, elapsedMs());
s.send(link::AiLinkSession::resultReply(call.id, lastCommandOk_, status_));
diff --git a/src/app/PanelTexture.cpp b/src/app/PanelTexture.cpp
index eec0a331..0e7059c7 100644
--- a/src/app/PanelTexture.cpp
+++ b/src/app/PanelTexture.cpp
@@ -184,17 +184,32 @@ void App::startNewTexture() {
runCommand("texture_create", {{"size", std::to_string(kSizes[std::clamp(textureSizeChoice_, 0, 3)])}});
}
-void App::startAutoUv() {
+CommandResult App::startAutoUvCommand(const Args& a) {
+ if (doc_.mesh.triangleCount() == 0) return {false, "There is no mesh to unwrap"};
+ if (autoUv_) return {false, "Auto UV is already running"};
+ // (resolution as the core command reads it: core/TextureCommands.cpp)
+ const int res = a.contains("resolution") ? int(argNumber(a, "resolution", kDefaultTextureSize))
+ : doc_.mesh.texture ? std::max(doc_.mesh.texture->width, doc_.mesh.texture->height)
+ : kDefaultTextureSize;
+ startAutoUv(res, argBool(a, "keep_texture", true), true);
+ return {true, "Auto UV: unwrapping " + std::to_string(doc_.mesh.triangleCount()) + " triangles in the background"};
+}
+
+void App::startAutoUv(int resolution, bool keepTexture, bool command) {
if (autoUv_ || doc_.mesh.triangleCount() == 0) return;
+ commandResult_.reset();
auto job = std::make_unique<AutoUvJob>();
+ job->keepTexture = keepTexture;
+ job->command = command;
job->progress = std::make_shared<std::atomic<float>>(0.0f);
job->cancel = std::make_shared<std::atomic<bool>>(false);
job->result = std::make_shared<AutoUvResult>();
job->error = std::make_shared<std::string>();
job->part = doc_.activePartInfo().id;
job->triangles = doc_.mesh.triangleCount();
- const int res = doc_.mesh.texture ? std::max(doc_.mesh.texture->width, doc_.mesh.texture->height)
- : kSizes[std::clamp(textureSizeChoice_, 0, 3)];
+ const int res = resolution > 0 ? resolution
+ : doc_.mesh.texture ? std::max(doc_.mesh.texture->width, doc_.mesh.texture->height)
+ : kSizes[std::clamp(textureSizeChoice_, 0, 3)];
std::promise<bool> promise;
job->done = promise.get_future();
std::thread([promise = std::move(promise), positions = doc_.mesh.positions, triangles = doc_.mesh.triangles, res,
@@ -219,15 +234,32 @@ void App::pollAutoUv() {
runCommand(kind == kExportTexture ? "texture_export" : "texture_import", {{"path", path}});
if (!autoUv_) return;
- if (autoUv_->done.wait_for(std::chrono::seconds(0)) != std::future_status::ready) {
+ // Cancel: xatlas only looks at it between its stages (the charts stage is most of the time), so the job is
+ // dropped now; the detached worker owns copies of what it reads and its result goes nowhere.
+ const bool cancelled = autoUv_->cancel->load();
+ if (!cancelled && autoUv_->done.wait_for(std::chrono::seconds(0)) != std::future_status::ready) {
status_ = "Auto UV: " + std::to_string(int(autoUv_->progress->load() * 100.0f)) + "%";
return;
}
if (jobWaitsForStroke()) return; // (the result lands when the stroke ends)
const std::unique_ptr<AutoUvJob> job = std::move(autoUv_);
const bool makeTexture = std::exchange(textureAfterAutoUv_, false);
+ // The uv_auto command's result, for whoever waits on it (console, AI link, UI test) and the console's log.
+ auto finish = [&](bool ok, const std::string& message) {
+ status_ = message;
+ if (!job->command) return;
+ commandResult_ = CommandResult{ok, message};
+ console_.print(ok ? Console::Kind::Result : Console::Kind::Error, message);
+ console_.seenStatus(status_);
+ if (!ok) SDL_Log("uv_auto failed: %s", message.c_str());
+ };
+ if (cancelled) {
+ textureAfterAutoUv_ = false;
+ finish(false, "Auto UV was cancelled");
+ return;
+ }
if (!job->done.get()) {
- status_ = *job->error;
+ finish(false, *job->error);
return;
}
// The Part it unwrapped may be inactive now, or changed topology meanwhile.
@@ -235,17 +267,22 @@ void App::pollAutoUv() {
for (int i = 0; i < doc_.partCount(); ++i)
if (doc_.parts[size_t(i)].id == job->part) index = i;
if (index < 0 || doc_.partMesh(index).triangleCount() != job->triangles) {
- status_ = "Auto UV skipped: the mesh changed while it was being unwrapped";
+ finish(false, "Auto UV skipped: the mesh changed while it was being unwrapped");
return;
}
const JobPart part(*this, index);
Mesh m = doc_.mesh;
// A painted texture and its layers move onto the new UVs, in the same undo step (core/TextureTransfer.h).
- const std::string note = setUvsKeepingTexture(m, std::move(job->result->uvs));
+ const std::string note = setUvsKeepingTexture(m, std::move(job->result->uvs), job->keepTexture);
doc_.replaceMeshUndoable(std::move(m), "Auto UV");
uvLinesKey_ = 0;
+ uvEditorChanged();
updateTitle();
const std::string islands = "Auto UV: " + std::to_string(job->result->charts) + " UV islands";
+ if (job->command) {
+ finish(true, islands + note);
+ return;
+ }
if (makeTexture && !hasTexture(doc_.mesh)) {
runCommand("texture_create", {{"size", std::to_string(kSizes[std::clamp(textureSizeChoice_, 0, 3)])}});
if (hasTexture(doc_.mesh)) status_ = islands + ", new " + std::to_string(doc_.mesh.texture->width / 1024) +
@@ -262,8 +299,10 @@ void App::drawTextureMapContents() {
// Status line: UVs, texture size, where paint goes.
if (autoUv_) {
- ImGui::ProgressBar(autoUv_->progress->load(), ImVec2(-1, 0), "Unwrapping...");
+ ImGui::ProgressBar(autoUv_->progress->load(), ImVec2(half, 0), "Unwrapping...");
ui::HelpText("Auto UV", nullptr, "Unwrapping the Part in the background; you can keep working.", kPath);
+ ImGui::SameLine();
+ if (ImGui::Button(tr("Cancel##autouv"), ImVec2(-1, 0))) autoUv_->cancel->store(true);
} else if (!m.hasUvs()) {
ImGui::TextDisabled("No UVs: Auto UV makes them");
} else if (!hasTexture(m)) {
diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cpp
index 80a269f7..74d47dab 100644
--- a/src/app/UiTest.cpp
+++ b/src/app/UiTest.cpp
@@ -147,6 +147,7 @@
// roughness differs from the default), shown_mean_r (mean red the viewport is sent),
// layer_channel_<id> (1 when the active texture layer has channel <id> on, e.g. roughness)
// auto_uv_running, qremesh_running (QRemesh working in the background),
+// longest_frame_ms (the longest real time between two frames since the last read; resets),
// voxel_remesh_running, quick_remesh / remesh_after_stroke (Tool > Remesh, #121), stroke_stretched,
// qremesh_guides (guide curves on the active Part), qremesh_guide_loops (closed ones),
// guides_mode (1 while the QRemesh Guides brush is picked),
@@ -307,6 +308,7 @@
#include <imgui_internal.h>
#include <algorithm>
+#include <utility>
#include <thread>
#include <mutex>
#include <atomic>
@@ -567,6 +569,7 @@ double Demo::query(const std::string& name, bool& known) const {
if (name == "texture_layers") return hasTexture(m) ? double(m.texture->layers.size()) : 0.0;
if (name == "active_texture_layer") return hasTexture(m) && !m.texture->layers.empty() ? double(m.texture->activeLayer) : -1.0;
if (name == "auto_uv_running") return app_.autoUv_ ? 1.0 : 0.0;
+ if (name == "longest_frame_ms") return std::exchange(longestFrameMs_, 0.0); // since the last read (it resets)
if (const double v = app_.bakeTestValue(name); !std::isnan(v)) return v;
if (const double v = app_.uvEditorTestValue(name); !std::isnan(v)) return v;
if (const double v = app_.pbrTestValue(name); !std::isnan(v)) return v; // material_channels, channel_view, ...
@@ -2118,17 +2121,32 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& e
at += scriptDir_.size();
}
if (!parseScript(text, statements, error)) return false;
- run([this, line, statements] {
- for (const auto& st : statements) {
+ // A command that works on a worker (uv_auto: App::commandRunning) holds the script, frames still drawing,
+ // until its result is in, like the console and an AI tool wait for it.
+ auto next = std::make_shared<size_t>(0);
+ steps_.push_back([this, line, statements, next](float t) {
+ if (*next > 0 && app_.commandRunning()) {
+ SDL_Delay(5); // real work runs in real time
+ if (t > 600.0f) fail(line, statements[*next - 1].command + " still running after 600 s");
+ return t > 600.0f;
+ }
+ if (CommandResult r; *next > 0 && app_.takeCommandResult(r) && !r.ok) {
+ fail(line, statements[*next - 1].command + " failed: " + r.message);
+ return true;
+ }
+ while (*next < statements.size()) {
+ const ScriptStatement& st = statements[(*next)++];
Args args = st.args; // $LIBRARY: the test's own library folder (known once the app runs)
for (auto& [key, value] : args)
if (auto* sv = std::get_if<std::string>(&value)) *sv = expandLibrary(*sv);
app_.runCommand(st.command, args);
if (!app_.lastCommandOk_) {
fail(line, st.command + " failed: " + app_.status_);
- return;
+ return true;
}
+ if (app_.commandRunning()) return false;
}
+ return true;
});
} else if (verb == "run_fails") { // run_fails COMMAND ARGS...: the command must be refused
if (!need(1)) return false;
diff --git a/tests/ui/auto_uv_worker.sfui b/tests/ui/auto_uv_worker.sfui
new file mode 100644
index 00000000..4753857f
--- /dev/null
+++ b/tests/ui/auto_uv_worker.sfui
@@ -0,0 +1,49 @@
+# Auto UV never holds up a frame: xatlas takes seconds on a big sculpt (10 s at 327k triangles on the PC when the
+# uv_auto command ran on the main thread: the stall slowtests saw "after texture_create"). The command from a
+# script, the console or an AI tool works on the same worker as the Auto UV button: the Texture Map panel shows its
+# progress with Cancel, frames keep drawing, and the next statement waits for the UVs.
+run new_sphere subdivisions=6
+expect_value triangles > 80000
+click "Texture Map##tool.texture"
+expect_value uvs == 0
+
+# The button: progress and Cancel while it works. Cancel takes effect at once (xatlas itself only stops between its
+# stages: the job is dropped) and leaves the Part without UVs.
+click "Texture Map/Auto UV"
+expect_value auto_uv_running == 1
+expect "Texture Map/Cancel##autouv"
+shot auto-uv-progress
+click "Texture Map/Cancel##autouv"
+wait_value auto_uv_running == 0 1
+expect_value uvs == 0
+expect_status "cancelled"
+
+# The command (a script line): frames keep drawing while it unwraps, and texture_create after it finds the UVs.
+run new_sphere subdivisions=5
+wait 0.3
+remember longest_frame_ms ignored
+run uv_auto resolution=1024
+expect_value uvs == 1
+expect_status "UV islands"
+run texture_create size=1024
+wait 0.3
+expect_value longest_frame_ms < 100
+expect_value texture_size == 1024
+
+# The console: the line's next statement waits for the worker too.
+run new_sphere subdivisions=5
+key backtick
+expect_value console_open == 1
+click "Console/Clear console"
+wait 0.3
+remember longest_frame_ms ignored2
+type Console/##console "uv_auto resolution=1024; texture_create size=1024"
+key enter
+expect_value auto_uv_running == 1
+wait_value auto_uv_running == 0 120
+wait_value texture_size == 1024 10
+expect_value longest_frame_ms < 100
+expect_text "UV islands"
+expect_text "New 1K texture"
+wait 0.2
+shot console