Internal change

PiperOrigin-RevId: 859578296
Change-Id: If4dd4ca68ab9750693e5f6ff20ea0c78d6b93464
This commit is contained in:
Matija Kecman
2026-01-22 06:19:39 -08:00
committed by Copybara-Service
parent 54dd623650
commit 98593259e7
2 changed files with 63 additions and 35 deletions
+62 -35
View File
@@ -156,6 +156,7 @@ void App::ClearModel() {
void App::RequestModelLoad(std::string model_file) { void App::RequestModelLoad(std::string model_file) {
pending_load_ = std::move(model_file); pending_load_ = std::move(model_file);
model_kind_ = kModelFromFile;
} }
void App::RequestModelReload() { void App::RequestModelReload() {
@@ -222,7 +223,7 @@ void App::LoadModelFromBuffer(std::span<const std::byte> buffer,
return; return;
} }
InitModel(model, spec, &vfs, "", kModelFromBuffer); InitModel(model, spec, &vfs, std::string(filename), kModelFromBuffer);
mj_deleteVFS(&vfs); mj_deleteVFS(&vfs);
} }
@@ -303,37 +304,39 @@ void App::UpdatePhysics() {
return; return;
} }
if (!step_control_.IsPaused()) {
mju_zero(data_->xfrc_applied, 6 * model_->nbody);
mjv_applyPerturbPose(model_, data_, &perturb_, 0);
mjv_applyPerturbForce(model_, data_, &perturb_);
} else {
mjv_applyPerturbPose(model_, data_, &perturb_, 1);
}
if (data_) {
for (int i = 0; i < mjNTIMER; i++) {
data_->timer[i].duration = 0;
data_->timer[i].number = 0;
}
}
bool stepped = false; bool stepped = false;
{
if (!step_control_.IsPaused()) {
mju_zero(data_->xfrc_applied, 6 * model_->nbody);
mjv_applyPerturbPose(model_, data_, &perturb_, 0);
mjv_applyPerturbForce(model_, data_, &perturb_);
} else {
mjv_applyPerturbPose(model_, data_, &perturb_, 1);
}
platform::StepControl::Status status = step_control_.Advance(model_, data_); if (data_) {
if (status == platform::StepControl::Status::kPaused) { for (int i = 0; i < mjNTIMER; i++) {
// do nothing data_->timer[i].duration = 0;
} else if (status == platform::StepControl::Status::kOk) { data_->timer[i].number = 0;
stepped = true; }
// If we are adding to the history we didn't have a divergence error }
step_error_ = "";
} else if (status == platform::StepControl::Status::kAutoReset) { platform::StepControl::Status status = step_control_.Advance(model_, data_);
ResetPhysics(); if (status == platform::StepControl::Status::kPaused) {
} else if (status == platform::StepControl::Status::kDiverged) { // do nothing
stepped = true; } else if (status == platform::StepControl::Status::kOk) {
for (mjtWarning w : platform::StepControl::kDivergedWarnings) { stepped = true;
if (data_->warning[w].number > 0) { // If we are adding to the history we didn't have a divergence error
step_error_ = mju_warningText(w, data_->warning[w].lastinfo); step_error_ = "";
} else if (status == platform::StepControl::Status::kAutoReset) {
ResetPhysics();
} else if (status == platform::StepControl::Status::kDiverged) {
stepped = true;
for (mjtWarning w : platform::StepControl::kDivergedWarnings) {
if (data_->warning[w].number > 0) {
step_error_ = mju_warningText(w, data_->warning[w].lastinfo);
}
} }
} }
} }
@@ -373,13 +376,19 @@ bool App::Update() {
// Check to see if we need to load a new model. // Check to see if we need to load a new model.
if (pending_load_.has_value()) { if (pending_load_.has_value()) {
std::string model_file = std::move(pending_load_.value()); std::string load_data = std::move(pending_load_.value());
pending_load_.reset(); pending_load_.reset();
if (model_kind_ == kModelFromBuffer) {
if (model_file.empty()) { // TODO(matijak): need to pass the content type and not assume mjb.
InitEmptyModel(); LoadModelFromBuffer(
std::span<const std::byte>(
reinterpret_cast<const std::byte*>(load_data.data()),
load_data.size()),
"application/mjb", model_name_);
} else if (model_kind_ == kModelFromFile) {
LoadModelFromFile(load_data);
} else { } else {
LoadModelFromFile(model_file); InitEmptyModel();
} }
} }
@@ -733,6 +742,15 @@ void App::LoadSettings() {
if (!settings.empty()) { if (!settings.empty()) {
ui_.FromDict(platform::ReadIniSection(settings, "[Studio][UX]")); ui_.FromDict(platform::ReadIniSection(settings, "[Studio][UX]"));
ImGui::LoadIniSettingsFromMemory(settings.data(), settings.size()); ImGui::LoadIniSettingsFromMemory(settings.data(), settings.size());
platform::KeyValues plugin_names =
platform::ReadIniSection(settings, "[Studio][Plugins]");
platform::ForEachGuiPlugin([&](platform::GuiPlugin* plugin) {
auto it = plugin_names.find(plugin->name);
if (it != plugin_names.end()) {
plugin->active = std::stoi(it->second) != 0;
}
});
} }
} }
} }
@@ -741,6 +759,13 @@ void App::SaveSettings() {
if (!ini_path_.empty()) { if (!ini_path_.empty()) {
std::string settings = ImGui::SaveIniSettingsToMemory(); std::string settings = ImGui::SaveIniSettingsToMemory();
platform::AppendIniSection(settings, "[Studio][UX]", ui_.ToDict()); platform::AppendIniSection(settings, "[Studio][UX]", ui_.ToDict());
platform::KeyValues plugin_names;
platform::ForEachGuiPlugin([&](platform::GuiPlugin* plugin) {
plugin_names[plugin->name] = std::to_string((int)plugin->active);
});
platform::AppendIniSection(settings, "[Studio][Plugins]", plugin_names);
platform::SaveText(settings, ini_path_); platform::SaveText(settings, ini_path_);
} }
} }
@@ -1545,6 +1570,7 @@ void App::MainMenuGui() {
#endif // !__EMSCRIPTEN__ #endif // !__EMSCRIPTEN__
ImGui::EndMenu(); ImGui::EndMenu();
} }
if (ImGui::BeginMenu("Simulation")) { if (ImGui::BeginMenu("Simulation")) {
if (ImGui::MenuItem("Pause", "Space", step_control_.IsPaused())) { if (ImGui::MenuItem("Pause", "Space", step_control_.IsPaused())) {
step_control_.TogglePause(); step_control_.TogglePause();
@@ -1572,9 +1598,9 @@ void App::MainMenuGui() {
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }
if (ImGui::BeginMenu("View")) { if (ImGui::BeginMenu("View")) {
if (ImGui::MenuItem("Save Config")) { if (ImGui::MenuItem("Save Config")) {
SaveSettings(); SaveSettings();
@@ -1604,6 +1630,7 @@ void App::MainMenuGui() {
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }
if (ImGui::BeginMenu("Charts")) { if (ImGui::BeginMenu("Charts")) {
if (ImGui::MenuItem("Solver", "F9")) { if (ImGui::MenuItem("Solver", "F9")) {
tmp_.chart_solver = !tmp_.chart_solver; tmp_.chart_solver = !tmp_.chart_solver;
+1
View File
@@ -188,6 +188,7 @@ class App {
std::vector<const char*> GetCameraNames(); std::vector<const char*> GetCameraNames();
std::string ini_path_; std::string ini_path_;
std::string model_name_; // Used if model_kind_ is kModelFromBuffer.
std::string model_path_; std::string model_path_;
std::string load_error_; std::string load_error_;
std::string step_error_; std::string step_error_;