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.

Transforms record into the active sculpt layer (#133)

islandmed3a437e4dc0bf
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c405e6e2..dcd96a7e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,11 @@ All notable changes to SculptForge are listed here. The format follows
## [Unreleased]
+### Fixed
+- Move, Scale and Rotate (the Transform gizmo and the `transform` command) with a sculpt layer active now record into
+ that layer, masked or not, like a stroke (#133, thanks Devon0209): the base mesh stays, the layer's weight dials
+ the move in and out, undo works. With Base mesh active they move the base and every layer, as before.
+
## [0.11.1] - 2026-11-05
### Added
diff --git a/docs/guide/gizmo.md b/docs/guide/gizmo.md
index 6f331bb1..1facca39 100644
--- a/docs/guide/gizmo.md
+++ b/docs/guide/gizmo.md
@@ -104,9 +104,11 @@ part.
## What a transform keeps
A transform only changes where the vertices are. Face Groups, quads, UVs, vertex paint, textures, the mask and
-the topology stay as they are. [Sculpt layers](layers.md) move with the surface: the base and every layer's
-offsets are transformed together, so a layer you sculpted on a forearm stays on it after posing, and layer
-sliders keep working. With [subdivision levels](subdivision-levels.md) you can pose at a low level: the detail of
+the topology stay as they are. With **Base mesh** active, [sculpt layers](layers.md) move with the surface: the
+base and every layer's offsets are transformed together, so a layer you sculpted on a forearm stays on it after
+posing, and layer sliders keep working. With a sculpt layer active, the transform goes into that layer, like a
+stroke: the base stays, and the layer's weight dials the move in and out (a pose as a blend shape). A layer whose
+weight is 0 can't be transformed into. With [subdivision levels](subdivision-levels.md) you can pose at a low level: the detail of
the higher levels, sculpt layers included, turns with it when you step back up. Projects save the result, and the gizmo's pivot and options too.
## Scripting
diff --git a/docs/guide/layers.md b/docs/guide/layers.md
index 9461614a..776046c6 100644
--- a/docs/guide/layers.md
+++ b/docs/guide/layers.md
@@ -42,7 +42,8 @@ or `Layer Base`).
1. Type a name in the field at the bottom of **Tool > Layers**, for example `Smile` or `BlinkL`.
2. Click **+ Add** (or press <kbd>Enter</kbd> in the field). The new layer becomes the active one.
-3. Sculpt. Every stroke goes into the layer until you select another layer or **Base mesh**.
+3. Sculpt. Every stroke goes into the layer until you select another layer or **Base mesh**. Moving, scaling and
+ rotating with the [Transform gizmo](gizmo.md) (or the `transform` command) goes into the layer too, masked or not.
A layer whose weight is 0 can't be sculpted into: SculptForge tells you to raise the weight first.
diff --git a/src/app/Gizmo.cpp b/src/app/Gizmo.cpp
index b4a0da79..2598f6f8 100644
--- a/src/app/Gizmo.cpp
+++ b/src/app/Gizmo.cpp
@@ -485,14 +485,18 @@ bool App::gizmoMouseDown(float x, float y, SDL_Keymod mod) {
s.bvh.build(s.mesh);
s.bvhDirty = false;
}
+ // The active Part records into its active sculpt layer, as strokes do.
+ const int layer = i == doc_.activePart ? doc_.activeLayer : -1;
TransformSession session;
- if (!session.begin(m, transformWeights(m, gizmo_.useMask, gizmo_.falloff), symmetry_, gizmo_.pivot))
+ if (!session.begin(m, transformWeights(m, gizmo_.useMask, gizmo_.falloff), symmetry_, gizmo_.pivot, layer))
continue;
gizmo_.parts.push_back(i);
gizmo_.sessions.push_back(std::move(session));
}
if (gizmo_.sessions.empty()) {
- status_ = gizmo_.useMask && doc_.mesh.hasMask() ? "Nothing to move: everything is masked (Ctrl+click the "
+ status_ = TransformSession::layerWeightZero(doc_.mesh, doc_.activeLayer)
+ ? "Active layer weight is 0 - raise it to transform into the layer"
+ : gizmo_.useMask && doc_.mesh.hasMask() ? "Nothing to move: everything is masked (Ctrl+click the "
"background to invert the mask)"
: "Nothing to move: the Part is hidden or empty";
gizmo_.handle = H::None;
diff --git a/src/core/MeshTransform.cpp b/src/core/MeshTransform.cpp
index ba7f6ef2..9edfce4b 100644
--- a/src/core/MeshTransform.cpp
+++ b/src/core/MeshTransform.cpp
@@ -154,8 +154,14 @@ glm::vec3 transformCenter(const Mesh& mesh, const std::vector<float>& weights) {
return box.valid() ? box.center() : glm::vec3(0.0f);
}
-bool TransformSession::begin(const Mesh& mesh, std::vector<float> weights, Symmetry mirror, const glm::vec3& pivot) {
+bool TransformSession::layerWeightZero(const Mesh& mesh, int layer) {
+ return layer >= 0 && layer < int(mesh.layers.size()) && std::abs(mesh.layers[size_t(layer)].weight) < 1e-6f;
+}
+
+bool TransformSession::begin(const Mesh& mesh, std::vector<float> weights, Symmetry mirror, const glm::vec3& pivot,
+ int layer) {
*this = TransformSession();
+ if (layerWeightZero(mesh, layer)) return false;
const size_t nv = mesh.vertexCount();
const bool weighted = weights.size() == nv;
std::vector<uint16_t> full(weighted ? nv : 0, 0); // every vertex's level (0 = still)
@@ -208,17 +214,21 @@ bool TransformSession::begin(const Mesh& mesh, std::vector<float> weights, Symme
usedLevels_.assign(kLevels + 1, 0);
for (uint16_t l : level_) usedLevels_[l] = 1;
layered_ = !mesh.layers.empty();
- const auto& src = mesh.basePositions();
+ layer_ = layer >= 0 && layer < int(mesh.layers.size()) ? layer : -1;
+ if (layer_ >= 0) invWeight_ = 1.0f / mesh.layers[size_t(layer_)].weight;
+ // Recording into a layer starts from the shape as shown; otherwise from the base.
+ const auto& src = layer_ >= 0 ? mesh.positions : mesh.basePositions();
base_.resize(verts_.size());
normals_.resize(verts_.size());
for (size_t i = 0; i < verts_.size(); ++i) {
base_[i] = src[verts_[i]];
normals_[i] = mesh.normals[verts_[i]];
}
- deltas_.resize(mesh.layers.size());
- for (size_t l = 0; l < mesh.layers.size(); ++l) {
+ deltas_.resize(layer_ >= 0 ? 1 : mesh.layers.size());
+ for (size_t l = 0; l < deltas_.size(); ++l) {
+ const std::vector<glm::vec3>& d = mesh.layers[layer_ >= 0 ? size_t(layer_) : l].delta;
deltas_[l].resize(verts_.size());
- for (size_t i = 0; i < verts_.size(); ++i) deltas_[l][i] = mesh.layers[l].delta[verts_[i]];
+ for (size_t i = 0; i < verts_.size(); ++i) deltas_[l][i] = d[verts_[i]];
}
active_ = true;
return true;
@@ -246,10 +256,23 @@ void TransformSession::apply(Mesh& mesh, const MeshTransform& t) {
}
}
std::vector<glm::vec3>& target = layered_ ? mesh.base : mesh.positions;
+ std::vector<glm::vec3>* layerDelta = layer_ >= 0 ? &mesh.layers[size_t(layer_)].delta : nullptr;
parallelRange(verts_.size(), [&](size_t b, size_t e) {
for (size_t i = b; i < e; ++i) {
const Level& L = table_[side_[i] * stride + level_[i]];
const uint32_t v = verts_[i];
+ if (layerDelta) { // the move goes into the layer's offsets, scaled up by its weight
+ const glm::vec3 p = L.translateOnly ? base_[i] + L.offset : L.linear * base_[i] + L.offset;
+ (*layerDelta)[v] = deltas_[0][i] + (p - base_[i]) * invWeight_;
+ if (L.translateOnly) {
+ mesh.normals[v] = normals_[i];
+ } else {
+ const glm::vec3 n = L.normal * normals_[i];
+ const float len = glm::length(n);
+ mesh.normals[v] = len > 1e-20f ? n / len : normals_[i];
+ }
+ continue;
+ }
if (L.translateOnly) {
target[v] = base_[i] + L.offset;
for (size_t k = 0; k < deltas_.size(); ++k) mesh.layers[k].delta[v] = deltas_[k][i];
@@ -271,6 +294,13 @@ void TransformSession::apply(Mesh& mesh, const MeshTransform& t) {
}
void TransformSession::writeBack(Mesh& mesh) {
+ if (layer_ >= 0) {
+ std::vector<glm::vec3>& d = mesh.layers[size_t(layer_)].delta;
+ for (size_t i = 0; i < verts_.size(); ++i) d[verts_[i]] = deltas_[0][i];
+ mesh.evaluate(verts_);
+ mesh.updateNormals(verts_);
+ return;
+ }
std::vector<glm::vec3>& target = layered_ ? mesh.base : mesh.positions;
for (size_t i = 0; i < verts_.size(); ++i) {
target[verts_[i]] = base_[i];
@@ -299,8 +329,14 @@ UndoRecord TransformSession::finish(Mesh& mesh, BVH& bvh, std::string label) {
if (verts_.size() * 4 > mesh.vertexCount()) bvh.refitAll(mesh);
else bvh.refit(mesh, verts_);
r.label = std::move(label);
- r.layer = -1;
r.positions.indices = verts_;
+ if (layer_ >= 0) { // like a stroke into the layer: its old offsets
+ r.layer = layer_;
+ r.positions.values = std::move(deltas_[0]);
+ *this = TransformSession();
+ return r;
+ }
+ r.layer = -1;
r.positions.values = std::move(base_);
for (auto& d : deltas_) {
UndoChannel<glm::vec3> ch;
@@ -334,7 +370,8 @@ size_t transformPart(Document& doc, int index, const MeshTransform& t, bool useM
info.bvhDirty = false;
}
TransformSession s;
- if (!s.begin(mesh, transformWeights(mesh, useMask, falloff), mirror, t.pivot)) return 0;
+ const int layer = active ? doc.activeLayer : -1;
+ if (!s.begin(mesh, transformWeights(mesh, useMask, falloff), mirror, t.pivot, layer)) return 0;
const size_t n = s.movingVertices();
s.apply(mesh, t);
UndoRecord r = s.finish(mesh, bvh, label);
diff --git a/src/core/MeshTransform.h b/src/core/MeshTransform.h
index 363b729c..3d38164f 100644
--- a/src/core/MeshTransform.h
+++ b/src/core/MeshTransform.h
@@ -11,8 +11,10 @@
// weight w moves by w * translate, turns by the rotation slerped to w and scales by mix(1, scale, w).
// Hidden vertices never move.
//
-// Sculpt layers move with the surface: the base takes the whole transform and every layer's offsets the
-// linear part, so positions == base + sum(weight * offset) still holds and the layers keep working.
+// With Base mesh active, sculpt layers move with the surface: the base takes the whole transform and every
+// layer's offsets the linear part, so positions == base + sum(weight * offset) still holds and the layers keep
+// working. With a sculpt layer active, the transform is recorded into that layer like a stroke: its offsets take
+// (moved - current) / weight, the base and the other layers stay, and the layer's weight dials the move in and out.
// Topology, UVs, quads, face groups, vertex paint and textures are left as they are.
#pragma once
@@ -69,8 +71,9 @@ public:
// `weights` as from transformWeights (empty = all). `mirror`: with a mask, the parts on the far side of
// each enabled symmetry plane (through the origin, as for brushes) from the pivot take the mirrored
// transform, so both arms pose together. Ignored for unmasked transforms and pivots on the plane.
- // False (and nothing to do) when no vertex can move.
- bool begin(const Mesh& mesh, std::vector<float> weights, Symmetry mirror, const glm::vec3& pivot);
+ // `layer`: the sculpt layer the transform records into (-1 = Base mesh: move the base and every layer).
+ // False (and nothing to do) when no vertex can move or `layer`'s weight is 0 (see layerWeightZero).
+ bool begin(const Mesh& mesh, std::vector<float> weights, Symmetry mirror, const glm::vec3& pivot, int layer = -1);
void apply(Mesh& mesh, const MeshTransform& t);
void restore(Mesh& mesh); // back to the originals (the session stays open)
// Refits the BVH and returns the undo step (empty when nothing moved). Ends the session.
@@ -80,6 +83,8 @@ public:
bool active() const { return active_; }
size_t movingVertices() const { return verts_.size(); }
bool mirrored() const { return mirrors_.size() > 1; }
+ // True when begin failed because the layer to record into has weight 0 (its offsets would not show).
+ static bool layerWeightZero(const Mesh& mesh, int layer);
private:
struct Level {
@@ -91,12 +96,15 @@ private:
bool active_ = false;
bool layered_ = false;
+ int layer_ = -1; // the sculpt layer recorded into (-1: base and every layer move)
+ float invWeight_ = 1.0f; // 1 / that layer's weight
std::vector<uint32_t> verts_; // vertices with weight > 0
std::vector<uint16_t> level_; // their weight, quantized to kLevels
std::vector<uint8_t> side_; // index into mirrors_
std::vector<uint32_t> boundary_; // vertices whose normals are recomputed (partial weight or a still neighbour)
- std::vector<glm::vec3> base_, normals_; // originals: Mesh::layerTarget(-1) and normals
- std::vector<std::vector<glm::vec3>> deltas_; // originals of each sculpt layer's offsets
+ std::vector<glm::vec3> base_, normals_; // originals: Mesh::layerTarget(-1) (positions when
+ // recording into a layer) and normals
+ std::vector<std::vector<glm::vec3>> deltas_; // originals of each sculpt layer's offsets (only layer_'s)
std::vector<glm::vec3> mirrors_; // sign vectors; [0] = (1,1,1)
std::vector<uint8_t> usedLevels_; // which quantized weights occur
std::vector<Level> table_; // mirrors_.size() * (kLevels + 1)
@@ -106,8 +114,8 @@ public:
static constexpr int kLevels = 4096;
};
-// Transforms Part `index` of `doc` (active or not) as one undoable step on that Part. Returns the number
-// of vertices that moved.
+// Transforms Part `index` of `doc` (active or not) as one undoable step on that Part; on the active Part it
+// records into the active sculpt layer (doc.activeLayer). Returns the number of vertices that moved.
size_t transformPart(Document& doc, int index, const MeshTransform& t, bool useMask, float falloff, Symmetry mirror,
const std::string& label);
diff --git a/src/core/TransformCommands.cpp b/src/core/TransformCommands.cpp
index 488d4c64..3049ff69 100644
--- a/src/core/TransformCommands.cpp
+++ b/src/core/TransformCommands.cpp
@@ -98,6 +98,8 @@ CommandResult runTransform(Document& doc, const Args& a) {
return {false, "Bad pivot '" + pivot + "' (expected center, origin or x,y,z)"};
}
if (t.identity()) return {true, "Nothing to do: the transform changes nothing"};
+ if (TransformSession::layerWeightZero(doc.mesh, doc.activeLayer))
+ return {false, "Active layer weight is 0 - raise it to transform into the layer"};
size_t moved = 0;
for (int i : targets) moved += transformPart(doc, i, t, useMask, falloff, sym, "Transform");
@@ -116,7 +118,8 @@ void registerTransformCommands(CommandRegistry& reg) {
reg.add({"transform",
"Move, rotate and scale the active Part, or only its unmasked part (the Gizmo as a command): "
"p' = pivot + translate + R * S * (p - pivot). Masked vertices stay, partly masked ones move partly; "
- "falloff eases it in from the mask's edge (a soft joint for posing). One undo step per Part.",
+ "falloff eases it in from the mask's edge (a soft joint for posing). With a sculpt layer active the "
+ "move goes into that layer (as a stroke does). One undo step per Part.",
{{"translate", T::String, "Movement x,y,z in world units. Default 0,0,0.", false},
{"rotate", T::String, "Rotation in degrees about the X, then Y, then Z axis (of orient). Default 0,0,0.",
false},
diff --git a/tests/test_transform.cpp b/tests/test_transform.cpp
index 6843fcee..9e9252b3 100644
--- a/tests/test_transform.cpp
+++ b/tests/test_transform.cpp
@@ -237,6 +237,105 @@ TEST(transform_sculpt_layers_move_with_the_surface) {
CHECK(sameBits(m.layers[0].delta, delta));
}
+// GitHub #133: with a sculpt layer active, Move / Scale / Rotate record into that layer like a stroke (masked or
+// not): the base and other layers stay, the layer's weight dials the move in and out, undo restores it.
+TEST(transform_records_into_the_active_layer) {
+ Document d = makeDoc(makeArm());
+ Mesh& m = d.mesh;
+ CHECK(run(d, "layer_add", {{"name", std::string("Other")}}).ok);
+ for (uint32_t v = 0; v < m.vertexCount(); ++v) m.layers[0].delta[v] = m.normals[v] * 0.05f;
+ m.setLayerWeight(0, 0.5f);
+ CHECK(run(d, "layer_add", {{"name", std::string("Pose")}}).ok);
+ CHECK(d.activeLayer == 1);
+ const auto pos = m.positions, base = m.base, other = m.layers[0].delta;
+
+ // Unmasked rotate: the whole Part turns, all of it in the active layer.
+ CHECK(run(d, "transform", {{"rotate", std::string("0,0,90")}, {"pivot", std::string("origin")}}).ok);
+ auto rot = [](const glm::vec3& p) { return glm::vec3(-p.y, p.x, p.z); };
+ float err = 0.0f;
+ for (size_t v = 0; v < pos.size(); ++v) err = std::max(err, glm::length(m.positions[v] - rot(pos[v])));
+ CHECK(err < 1e-5f);
+ CHECK(sameBits(m.base, base));
+ CHECK(sameBits(m.layers[0].delta, other));
+ m.setLayerWeight(1, 0.0f); // the layer off: the shape as before
+ CHECK(maxDistance(m.positions, pos) < 1e-5f);
+ m.setLayerWeight(1, 0.5f); // half: halfway along each vertex's move
+ err = 0.0f;
+ for (size_t v = 0; v < pos.size(); ++v) err = std::max(err, glm::length(m.positions[v] - glm::mix(pos[v], rot(pos[v]), 0.5f)));
+ CHECK(err < 1e-5f);
+ m.setLayerWeight(1, 1.0f);
+ CHECK(run(d, "undo").ok);
+ CHECK(maxDistance(m.positions, pos) < 1e-6f);
+ for (const glm::vec3& o : m.layers[1].delta) CHECK(o == glm::vec3(0.0f));
+
+ // Masked (the issue: mask, invert, move): only the unmasked part moves, into the layer, at the layer's weight.
+ maskBelowX(m, 0.0f);
+ m.setLayerWeight(1, 0.25f);
+ const auto pos2 = m.positions;
+ CHECK(run(d, "transform", {{"translate", std::string("0,1,0")}}).ok);
+ bool still = true, full = true;
+ for (size_t v = 0; v < pos2.size(); ++v) {
+ if (m.mask[v] >= 1.0f) still = still && m.positions[v] == pos2[v];
+ else full = full && glm::length(m.positions[v] - (pos2[v] + glm::vec3(0, 1, 0))) < 1e-5f;
+ }
+ CHECK(still);
+ CHECK(full);
+ CHECK(sameBits(m.base, base));
+ m.setLayerWeight(1, 0.0f);
+ float e2 = 0.0f; // the layer off: base + the other layer at 0.5
+ for (size_t v = 0; v < base.size(); ++v) e2 = std::max(e2, glm::length(m.positions[v] - (base[v] + 0.5f * other[v])));
+ CHECK(e2 < 1e-5f);
+ m.setLayerWeight(1, 0.25f);
+ CHECK(run(d, "undo").ok);
+ CHECK(maxDistance(m.positions, pos2) < 1e-6f);
+
+ // Weight 0: refused, like a stroke.
+ m.setLayerWeight(1, 0.0f);
+ const auto off = m.positions;
+ CHECK(!run(d, "transform", {{"translate", std::string("0,1,0")}}).ok);
+ CHECK(sameBits(m.positions, off));
+
+ // Base mesh active: the old behaviour (the base and every layer move).
+ m.setLayerWeight(1, 1.0f);
+ CHECK(run(d, "layer_set_active", {{"index", -1.0}}).ok);
+ CHECK(run(d, "transform", {{"translate", std::string("0,1,0")}, {"masked", false}}).ok);
+ CHECK(m.base[0] == base[0] + glm::vec3(0, 1, 0));
+}
+
+// The gizmo's path: a TransformSession recording into a layer, dragged (applied many times), cancelled, finished.
+TEST(transform_session_into_a_layer) {
+ Document d = makeDoc(makeIcosphere(3));
+ Mesh& m = d.mesh;
+ m.addLayer("Pose");
+ m.setLayerWeight(0, 0.5f);
+ d.syncBvh();
+ const auto pos = m.positions, base = m.base;
+ TransformSession s;
+ CHECK(s.begin(m, {}, Symmetry{false, false, false}, glm::vec3(0.0f), 0));
+ MeshTransform t;
+ for (int k = 1; k <= 10; ++k) {
+ t.rotate = eulerRotation(glm::vec3(0, 0, 9.0f * float(k)));
+ t.scale = glm::vec3(1.0f + 0.05f * float(k));
+ s.apply(m, t);
+ }
+ s.restore(m);
+ CHECK(maxDistance(m.positions, pos) < 1e-6f);
+ s.apply(m, t);
+ float err = 0.0f;
+ for (size_t v = 0; v < pos.size(); ++v) err = std::max(err, glm::length(m.positions[v] - t.apply(pos[v])));
+ CHECK(err < 1e-4f);
+ CHECK(sameBits(m.base, base));
+ UndoRecord r = s.finish(m, d.bvh, "Gizmo Rotate");
+ CHECK(r.layer == 0 && r.layerOffsets.empty());
+ d.undo.push(std::move(r));
+ CHECK(run(d, "undo").ok);
+ CHECK(maxDistance(m.positions, pos) < 1e-6f);
+ // Weight 0: nothing to do.
+ m.setLayerWeight(0, 0.0f);
+ CHECK(!s.begin(m, {}, Symmetry{false, false, false}, glm::vec3(0.0f), 0));
+ CHECK(TransformSession::layerWeightZero(m, 0));
+}
+
TEST(transform_leaves_topology_and_attributes) {
Document d = makeDoc(makeCube(6));
Mesh& m = d.mesh;
diff --git a/tests/ui/gizmo.sfui b/tests/ui/gizmo.sfui
index 90d85970..59c7bdec 100644
--- a/tests/ui/gizmo.sfui
+++ b/tests/ui/gizmo.sfui
@@ -126,6 +126,30 @@ shot posed-redo
key ctrl+z
run mask_clear
+# A sculpt layer active (GitHub #133): mask, invert, move: the move goes into the layer, so its weight dials it in
+# and out and the base mesh stays.
+run layer_add name=Pose
+expect_value layers == 1
+run mask_rect x0=-1 y0=-1 x1=0 y1=1 ; mask_invert
+key w
+snapshot
+gizmo_drag move_y 0 -70
+expect_value moved > 1000
+expect_value moved_masked == 0
+expect_status Gizmo Move
+shot layer-moved
+run layer_set_weight index=0 weight=0
+expect_value moved == 0
+run layer_set_weight index=0 weight=1
+expect_value moved > 1000
+# Weight 0: the gizmo won't move into the layer and says why.
+run layer_set_weight index=0 weight=0
+gizmo_drag move_y 0 -70
+expect_value moved == 0
+expect_status weight is 0
+run layer_set_weight index=0 weight=1 ; layer_remove index=0 ; mask_clear
+key r
+
# Every control explains itself with Ctrl held; Ctrl+F1 opens this chapter of the guide.
hold ctrl
hover "Gizmo/Apply"