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.

Per-brush settings (#78): each brush keeps its own size, intensity, falloff, RGB intensity, stroke and alpha

islandmed6f43a9ac65b6
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f29c9447..ba2f40e8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,10 +27,17 @@ All notable changes to SculptForge are listed here. The format follows
Insiders repository and posts to #insiders (`.github/workflows/insider-preview.yml`).
### Fixed
+- UI test runs remove their temporary settings folder when they pass (thousands of runs had filled `/tmp`).
- Custom and Community brushes (Clay Slab and other presets) show the whole alpha at Focal Shift -100, as the
built-in brushes do since #60: the stamp reaches its square's corners instead of being cut by the preset's round
tip (#75, reported by rodrigomoretto). At the default Focal Shift presets look as before; saved presets are unchanged.
### Added
+- Each brush keeps its own settings (#78, requested by dCo3lh0 and on Discord): Draw Size, Z Intensity, Falloff, RGB
+ Intensity, the stroke (with Spray's scatter and Lazy Stroke's radius) and the alpha follow the brush, custom and
+ Community brushes included. Switching back brings back the values that brush had this session; a brush not used yet
+ starts from the current values. **Brush > Save Brush Settings** keeps them for every start (`brush-settings.json`
+ in the user data folder), **Brush > Reset Brush to Default** restores the shipped values, and **Preferences >
+ Input > Share size and intensity across brushes** (off by default) keeps Draw Size and Z Intensity shared as before.
- A new default look: new installs start in **Forge Terminal** with the **Tokyo Night** colours and the
**SculptForge layout** (menus by task: File, Edit, Sculpt, Paint, Mesh, View, Render, Help, whose sections are the
palettes; a status bar; a one-row tool shelf). Existing installs keep their look and layout, and see a one-time card,
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a54b9989..aff42cc5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -58,6 +58,7 @@ add_library(sculptforge_core STATIC
src/core/Pack.cpp
src/core/Json.cpp
src/core/BrushRecipe.cpp
+ src/core/BrushSettings.cpp
src/core/BrushLibrary.cpp
src/core/Image.cpp
src/core/ImageTo3D.cpp
@@ -291,6 +292,7 @@ if(SCULPTFORGE_BUILD_APP)
src/app/PanelSelectionSets.cpp
src/app/Selection.cpp
src/app/PanelBrushes.cpp
+ src/app/BrushSettingsUi.cpp
src/app/PanelVdm.cpp
src/app/PanelKelvinlet.cpp
src/app/PanelFace.cpp
diff --git a/docs/guide/sculpting.md b/docs/guide/sculpting.md
index 70824b17..ed66d3ad 100644
--- a/docs/guide/sculpting.md
+++ b/docs/guide/sculpting.md
@@ -157,6 +157,21 @@ With symmetry on, a fainter ring shows where the mirrored brush lands. Off the m
The <kbd>S</kbd>, <kbd>U</kbd>, <kbd>I</kbd> and <kbd>O</kbd> pop-ups take the mouse or the keyboard: drag the
slider and release to close it, or type a number and press <kbd>Enter</kbd> (a minus first for a negative Falloff). <kbd>Esc</kbd> closes the pop-up without typing anything new.
+### Each brush keeps its own settings
+
+Every brush remembers its own **Draw Size**, **Intensity**, **Falloff**, **RGB Intensity**, [stroke](#strokes)
+and [alpha](#alphas). That includes custom and Community brushes. Set up a small, sharp crease brush and a big
+Clay brush, and each one comes back the way you left it when you switch between them. A brush you haven't used
+yet in the session starts from the values you have now, so nothing jumps.
+
+- **Brush > Save Brush Settings** keeps the current brush's values for every new session (in
+ `brush-settings.json`, in SculptForge's user data folder).
+- **Brush > Reset Brush to Default** puts the current brush back to the values SculptForge ships with and
+ forgets what you saved for it.
+- **Preferences > Input > Share size and intensity across brushes** (off by default) keeps Draw Size and
+ Intensity the same for every brush, as before 0.10.7. Falloff, RGB Intensity, the stroke and the alpha still
+ follow each brush.
+
### Smooth's own Intensity
Smooth keeps an Intensity of its own, as in ZBrush, so a gentle Standard stroke at 10 still smooths at full
diff --git a/docs/guide/whats-new.md b/docs/guide/whats-new.md
index 83a50cb8..f9515e2d 100644
--- a/docs/guide/whats-new.md
+++ b/docs/guide/whats-new.md
@@ -7,6 +7,10 @@ chapter opens by itself the first time you start a new version; you can always f
## Version 0.10.7
+- **Each brush keeps its own settings**: Draw Size, Intensity, Falloff, RGB Intensity, stroke and alpha follow
+ the brush, custom and Community brushes included. **Brush > Save Brush Settings** keeps them for the next start,
+ **Reset Brush to Default** puts back the shipped values, and **Preferences > Input > Share size and intensity
+ across brushes** keeps the old way (#78). See [Sculpting](sculpting.md#each-brush-keeps-its-own-settings).
- **A clearer membership card**: the three memberships side by side with what each one gets you, in your look's
colours. **Maybe later** now hides it for 30 days.
- **New memberships**: Supporters get the credits (in the app and on the website), the monthly pack and a vote;
diff --git a/src/app/App.cpp b/src/app/App.cpp
index 08c6194c..afc385b5 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -308,6 +308,7 @@ bool App::init(const AppOptions& options) {
initBrushLibrary(); // before a project can bring its own brushes
initProject(); // before opening anything, so it lands in the recent files
initActions(); // the default shortcuts and every command, for keys, help and the command palette
+ initBrushSettings(); // each brush's own size, intensity, stroke and alpha (#78)
initPhoneActions(); // File > Capture from phone (PanelPhone.cpp)
initLightBox();
runCommand("new_sphere");
@@ -2279,6 +2280,7 @@ void App::drawUi() {
drawUvEditor(); // Tool > Texture Map > UV Editor (PanelUvEditor.cpp)
drawTimelineUi(); // the Sculpt Timeline strip, timelapse, proof mode HUD (PanelTimeline.cpp, ProofMode.cpp)
pollAlphaFile();
+ syncBrushSettings(); // a brush switch brings back that brush's own settings (#78); before pollVdm's stroke
pollVdm();
if (previewLod_ >= 0) {
const CanvasRect c = canvasRect();
diff --git a/src/app/App.h b/src/app/App.h
index 34fbdfa2..074bbdbc 100644
--- a/src/app/App.h
+++ b/src/app/App.h
@@ -19,6 +19,7 @@
#include "app/UserGuide.h"
#include "ai/ComfyUI.h"
#include "ai/ImageTo3DService.h"
+#include "core/BrushSettings.h"
#include "core/Commands.h"
#include "core/Image.h"
#include "core/Macro.h"
@@ -1532,6 +1533,26 @@ private:
bool strokeAltSmooth_ = false;
float rgbIntensity_ = 1.0f; // paint strength (ZBrush RGB Intensity, 100 by default)
float focalShift_ = 0.0f; // -1..1 (ZBrush Focal Shift -100..100)
+ // Per-brush settings (#78, BrushSettingsUi.cpp, core/BrushSettings.h): each frame, a brush switch stores the
+ // brush left and brings back the picked brush's own Draw Size, Z Intensity, Falloff, RGB Intensity, stroke and
+ // alpha. Save Brush Settings writes them (<app data>/brush-settings.json) so they load at startup; Reset Brush
+ // to Default puts back the shipped values. Off in demo videos and benchmarks (their scripts set every value).
+public:
+ std::string currentBrushKey() const; // "builtin:Clay", "custom:Clay Slab"
+ BrushToolSettings liveBrushSettings() const;
+ void applyBrushSettings(const BrushToolSettings& s);
+ void saveBrushSettings(); // Brush > Save Brush Settings
+ void resetBrushSettings(); // Brush > Reset Brush to Default
+ void setShareBrushSize(bool on);
+ const BrushSettingsStore& brushSettingsStore() const { return brushSettings_; }
+
+private:
+ void initBrushSettings();
+ void syncBrushSettings(); // every frame, outside strokes
+ std::string brushSettingsPath() const;
+ BrushSettingsStore brushSettings_;
+ std::string brushSettingsKey_; // the brush the live values belong to
+ bool brushSettingsOn_ = true;
struct QuickMenu {
bool open = false;
ImVec2 at{0, 0};
diff --git a/src/app/BrushSettingsUi.cpp b/src/app/BrushSettingsUi.cpp
new file mode 100644
index 00000000..ebe42205
--- /dev/null
+++ b/src/app/BrushSettingsUi.cpp
@@ -0,0 +1,128 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Per-brush settings (#78): the app side of core/BrushSettings.h. A brush switch is noticed once per frame, outside
+// strokes, so every way of picking a brush (shelf, pickers, keys, the Library, scripts, Ctrl-held brushes) behaves
+// the same without each of them knowing about it.
+#include "app/App.h"
+
+#include "core/BrushLibrary.h"
+#include "core/Image.h"
+
+#include <algorithm>
+#include <cctype>
+#include <filesystem>
+
+namespace sf {
+
+namespace {
+
+std::string lower(std::string s) {
+ for (char& c : s) c = char(std::tolower(static_cast<unsigned char>(c)));
+ return s;
+}
+
+} // namespace
+
+std::string App::brushSettingsPath() const {
+ const std::string dir = appDataDir();
+ return dir.empty() ? std::string() : dir + "/brush-settings.json";
+}
+
+std::string App::currentBrushKey() const {
+ const bool custom = doc_.activeBrush >= 0 && doc_.activeBrush < int(doc_.brushes.size());
+ return brushSettingsKey(brushName(brush_), custom ? doc_.brushes[size_t(doc_.activeBrush)].name : std::string());
+}
+
+BrushToolSettings App::liveBrushSettings() const {
+ BrushToolSettings s;
+ s.radiusPx = radiusPx_;
+ s.strength = strength_;
+ s.focal = focalShift_;
+ s.rgbIntensity = rgbIntensity_;
+ s.stroke = strokeType_;
+ s.sprayScatter = sprayScatter_;
+ s.lazyRadius = lazyMouse_.radius;
+ const BrushAlpha& a = doc_.brushAlpha;
+ s.alphaName = a.empty() ? std::string("Round") : a.name;
+ s.alphaPath = a.empty() ? std::string() : a.path;
+ s.alphaInvert = !s.alphaPath.empty() && a.inverted;
+ return s;
+}
+
+void App::applyBrushSettings(const BrushToolSettings& in) {
+ BrushToolSettings s = in;
+ s.clamp();
+ radiusPx_ = s.radiusPx;
+ strength_ = s.strength;
+ focalShift_ = s.focal;
+ rgbIntensity_ = s.rgbIntensity;
+ strokeType_ = s.stroke;
+ sprayScatter_ = s.sprayScatter;
+ lazyMouse_.radius = s.lazyRadius;
+ // The alpha, loaded only when it differs; one that can't be loaded any more (a moved image) leaves the current.
+ const BrushToolSettings live = liveBrushSettings();
+ if (lower(live.alphaName) == lower(s.alphaName) && live.alphaPath == s.alphaPath && live.alphaInvert == s.alphaInvert)
+ return;
+ if (s.alphaPath.empty()) {
+ BrushAlpha a;
+ if (BrushAlpha::builtin(s.alphaName, a)) doc_.brushAlpha = std::move(a);
+ return;
+ }
+ Image img;
+ std::string err;
+ if (!Image::load(s.alphaPath, img, err)) return;
+ BrushAlpha a = BrushAlpha::fromImage(img, s.alphaName, s.alphaInvert);
+ a.path = s.alphaPath;
+ a.inverted = s.alphaInvert;
+ doc_.brushAlpha = std::move(a);
+}
+
+void App::initBrushSettings() {
+ const bool recording = !options_.recordPath.empty() && options_.uiTest.empty(); // demo videos
+ brushSettingsOn_ = !recording && !options_.benchmark && options_.timelapsePath.empty() &&
+ options_.renderPath.empty() && options_.measureIdle <= 0.0f && options_.measureStroke <= 0.0f;
+ brushSettingsKey_ = currentBrushKey();
+ if (!brushSettingsOn_) return;
+ std::string err;
+ if (const std::string path = brushSettingsPath(); !path.empty() && !brushSettings_.load(path, err))
+ SDL_Log("Brush settings: %s", err.c_str());
+ if (BrushToolSettings s; brushSettings_.savedFor(brushSettingsKey_, s)) applyBrushSettings(s);
+}
+
+void App::syncBrushSettings() {
+ if (!brushSettingsOn_ || drag_ != Drag::None || doc_.sculptor.strokeActive()) return;
+ const std::string key = currentBrushKey();
+ if (key == brushSettingsKey_) return;
+ applyBrushSettings(brushSettings_.switchBrush(brushSettingsKey_, key, liveBrushSettings()));
+ brushSettingsKey_ = key;
+}
+
+void App::saveBrushSettings() {
+ syncBrushSettings(); // (the live values belong to the brush picked now)
+ brushSettings_.store(brushSettingsKey_, liveBrushSettings());
+ std::string err;
+ const std::string path = brushSettingsPath();
+ if (!brushSettingsOn_ || path.empty()) status_ = "Brush settings kept for this session";
+ else if (!brushSettings_.save(path, err)) status_ = err;
+ else status_ = "Saved this brush's settings: it starts with them from now on";
+}
+
+void App::resetBrushSettings() {
+ syncBrushSettings();
+ const bool wasSaved = brushSettings_.hasSaved(brushSettingsKey_);
+ applyBrushSettings(brushSettings_.reset(brushSettingsKey_));
+ std::string err;
+ if (wasSaved && brushSettingsOn_ && !brushSettingsPath().empty() && !brushSettings_.save(brushSettingsPath(), err))
+ status_ = err;
+ else
+ status_ = "Brush reset to its default settings";
+}
+
+void App::setShareBrushSize(bool on) {
+ brushSettings_.shareSizeIntensity = on;
+ std::string err;
+ if (brushSettingsOn_ && !brushSettingsPath().empty() && !brushSettings_.save(brushSettingsPath(), err)) status_ = err;
+}
+
+} // namespace sf
diff --git a/src/app/Config.cpp b/src/app/Config.cpp
index 7174c9cf..93ecfd8e 100644
--- a/src/app/Config.cpp
+++ b/src/app/Config.cpp
@@ -485,6 +485,20 @@ void App::initActions() {
"SculptForge reads and writes only there, and only when you send or get.");
add("brush.paste", "Paste brush", "Brush", "Ctrl+V", [this] { pasteBrush(); }, ICON_LC_CLIPBOARD_PASTE, nullptr, nullptr,
nullptr, "Add the brush on the clipboard: a brush code someone shared, or the text of a .sfbrush file.");
+ // Per-brush settings (#78, BrushSettingsUi.cpp).
+ add("brush.save_settings", "Save Brush Settings", "Brush", nullptr, [this] { saveBrushSettings(); }, ICON_LC_SAVE,
+ nullptr, nullptr, nullptr,
+ "Every brush keeps its own Draw Size, Z Intensity, Falloff, RGB Intensity, stroke and alpha while you work. "
+ "This saves the current brush's, so it starts with them every time SculptForge opens.");
+ add("brush.reset_settings", "Reset Brush to Default", "Brush", nullptr, [this] { resetBrushSettings(); },
+ ICON_LC_ROTATE_CCW, nullptr, nullptr, nullptr,
+ "Put the current brush's Draw Size, Z Intensity, Falloff, RGB Intensity, stroke and alpha back to the values "
+ "SculptForge ships with, and forget what Save Brush Settings stored for it.");
+ add("brush.share_size", "Share size and intensity across brushes", "Preferences > Input", nullptr,
+ [this] { setShareBrushSize(!brushSettings_.shareSizeIntensity); }, nullptr, nullptr,
+ [this] { return brushSettings_.shareSizeIntensity; }, nullptr,
+ "On: Draw Size and Z Intensity stay as they are when you pick another brush (as before 0.10.7); Falloff, "
+ "RGB Intensity, the stroke and the alpha still follow each brush. Off (the default): every brush keeps its own.");
add("brush.size_down", "Smaller brush", "Draw", "[", [this] { radiusPx_ = std::max(2.0f, radiusPx_ / 1.1f); }, nullptr,
nullptr, nullptr, nullptr, "Make the brush 10% smaller (hold to repeat).");
add("brush.size_up", "Larger brush", "Draw", "]", [this] { radiusPx_ = std::min(800.0f, radiusPx_ * 1.1f); }, nullptr,
diff --git a/src/app/Input.cpp b/src/app/Input.cpp
index 2ab4aec6..64a79801 100644
--- a/src/app/Input.cpp
+++ b/src/app/Input.cpp
@@ -474,6 +474,9 @@ void App::drawInputSection() {
bool airStrokes = uiPrefs_.airStrokes;
if (ui::Checkbox("Start strokes off the model##input.air_strokes", &airStrokes)) uiPrefs_.airStrokes = airStrokes;
ui::HelpAction(*this, "input.air_strokes");
+ bool shareSize = brushSettingsStore().shareSizeIntensity;
+ if (ui::Checkbox("Share size and intensity across brushes##brush.share_size", &shareSize)) setShareBrushSize(shareSize);
+ ui::HelpAction(*this, "brush.share_size");
bool surfaceCursor = uiPrefs_.cursorFollowsSurface;
if (ui::Checkbox("Cursor follows surface##input.surface_cursor", &surfaceCursor))
uiPrefs_.cursorFollowsSurface = surfaceCursor;
diff --git a/src/app/Palettes.cpp b/src/app/Palettes.cpp
index 36025693..dbc41cdc 100644
--- a/src/app/Palettes.cpp
+++ b/src/app/Palettes.cpp
@@ -302,6 +302,7 @@ void App::drawPalette_Brush() {
// then every brush as thumbnails (Pickers.cpp, the same grid as the B picker).
actionRow(*this, {"ui.brush_lab", "ui.brush_gallery"});
actionRow(*this, {"brush.paste"});
+ actionRow(*this, {"brush.save_settings", "brush.reset_settings"}); // this brush's own settings (#78)
drawCustomBrushes();
ImGui::Checkbox(tr("Front faces only"), &frontFacesOnly_);
ui::HelpAction(*this, "draw.front_faces");
diff --git a/src/app/PanelProject.cpp b/src/app/PanelProject.cpp
index d3df4918..d1c18704 100644
--- a/src/app/PanelProject.cpp
+++ b/src/app/PanelProject.cpp
@@ -468,6 +468,7 @@ void App::applyProjectSettings() {
doc_.sculptor.dyntopo.smoothRemesh = std::clamp(doc_.sculptor.dyntopo.smoothRemesh, 0.0f, 1.0f);
if (const int vdm = b["vdm"].isString() ? findVdm(doc_, b["vdm"].asString()) : -1; vdm >= 0)
doc_.brushVdm = doc_.vdms[size_t(vdm)];
+ brushSettingsKey_ = currentBrushKey(); // the project's values belong to its brush (#78), nothing to swap
}
if (const Json& sym = s["symmetry"]; sym.isObject()) {
readBool(sym, "x", symmetry_.x);
diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cpp
index fcef8be5..6a394acc 100644
--- a/src/app/UiTest.cpp
+++ b/src/app/UiTest.cpp
@@ -459,6 +459,8 @@ double Demo::query(const std::string& name, bool& known) const {
if (name == "rgb_intensity") return std::round(app_.rgbIntensity_ * 100.0f);
if (name == "focal_shift") return std::round(app_.focalShift_ * 100.0f);
if (name == "draw_size") return std::round(app_.radiusPx_);
+ if (name == "brush_settings_saved") return double(app_.brushSettingsStore().savedCount()); // (#78)
+ if (name == "share_brush_size") return app_.brushSettingsStore().shareSizeIntensity ? 1.0 : 0.0;
if (name == "quickmenu_open") return app_.quickMenu_.open ? 1.0 : 0.0;
if (name == "right_click_menu") return app_.uiPrefs_.rightClickQuickMenu ? 1.0 : 0.0;
if (name == "space_camera") return app_.uiPrefs_.spaceCamera ? 1.0 : 0.0;
diff --git a/src/app/main.cpp b/src/app/main.cpp
index bf7657ee..649d7f72 100644
--- a/src/app/main.cpp
+++ b/src/app/main.cpp
@@ -76,13 +76,18 @@ void usage() {
" --crash-test KIND Crash on purpose (segv, abort or throw) to test the crash note in the log");
}
+// The folder isolateSettings made (removed when the run succeeds, kept with its log when it fails).
+std::filesystem::path g_isolatedDir;
+
// UI tests must never read or change the user's real settings, recent files or
// autosaves: point the per-user data directories at a fresh temporary folder.
void isolateSettings() {
+ if (!g_isolatedDir.empty()) return;
std::error_code ec;
const auto dir = std::filesystem::temp_directory_path(ec) /
("sculptforge-ui-test-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
std::filesystem::create_directories(dir, ec);
+ g_isolatedDir = dir;
const std::string d = dir.string();
#if defined(_WIN32)
_putenv_s("APPDATA", d.c_str());
@@ -213,5 +218,9 @@ int main(int argc, char** argv) {
int code = app.run();
app.shutdown();
sf::crashlog::stop();
+ if (code == 0 && !g_isolatedDir.empty()) { // UI tests ran thousands of times a day filled /tmp
+ std::error_code ec;
+ std::filesystem::remove_all(g_isolatedDir, ec);
+ }
return code;
}
diff --git a/src/core/BrushAlpha.cpp b/src/core/BrushAlpha.cpp
index dcce7aa8..b6594154 100644
--- a/src/core/BrushAlpha.cpp
+++ b/src/core/BrushAlpha.cpp
@@ -398,6 +398,8 @@ void registerBrushCommands(CommandRegistry& registry) {
auto stem = std::filesystem::path(std::u8string(path.begin(), path.end())).stem().u8string();
doc.brushAlpha = BrushAlpha::fromImage(img, std::string(stem.begin(), stem.end()),
argBool(a, "invert", false));
+ doc.brushAlpha.path = path;
+ doc.brushAlpha.inverted = argBool(a, "invert", false);
return {true, "Alpha '" + doc.brushAlpha.name + "' (" + std::to_string(doc.brushAlpha.width) +
"x" + std::to_string(doc.brushAlpha.height) + ")"};
}
diff --git a/src/core/BrushAlpha.h b/src/core/BrushAlpha.h
index d39497d0..faa08abe 100644
--- a/src/core/BrushAlpha.h
+++ b/src/core/BrushAlpha.h
@@ -22,6 +22,10 @@ struct BrushAlpha {
std::string name = "Round";
int width = 0, height = 0;
std::vector<float> values; // 0..1, row-major, row 0 at the top
+ // An image alpha's file and whether it was inverted (brush_set_alpha path=...), so a brush's settings can
+ // bring it back (core/BrushSettings.h). Empty for the built-ins.
+ std::string path;
+ bool inverted = false;
bool empty() const { return values.empty(); }
// u, v in brush-local -1..1 (v points up); 0 outside the stamp.
diff --git a/src/core/BrushSettings.cpp b/src/core/BrushSettings.cpp
new file mode 100644
index 00000000..beb588f5
--- /dev/null
+++ b/src/core/BrushSettings.cpp
@@ -0,0 +1,165 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "core/BrushSettings.h"
+
+#include <algorithm>
+#include <filesystem>
+#include <fstream>
+#include <iterator>
+
+namespace sf {
+
+namespace {
+
+std::filesystem::path utf8Path(const std::string& s) { return std::filesystem::path(std::u8string(s.begin(), s.end())); }
+
+// Stroke types by a stable id (the labels may change).
+const char* const kStrokeIds[] = {"dots", "drag_rect", "drag_dot", "spray", "lazy_mouse"};
+static_assert(std::size(kStrokeIds) == size_t(StrokeType::Count));
+
+void readFloat(const Json& j, const char* key, float& v) {
+ if (j[key].isNumber()) v = float(j[key].asNumber());
+}
+
+} // namespace
+
+void BrushToolSettings::clamp() {
+ radiusPx = std::clamp(radiusPx, 2.0f, 800.0f);
+ strength = std::clamp(strength, 0.0f, 1.0f);
+ focal = std::clamp(focal, -1.0f, 1.0f);
+ rgbIntensity = std::clamp(rgbIntensity, 0.0f, 1.0f);
+ sprayScatter = std::clamp(sprayScatter, 0.0f, 4.0f);
+ lazyRadius = std::clamp(lazyRadius, 0.0f, 400.0f);
+ if (int(stroke) < 0 || stroke >= StrokeType::Count) stroke = StrokeType::Dots;
+}
+
+Json BrushToolSettings::toJson() const {
+ Json j = Json::object();
+ j.set("draw_size", double(radiusPx));
+ j.set("z_intensity", double(strength));
+ j.set("focal_shift", double(focal));
+ j.set("rgb_intensity", double(rgbIntensity));
+ j.set("stroke", kStrokeIds[int(stroke)]);
+ j.set("spray_scatter", double(sprayScatter));
+ j.set("lazy_radius", double(lazyRadius));
+ j.set("alpha", alphaName);
+ if (!alphaPath.empty()) j.set("alpha_path", alphaPath);
+ if (alphaInvert) j.set("alpha_invert", true);
+ return j;
+}
+
+void BrushToolSettings::applyJson(const Json& j) {
+ if (!j.isObject()) return;
+ readFloat(j, "draw_size", radiusPx);
+ readFloat(j, "z_intensity", strength);
+ readFloat(j, "focal_shift", focal);
+ readFloat(j, "rgb_intensity", rgbIntensity);
+ readFloat(j, "spray_scatter", sprayScatter);
+ readFloat(j, "lazy_radius", lazyRadius);
+ for (int i = 0; i < int(StrokeType::Count); ++i)
+ if (j["stroke"].asString() == kStrokeIds[i]) stroke = StrokeType(i);
+ if (j["alpha"].isString()) {
+ alphaName = j["alpha"].asString();
+ alphaPath = j["alpha_path"].isString() ? j["alpha_path"].asString() : std::string();
+ alphaInvert = j["alpha_invert"].asBool(false);
+ }
+ clamp();
+}
+
+std::string brushSettingsKey(const std::string& builtinName, const std::string& customName) {
+ return customName.empty() ? "builtin:" + builtinName : "custom:" + customName;
+}
+
+BrushToolSettings BrushSettingsStore::switchBrush(const std::string& from, const std::string& to,
+ const BrushToolSettings& current) {
+ if (!from.empty()) session_[from] = current;
+ BrushToolSettings next = current; // a brush not used yet: the current values, nothing jumps
+ if (auto it = session_.find(to); it != session_.end()) next = it->second;
+ else if (auto s = saved_.find(to); s != saved_.end()) next = s->second;
+ if (shareSizeIntensity) {
+ next.radiusPx = current.radiusPx;
+ next.strength = current.strength;
+ }
+ return next;
+}
+
+bool BrushSettingsStore::savedFor(const std::string& key, BrushToolSettings& out) const {
+ const auto it = saved_.find(key);
+ if (it == saved_.end()) return false;
+ out = it->second;
+ return true;
+}
+
+void BrushSettingsStore::store(const std::string& key, const BrushToolSettings& s) {
+ saved_[key] = s;
+ session_[key] = s;
+}
+
+BrushToolSettings BrushSettingsStore::reset(const std::string& key) {
+ saved_.erase(key);
+ session_.erase(key);
+ return shipped(key);
+}
+
+BrushToolSettings BrushSettingsStore::shipped(const std::string& key) {
+ BrushToolSettings s;
+ if (key == "builtin:Vdm") s.stroke = StrokeType::DragRect; // stamps a VDM where you drag (PanelVdm.cpp)
+ return s;
+}
+
+Json BrushSettingsStore::toJson() const {
+ Json brushes = Json::object();
+ for (const auto& [key, s] : saved_) brushes.set(key, s.toJson());
+ Json j = Json::object();
+ j.set("format", "sculptforge-brush-settings");
+ j.set("version", 1);
+ j.set("share_size_intensity", shareSizeIntensity);
+ j.set("brushes", std::move(brushes));
+ return j;
+}
+
+void BrushSettingsStore::applyJson(const Json& j) {
+ shareSizeIntensity = j["share_size_intensity"].asBool(shareSizeIntensity);
+ const Json& brushes = j["brushes"];
+ if (!brushes.isObject()) return;
+ for (const auto& [key, value] : brushes.members()) {
+ if (!value.isObject()) continue;
+ BrushToolSettings s;
+ s.applyJson(value);
+ saved_[key] = s;
+ }
+}
+
+bool BrushSettingsStore::save(const std::string& path, std::string& error) const {
+ std::error_code ec;
+ std::filesystem::create_directories(utf8Path(path).parent_path(), ec);
+ const std::string tmp = path + ".tmp";
+ {
+ std::ofstream f(utf8Path(tmp), std::ios::binary);
+ if (!(f << toJson().dump(true) << "\n")) {
+ error = "Cannot write " + path;
+ return false;
+ }
+ }
+ std::filesystem::rename(utf8Path(tmp), utf8Path(path), ec);
+ if (ec) {
+ error = "Cannot write " + path + ": " + ec.message();
+ return false;
+ }
+ return true;
+}
+
+bool BrushSettingsStore::load(const std::string& path, std::string& error) {
+ std::ifstream f(utf8Path(path), std::ios::binary);
+ if (!f) return true; // nothing saved yet
+ const std::string text((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
+ Json j;
+ if (!Json::parse(text, j, error)) {
+ error = path + ": " + error;
+ return false;
+ }
+ applyJson(j);
+ return true;
+}
+
+} // namespace sf
diff --git a/src/core/BrushSettings.h b/src/core/BrushSettings.h
new file mode 100644
index 00000000..2203d800
--- /dev/null
+++ b/src/core/BrushSettings.h
@@ -0,0 +1,67 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Per-brush settings (#78): each brush keeps its own Draw Size, Z Intensity, Focal Shift, RGB Intensity, stroke and
+// alpha. Picking another brush remembers the one left and brings back what the picked one had this session (or what
+// Save Brush Settings stored for it, <app data>/brush-settings.json); a brush not used yet takes the current values,
+// so nothing jumps the first time. Reset Brush to Default puts back the shipped values.
+#pragma once
+
+#include "core/Json.h"
+#include "core/Stroke.h"
+
+#include <map>
+#include <string>
+
+namespace sf {
+
+struct BrushToolSettings {
+ float radiusPx = 60.0f; // Draw Size (pixels)
+ float strength = 0.5f; // Z Intensity, 0..1
+ float focal = 0.0f; // Focal Shift, -1..1
+ float rgbIntensity = 1.0f; // paint strength, 0..1
+ StrokeType stroke = StrokeType::Dots;
+ float sprayScatter = 1.0f; // Spray's scatter (fraction of the brush size)
+ float lazyRadius = 40.0f; // LazyMouse's string (pixels)
+ // The alpha: a built-in by name (alphaPath empty), or an image file (the library's, a pack's, a loaded one).
+ std::string alphaName = "Round";
+ std::string alphaPath;
+ bool alphaInvert = false;
+
+ bool operator==(const BrushToolSettings&) const = default;
+ void clamp();
+ Json toJson() const;
+ void applyJson(const Json& j); // lenient: missing keys keep their values, values are clamped
+};
+
+// The key a brush's settings are kept under: "builtin:<brushName>" or "custom:<recipe name>".
+std::string brushSettingsKey(const std::string& builtinName, const std::string& customName = "");
+
+class BrushSettingsStore {
+public:
+ // Preferences > Brushes > Share size and intensity across brushes: Draw Size and Z Intensity stay as they are
+ // when switching (the behaviour before 0.10.7); the rest still follows the brush.
+ bool shareSizeIntensity = false;
+
+ // A switch from `from` (whose live values are `current`) to `to`: what `to` uses now.
+ BrushToolSettings switchBrush(const std::string& from, const std::string& to, const BrushToolSettings& current);
+ // What `key` uses at startup (its saved settings) when it has any.
+ bool savedFor(const std::string& key, BrushToolSettings& out) const;
+ // Save Brush Settings: `key` starts with `s` from now on (call save(path) to write the file).
+ void store(const std::string& key, const BrushToolSettings& s);
+ // Reset Brush to Default: forgets `key`'s session and saved values; returns the shipped ones.
+ BrushToolSettings reset(const std::string& key);
+ static BrushToolSettings shipped(const std::string& key); // the Vector Displacement brush stamps with DragRect
+ bool hasSaved(const std::string& key) const { return saved_.count(key) != 0; }
+ size_t savedCount() const { return saved_.size(); }
+
+ Json toJson() const; // the saved settings and shareSizeIntensity (the session's values are not written)
+ void applyJson(const Json& j);
+ bool save(const std::string& path, std::string& error) const;
+ bool load(const std::string& path, std::string& error); // a missing file is no error (nothing saved yet)
+
+private:
+ std::map<std::string, BrushToolSettings> session_, saved_;
+};
+
+} // namespace sf
diff --git a/tests/test_brush_settings.cpp b/tests/test_brush_settings.cpp
new file mode 100644
index 00000000..709097a7
--- /dev/null
+++ b/tests/test_brush_settings.cpp
@@ -0,0 +1,104 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// #78: per-brush settings (core/BrushSettings.h). Each brush keeps its own size, intensity, falloff, stroke and alpha
+// in the session; Save Brush Settings writes them for the next start; Reset Brush to Default gives the shipped values.
+#include "TestHarness.h"
+
+#include "core/BrushSettings.h"
+
+#include <cstdio>
+#include <filesystem>
+
+using namespace sf;
+
+TEST(brush_settings_switch_keeps_each_brush) {
+ BrushSettingsStore store;
+ const std::string standard = brushSettingsKey("Standard"), clay = brushSettingsKey("Clay");
+ BrushToolSettings live;
+ live.radiusPx = 120.0f;
+ live.strength = 0.8f;
+ live.alphaName = "Pores";
+ // Clay hasn't been used: it starts from the current values.
+ BrushToolSettings c = store.switchBrush(standard, clay, live);
+ CHECK(c == live);
+ c.radiusPx = 20.0f;
+ c.focal = -0.5f;
+ c.stroke = StrokeType::Spray;
+ c.alphaName = "Round";
+ const BrushToolSettings s = store.switchBrush(clay, standard, c);
+ CHECK(s == live);
+ const BrushToolSettings back = store.switchBrush(standard, clay, s);
+ CHECK(back == c);
+ // Custom brushes are kept by name, apart from the built-ins.
+ CHECK(brushSettingsKey("Standard", "Clay Slab") == "custom:Clay Slab");
+ CHECK(brushSettingsKey("Standard", "Clay Slab") != standard);
+}
+
+TEST(brush_settings_share_size_and_intensity) {
+ BrushSettingsStore store;
+ store.shareSizeIntensity = true;
+ BrushToolSettings a;
+ a.radiusPx = 30.0f;
+ a.strength = 0.2f;
+ a.alphaName = "Square";
+ store.switchBrush("builtin:Standard", "builtin:Clay", a);
+ BrushToolSettings b = a;
+ b.alphaName = "Noise";
+ b.radiusPx = 200.0f;
+ b.strength = 0.9f;
+ const BrushToolSettings back = store.switchBrush("builtin:Clay", "builtin:Standard", b);
+ CHECK(back.radiusPx == 200.0f && back.strength == 0.9f); // size and intensity stay
+ CHECK(back.alphaName == "Square"); // the rest follows the brush
+}
+
+TEST(brush_settings_save_load_reset) {
+ const std::string path = tempPath("sf_brush_settings_test.json");
+ std::remove(path.c_str());
+ BrushSettingsStore store;
+ BrushToolSettings s;
+ s.radiusPx = 42.0f;
+ s.strength = 0.25f;
+ s.focal = 0.5f;
+ s.rgbIntensity = 0.3f;
+ s.stroke = StrokeType::LazyMouse;
+ s.lazyRadius = 80.0f;
+ s.alphaName = "brick";
+ s.alphaPath = "/alphas/brick.png";
+ s.alphaInvert = true;
+ store.store("custom:Clay Slab", s);
+ store.shareSizeIntensity = true;
+ std::string err;
+ CHECK(store.save(path, err));
+
+ BrushSettingsStore loaded;
+ CHECK(loaded.load(path, err));
+ CHECK(loaded.shareSizeIntensity);
+ BrushToolSettings got;
+ CHECK(loaded.savedFor("custom:Clay Slab", got));
+ CHECK(got == s);
+ CHECK(!loaded.savedFor("builtin:Clay", got));
+ // A saved brush not used this session starts with its saved values.
+ loaded.shareSizeIntensity = false;
+ CHECK(loaded.switchBrush("builtin:Standard", "custom:Clay Slab", BrushToolSettings{}) == s);
+
+ const BrushToolSettings def = loaded.reset("custom:Clay Slab");
+ CHECK(def == BrushToolSettings{});
+ CHECK(!loaded.hasSaved("custom:Clay Slab"));
+ CHECK(BrushSettingsStore::shipped("builtin:Vdm").stroke == StrokeType::DragRect);
+ // After the reset the session doesn't bring back the old values either.
+ BrushToolSettings live;
+ live.radiusPx = 99.0f;
+ CHECK(loaded.switchBrush("builtin:Standard", "custom:Clay Slab", live).radiusPx == 99.0f);
+
+ // Missing file: nothing saved, no error. Damaged values are clamped.
+ BrushSettingsStore none;
+ CHECK(none.load(path + ".missing", err) && none.savedCount() == 0);
+ Json j;
+ CHECK(Json::parse(R"({"brushes":{"builtin:Clay":{"draw_size":5000,"z_intensity":-3,"stroke":"nope"}}})", j, err));
+ BrushSettingsStore bad;
+ bad.applyJson(j);
+ CHECK(bad.savedFor("builtin:Clay", got));
+ CHECK(got.radiusPx == 800.0f && got.strength == 0.0f && got.stroke == StrokeType::Dots);
+ std::remove(path.c_str());
+}
diff --git a/tests/ui/brush_settings.sfui b/tests/ui/brush_settings.sfui
new file mode 100644
index 00000000..37e0de11
--- /dev/null
+++ b/tests/ui/brush_settings.sfui
@@ -0,0 +1,74 @@
+# #78: every brush keeps its own Draw Size, Z Intensity, alpha... Picking another brush and coming back brings its
+# values back; Save Brush Settings keeps them for the next start; Reset Brush to Default puts back the shipped ones;
+# Preferences > Input > Share size and intensity across brushes keeps size and intensity as before.
+run ui_look look=classic layout=zbrush # written for the Classic look and the ZBrush-style palette bar and shelves
+run new_sphere subdivisions=3
+key f
+click "Standard##brush.standard"
+expect_value draw_size == 60
+expect_value brush_settings_saved == 0
+key ]
+key ]
+remember draw_size std
+run brush_set_alpha name=pores
+wait 0.1
+remember alpha_width std_alpha
+expect_value alpha_width > 0
+
+# A brush not used yet starts from the current values (nothing jumps), then keeps its own.
+click "Clay##brush.clay"
+expect_value draw_size == $std
+key [
+key [
+key [
+remember draw_size clay
+run brush_set_alpha name=round
+wait 0.1
+expect_value alpha_width == 0
+click "Standard##brush.standard"
+expect_value draw_size == $std
+expect_value alpha_width == $std_alpha
+click "Clay##brush.clay"
+expect_value draw_size == $clay
+expect_value alpha_width == 0
+
+# A custom brush has its own too.
+run brush_create name="Test Slab" depth=0.4
+wait 0.1 # (the switch is picked up on the next frame)
+key ]
+key ]
+key ]
+key ]
+remember draw_size slab
+click "Clay##brush.clay"
+expect_value draw_size == $clay
+run brush_create name="Test Slab" depth=0.4
+wait 0.1 # (the switch is picked up on the next frame)
+expect_value draw_size == $slab
+
+# Save Brush Settings, then Reset Brush to Default.
+click "Brush##palette.brush"
+click "Save Brush Settings##brush.save_settings"
+expect_value brush_settings_saved == 1
+expect_status "Saved this brush's settings"
+click "Reset Brush to Default##brush.reset_settings"
+expect_value brush_settings_saved == 0
+expect_value draw_size == 60
+expect_value z_intensity == 50
+expect_status "default settings"
+click "Brush##palette.brush" # (closed again: the shelf's tiles are clickable)
+
+# Share size and intensity across brushes: size and intensity stay; the alpha still follows the brush.
+click "Standard##brush.standard"
+expect_value draw_size == $std
+expect_value share_brush_size == 0
+click "Preferences##palette.preferences"
+click "Input##preferences.input"
+click "Share size and intensity across brushes##brush.share_size"
+expect_value share_brush_size == 1
+key escape
+click "Clay##brush.clay"
+expect_value draw_size == $std
+expect_value alpha_width == 0
+click "Standard##brush.standard"
+expect_value alpha_width == $std_alpha
diff --git a/tests/ui/vdm.sfui b/tests/ui/vdm.sfui
index 9c87c9c6..9c832089 100644
--- a/tests/ui/vdm.sfui
+++ b/tests/ui/vdm.sfui
@@ -78,7 +78,7 @@ click "Stroke##palette.stroke"
click "Dots##stroke"
key escape
click "Horn##vdm0"
-expect_value stroke_type == 1
+expect_value stroke_type == 2 # each brush keeps its own stroke (#78): the VDM brush its Slide Stamp
click "Clay##brush.clay"
expect_value stroke_type == 0