Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.
C++ 3d3d-sculptingcppdear-imguidigital-sculptingfbxgame-developmentgaussian-splattinggltfimage-to-3dmcpopenglopenusdsculptingtext-to-3dunreal-engine
USD import: metersPerUnit to metres exactly (cm and m stages, round trip; @NicTanghe); cached size readouts; UI test clicks follow a settling panel
islandmed5cf8b74cba25
diff --git a/changes/feat-real-world-units.md b/changes/feat-real-world-units.mdindex 61b81a93..fd60ad8a 100644--- a/changes/feat-real-world-units.md+++ b/changes/feat-real-world-units.md@@ -12,7 +12,7 @@ fixed: Exports keep the size a part was opened with (#148, @Swar55): a scene opeSTL opened in another unit is written back in it, and decimal sizes round-trip exactly (12.7 mm stays 12.7, not12.700001). Projects remember the unit.fixed: USD files in centimetres (`metersPerUnit = 0.01`, as Maya and Houdini write) open at their real size instead- of 100x too large.+ of 100x too large, to the exact millimetre (#148, thanks @NicTanghe).testers:- Open an STL from your slicer (e.g. a 20 mm calibration cube): Tool shows 20 x 20 x 20 mm. File > Export OBJ andExport STL, open both in the slicer: both are 20 mm.diff --git a/src/app/App.h b/src/app/App.hindex 42dda0db..850fd8a7 100644--- a/src/app/App.h+++ b/src/app/App.h@@ -1725,6 +1725,13 @@ public:// Preferences > Interface > Units (core/Units.h): the unit sizes show in.const LengthUnit& lengthUnit() const;void drawExportSizeLine(); // File > Import / Export: the size exports write (Palettes.cpp)+ // The active Part's and the visible Parts' bounds for the size readouts (#148): walked again only when the undo+ // revision, the active Part, its vertex count or the Parts and their visibility changed, never every frame.+ struct SizeBounds {+ uint64_t key = ~uint64_t(0);+ AABB part, visible;+ } sizeBounds_;+ const SizeBounds& sizeBounds();const CommandRegistry& commands() const { return commands_; }ImFont* helpTitleFont() const { return fontHelpTitle_ ? fontHelpTitle_ : ImGui::GetFont(); }ImFont* consoleFont() const { return fontConsole_ ? fontConsole_ : ImGui::GetFont(); }diff --git a/src/app/Palettes.cpp b/src/app/Palettes.cppindex 53d73d5f..24669f9a 100644--- a/src/app/Palettes.cpp+++ b/src/app/Palettes.cpp@@ -518,10 +518,20 @@ void App::drawPalette_Edit() {}}+const App::SizeBounds& App::sizeBounds() {+ if (doc_.sculptor.strokeActive() || solverBusy()) return sizeBounds_; // (holds still during a stroke)+ uint64_t key = doc_.undo.revision() * 0x9E3779B97F4A7C15ull ^ (uint64_t(doc_.activePart) << 40) ^+ uint64_t(doc_.mesh.vertexCount()) ^ (uint64_t(doc_.parts.size()) << 56);+ for (size_t i = 0; i < doc_.parts.size(); ++i)+ if (!doc_.parts[i].visible) key ^= 0xC2B2AE3D27D4EB4Full * (i + 1);+ if (key != sizeBounds_.key) sizeBounds_ = {key, doc_.mesh.bounds(), doc_.visibleBounds()};+ return sizeBounds_;+}+// File > Import / Export (#148): the size the exports write, in the Units preference, and the unit STL, OBJ and PLY// numbers are written in (the unit-less file the scene was opened from, else each format's convention).void App::drawExportSizeLine() {- const AABB b = doc_.visibleBounds();+ const AABB b = sizeBounds().visible;if (!b.valid()) return;ImGui::TextDisabled("%s", (std::string(tr("Export size")) + ": " + formatSize(b.extent(), lengthUnit())).c_str());const LengthUnit* stl = unitForScale(doc_.unitlessExportScale("stl"));@@ -814,7 +824,7 @@ void App::drawPalette_Tool() {ImGui::SameLine(ImGui::GetContentRegionMax().x - ImGui::CalcTextSize(tris).x);ImGui::TextDisabled("%s", tris);// The Part's size, width x height x depth, in the Units preference (#148).- if (const AABB b = doc_.mesh.bounds(); b.valid()) {+ if (const AABB b = sizeBounds().part; b.valid()) {ImGui::TextDisabled("%s", formatSize(b.extent(), lengthUnit()).c_str());ui::HelpText("Part size", nullptr,"The active Part's width x height x depth (X, Y, Z). The unit is set in Preferences > Interface > "diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cppindex 80a269f7..dac13031 100644--- a/src/app/UiTest.cpp+++ b/src/app/UiTest.cpp@@ -1155,9 +1155,18 @@ void Demo::testClick(int line, const std::string& label, int button, bool doublemouseMotion(to.x, to.y);return true;});- steps_.push_back([this, button, doubleClick](float t) {+ steps_.push_back([this, label, button, doubleClick](float t) {// A frame of hover first, so ImGui registers the item as hovered before the press.if (t == 0.0f) return false;+ // A panel still settling after the wheel (or a layout that moved) carries the item away: follow it and+ // hover again before pressing, so the press lands on the item, not on the row now under the old spot.+ ImVec2 to;+ bool visible = false;+ if (t < 1.0f && findItem(label, to, &visible) && visible &&+ (std::abs(to.x - mouse_.x) > 1.0f || std::abs(to.y - mouse_.y) > 1.0f)) {+ mouseMotion(to.x, to.y);+ return false;+ }mouseButton(true, button);mouseButton(false, button);if (doubleClick) {diff --git a/src/io/UsdIO.cpp b/src/io/UsdIO.cppindex 3c1ffe85..66bf0084 100644--- a/src/io/UsdIO.cpp+++ b/src/io/UsdIO.cpp@@ -3,6 +3,7 @@#include "io/UsdIO.h"#include "core/SelectionSets.h"+#include "core/Units.h"#include "io/TextureExport.h"#include "io/Zip.h"@@ -1012,10 +1013,14 @@ bool readUsdaText(const std::string& text, bool split, std::vector<MeshObject>&std::map<std::string, size_t> byPath;for (size_t i = 0; i < prims.size(); ++i) byPath[prims[i].path] = i;- // World transforms (parents come first in `prims`), in metres with Y up.- const glm::mat4 up = glm::scale(parser.upAxis == "Z" ? glm::rotate(glm::mat4(1.0f), glm::radians(-90.0f), glm::vec3(1, 0, 0))- : glm::mat4(1.0f),- glm::vec3(float(parser.metersPerUnit)));+ // World transforms (parents come first in `prims`), Y up, in the stage's unit. Mesh points go to metres through+ // double (fromFileUnits), so 12.7 cm is the float nearest 0.127 m; splats take the unit in their transform.+ const glm::mat4 up = parser.upAxis == "Z" ? glm::rotate(glm::mat4(1.0f), glm::radians(-90.0f), glm::vec3(1, 0, 0))+ : glm::mat4(1.0f);+ const double perUnit = 1.0 / parser.metersPerUnit; // file numbers per metre+ const auto toMetres = [perUnit](glm::vec3 v) {+ return glm::vec3(fromFileUnits(v.x, perUnit), fromFileUnits(v.y, perUnit), fromFileUnits(v.z, perUnit));+ };std::vector<glm::mat4> world(prims.size());for (size_t i = 0; i < prims.size(); ++i) {bool reset = false;@@ -1082,7 +1087,7 @@ bool readUsdaText(const std::string& text, bool split, std::vector<MeshObject>&return layers.back();};const uint32_t baseIndex = uint32_t(m.positions.size());- for (const auto& q : pts) m.positions.push_back(glm::vec3(world[i] * glm::vec4(q, 1.0f)));+ for (const auto& q : pts) m.positions.push_back(toMetres(glm::vec3(world[i] * glm::vec4(q, 1.0f))));// Texture coordinates: primvars:st, else the first texCoord2 primvar; face-varying (per face corner)// or per point, optionally indexed (<name>:indices).std::string stName = "primvars:st";@@ -1163,7 +1168,7 @@ bool readUsdaText(const std::string& text, bool split, std::vector<MeshObject>&for (size_t o = 0; o < offsets.size(); ++o) {const size_t v = pointIndices ? (o < pointIndices->nums.size() ? indexValue(pointIndices->nums[o], pts.size()) : pts.size()): o;- if (v < pts.size()) layer.delta[baseIndex + v] = linear * offsets[o];+ if (v < pts.size()) layer.delta[baseIndex + v] = toMetres(linear * offsets[o]);}}}@@ -1190,7 +1195,7 @@ bool readUsdaText(const std::string& text, bool split, std::vector<MeshObject>&const float len = glm::length(r);cloud.rotations[k] = len > 0.0f ? r / len : glm::quat(1.0f, 0.0f, 0.0f, 0.0f);}- cloud.transform = world[i];+ cloud.transform = glm::scale(glm::mat4(1.0f), glm::vec3(float(parser.metersPerUnit))) * world[i];cloud.name = p.path.substr(p.path.rfind('/') + 1);splatsFound = true;}diff --git a/tests/test_units.cpp b/tests/test_units.cppindex 566a231d..ea67114a 100644--- a/tests/test_units.cpp+++ b/tests/test_units.cpp@@ -219,23 +219,37 @@ TEST(units_gltf_fbx_usd_keep_the_size) {}TEST(units_usd_meters_per_unit) {- // A centimetre stage (Maya, Houdini): 5 x 5 x 19.95 cm comes in as 0.05 x 0.05 x 0.1995 m, not 100x.- const std::string path = tempPath("sf_units_cm.usda");- std::ostringstream s;- s << "#usda 1.0\n(\n metersPerUnit = 0.01\n upAxis = \"Y\"\n)\n\ndef Mesh \"Box\"\n{\n point3f[] points = [";- const auto tris = boxTriangles(scaled(kBox, 0.1f));- for (size_t i = 0; i < tris.size(); ++i) s << (i ? ", " : "") << "(" << tris[i].x << ", " << tris[i].y << ", " << tris[i].z << ")";- s << "]\n int[] faceVertexCounts = [";- for (size_t i = 0; i < tris.size() / 3; ++i) s << (i ? ", " : "") << 3;- s << "]\n int[] faceVertexIndices = [";- for (size_t i = 0; i < tris.size(); ++i) s << (i ? ", " : "") << i;- s << "]\n}\n";- std::ofstream(std::filesystem::path(path), std::ios::binary) << s.str();- CommandRegistry reg;- Document doc;- CHECK(reg.execute(doc, "open", {{"path", path}}).ok);- CHECK(near(meshBounds(doc.mesh), scaled(kBox, 0.001f)));- std::filesystem::remove(path);+ // Hand-written stages in centimetres (Maya, Houdini: metersPerUnit = 0.01) and in metres, with the same numbers:+ // -12.7 .. 199.5 cm comes in as exactly the floats nearest -0.127 .. 1.995 m (not 100x), metres as written, and+ // both come back the same after export_usd and reopening (#148, @NicTanghe).+ for (double metersPerUnit : {0.01, 1.0}) {+ const std::string path = tempPath(metersPerUnit == 1.0 ? "sf_units_m.usda" : "sf_units_cm.usda");+ const std::string out = tempPath("sf_units_usd_back.usda");+ std::ostringstream s;+ s << "#usda 1.0\n(\n metersPerUnit = " << metersPerUnit << "\n upAxis = \"Y\"\n)\n\ndef Mesh \"Box\"\n{\n"+ << " point3f[] points = [";+ const auto tris = boxTriangles(kBox);+ for (size_t i = 0; i < tris.size(); ++i) s << (i ? ", " : "") << "(" << tris[i].x << ", " << tris[i].y << ", " << tris[i].z << ")";+ s << "]\n int[] faceVertexCounts = [";+ for (size_t i = 0; i < tris.size() / 3; ++i) s << (i ? ", " : "") << 3;+ s << "]\n int[] faceVertexIndices = [";+ for (size_t i = 0; i < tris.size(); ++i) s << (i ? ", " : "") << i;+ s << "]\n}\n";+ std::ofstream(std::filesystem::path(path), std::ios::binary) << s.str();+ const double per = 1.0 / metersPerUnit;+ const Box expected{{fromFileUnits(kBox.lo.x, per), fromFileUnits(kBox.lo.y, per), fromFileUnits(kBox.lo.z, per)},+ {fromFileUnits(kBox.hi.x, per), fromFileUnits(kBox.hi.y, per), fromFileUnits(kBox.hi.z, per)}};+ CommandRegistry reg;+ Document doc;+ CHECK(reg.execute(doc, "open", {{"path", path}}).ok);+ CHECK(same(meshBounds(doc.mesh), expected));+ CHECK(reg.execute(doc, "export_usd", {{"path", out}}).ok);+ Document back;+ CHECK(reg.execute(back, "open", {{"path", out}}).ok);+ CHECK(same(meshBounds(back.mesh), expected));+ std::filesystem::remove(path);+ std::filesystem::remove(out);+ }}TEST(units_project_keeps_the_file_unit) {