simulate: fix display of loading message
This commit is contained in:
@@ -203,7 +203,11 @@ PYBIND11_MODULE(_simulate, pymodule) {
|
|||||||
}))
|
}))
|
||||||
.def("destroy", &SimulateWrapper::Destroy,
|
.def("destroy", &SimulateWrapper::Destroy,
|
||||||
py::call_guard<py::gil_scoped_release>())
|
py::call_guard<py::gil_scoped_release>())
|
||||||
|
.def("load_message", CallIfNotNull(&mujoco::Simulate::LoadMessage),
|
||||||
|
py::call_guard<py::gil_scoped_release>())
|
||||||
.def("load", &SimulateWrapper::Load)
|
.def("load", &SimulateWrapper::Load)
|
||||||
|
.def("load_message_clear", CallIfNotNull(&mujoco::Simulate::LoadMessageClear),
|
||||||
|
py::call_guard<py::gil_scoped_release>())
|
||||||
.def("sync", CallIfNotNull(&mujoco::Simulate::Sync),
|
.def("sync", CallIfNotNull(&mujoco::Simulate::Sync),
|
||||||
py::call_guard<py::gil_scoped_release>())
|
py::call_guard<py::gil_scoped_release>())
|
||||||
|
|
||||||
|
|||||||
@@ -174,9 +174,11 @@ def _reload(
|
|||||||
) -> Optional[Tuple[mujoco.MjModel, mujoco.MjData]]:
|
) -> Optional[Tuple[mujoco.MjModel, mujoco.MjData]]:
|
||||||
"""Internal function for reloading a model in the viewer."""
|
"""Internal function for reloading a model in the viewer."""
|
||||||
try:
|
try:
|
||||||
|
simulate.load_message('') # path is unknown at this point
|
||||||
load_tuple = loader()
|
load_tuple = loader()
|
||||||
except Exception as e: # pylint: disable=broad-except
|
except Exception as e: # pylint: disable=broad-except
|
||||||
simulate.load_error = str(e)
|
simulate.load_error = str(e)
|
||||||
|
simulate.load_message_clear()
|
||||||
else:
|
else:
|
||||||
m, d = load_tuple[:2]
|
m, d = load_tuple[:2]
|
||||||
|
|
||||||
|
|||||||
@@ -259,6 +259,7 @@ void PhysicsLoop(mj::Simulate& sim) {
|
|||||||
// run until asked to exit
|
// run until asked to exit
|
||||||
while (!sim.exitrequest.load()) {
|
while (!sim.exitrequest.load()) {
|
||||||
if (sim.droploadrequest.load()) {
|
if (sim.droploadrequest.load()) {
|
||||||
|
sim.LoadMessage(sim.dropfilename);
|
||||||
mjModel* mnew = LoadModel(sim.dropfilename, sim);
|
mjModel* mnew = LoadModel(sim.dropfilename, sim);
|
||||||
sim.droploadrequest.store(false);
|
sim.droploadrequest.store(false);
|
||||||
|
|
||||||
@@ -279,10 +280,14 @@ void PhysicsLoop(mj::Simulate& sim) {
|
|||||||
ctrlnoise = (mjtNum*) malloc(sizeof(mjtNum)*m->nu);
|
ctrlnoise = (mjtNum*) malloc(sizeof(mjtNum)*m->nu);
|
||||||
mju_zero(ctrlnoise, m->nu);
|
mju_zero(ctrlnoise, m->nu);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
sim.LoadMessageClear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sim.uiloadrequest.load()) {
|
if (sim.uiloadrequest.load()) {
|
||||||
sim.uiloadrequest.fetch_sub(1);
|
sim.uiloadrequest.fetch_sub(1);
|
||||||
|
sim.LoadMessage(sim.filename);
|
||||||
mjModel* mnew = LoadModel(sim.filename, sim);
|
mjModel* mnew = LoadModel(sim.filename, sim);
|
||||||
mjData* dnew = nullptr;
|
mjData* dnew = nullptr;
|
||||||
if (mnew) dnew = mj_makeData(mnew);
|
if (mnew) dnew = mj_makeData(mnew);
|
||||||
@@ -301,6 +306,9 @@ void PhysicsLoop(mj::Simulate& sim) {
|
|||||||
ctrlnoise = static_cast<mjtNum*>(malloc(sizeof(mjtNum)*m->nu));
|
ctrlnoise = static_cast<mjtNum*>(malloc(sizeof(mjtNum)*m->nu));
|
||||||
mju_zero(ctrlnoise, m->nu);
|
mju_zero(ctrlnoise, m->nu);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
sim.LoadMessageClear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// sleep for 1 ms or yield, to let main thread run
|
// sleep for 1 ms or yield, to let main thread run
|
||||||
@@ -404,6 +412,7 @@ void PhysicsLoop(mj::Simulate& sim) {
|
|||||||
void PhysicsThread(mj::Simulate* sim, const char* filename) {
|
void PhysicsThread(mj::Simulate* sim, const char* filename) {
|
||||||
// request loadmodel if file given (otherwise drag-and-drop)
|
// request loadmodel if file given (otherwise drag-and-drop)
|
||||||
if (filename != nullptr) {
|
if (filename != nullptr) {
|
||||||
|
sim->LoadMessage(filename);
|
||||||
m = LoadModel(filename, *sim);
|
m = LoadModel(filename, *sim);
|
||||||
if (m) d = mj_makeData(m);
|
if (m) d = mj_makeData(m);
|
||||||
if (d) {
|
if (d) {
|
||||||
@@ -414,6 +423,8 @@ void PhysicsThread(mj::Simulate* sim, const char* filename) {
|
|||||||
free(ctrlnoise);
|
free(ctrlnoise);
|
||||||
ctrlnoise = static_cast<mjtNum*>(malloc(sizeof(mjtNum)*m->nu));
|
ctrlnoise = static_cast<mjtNum*>(malloc(sizeof(mjtNum)*m->nu));
|
||||||
mju_zero(ctrlnoise, m->nu);
|
mju_zero(ctrlnoise, m->nu);
|
||||||
|
} else {
|
||||||
|
sim->LoadMessageClear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+20
-2
@@ -1977,6 +1977,15 @@ void Simulate::Sync() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//------------------------- Tell the render thread to load a file and wait -------------------------
|
//------------------------- Tell the render thread to load a file and wait -------------------------
|
||||||
|
void Simulate::LoadMessage(const char* displayed_filename) {
|
||||||
|
mju::strcpy_arr(this->filename, displayed_filename);
|
||||||
|
|
||||||
|
{
|
||||||
|
MutexLock lock(mtx);
|
||||||
|
this->loadrequest = 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Simulate::Load(mjModel* m, mjData* d, const char* displayed_filename) {
|
void Simulate::Load(mjModel* m, mjData* d, const char* displayed_filename) {
|
||||||
this->mnew_ = m;
|
this->mnew_ = m;
|
||||||
this->dnew_ = d;
|
this->dnew_ = d;
|
||||||
@@ -1993,6 +2002,15 @@ void Simulate::Load(mjModel* m, mjData* d, const char* displayed_filename) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Simulate::LoadMessageClear(void) {
|
||||||
|
{
|
||||||
|
MutexLock lock(mtx);
|
||||||
|
this->loadrequest = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//------------------------------------- load mjb or xml model --------------------------------------
|
//------------------------------------- load mjb or xml model --------------------------------------
|
||||||
void Simulate::LoadOnRenderThread() {
|
void Simulate::LoadOnRenderThread() {
|
||||||
this->m_ = this->mnew_;
|
this->m_ = this->mnew_;
|
||||||
@@ -2167,7 +2185,7 @@ void Simulate::Render() {
|
|||||||
|
|
||||||
// label
|
// label
|
||||||
if (this->loadrequest) {
|
if (this->loadrequest) {
|
||||||
mjr_overlay(mjFONT_BIG, mjGRID_TOPRIGHT, smallrect, "loading", nullptr,
|
mjr_overlay(mjFONT_BIG, mjGRID_TOPLEFT, smallrect, "loading", nullptr,
|
||||||
&this->platform_ui->mjr_context());
|
&this->platform_ui->mjr_context());
|
||||||
} else {
|
} else {
|
||||||
char intro_message[Simulate::kMaxFilenameLength];
|
char intro_message[Simulate::kMaxFilenameLength];
|
||||||
@@ -2450,7 +2468,7 @@ void Simulate::RenderLoop() {
|
|||||||
// load model (not on first pass, to show "loading" label)
|
// load model (not on first pass, to show "loading" label)
|
||||||
if (this->loadrequest==1) {
|
if (this->loadrequest==1) {
|
||||||
this->LoadOnRenderThread();
|
this->LoadOnRenderThread();
|
||||||
} else if (this->loadrequest>1) {
|
} else if (this->loadrequest == 2) {
|
||||||
this->loadrequest = 1;
|
this->loadrequest = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+10
-1
@@ -62,10 +62,18 @@ class Simulate {
|
|||||||
void UpdateMesh(int meshid);
|
void UpdateMesh(int meshid);
|
||||||
void UpdateTexture(int texid);
|
void UpdateTexture(int texid);
|
||||||
|
|
||||||
|
// Request that the Simulate UI display a "loading" message
|
||||||
|
// Called prior to Load or LoadMessageClear
|
||||||
|
void LoadMessage(const char* displayed_filename);
|
||||||
|
|
||||||
// Request that the Simulate UI thread render a new model
|
// Request that the Simulate UI thread render a new model
|
||||||
// optionally delete the old model and data when done
|
|
||||||
void Load(mjModel* m, mjData* d, const char* displayed_filename);
|
void Load(mjModel* m, mjData* d, const char* displayed_filename);
|
||||||
|
|
||||||
|
// Clear the loading message
|
||||||
|
// Can be called instead of Load to clear the message without
|
||||||
|
// requesting the UI load a model
|
||||||
|
void LoadMessageClear(void);
|
||||||
|
|
||||||
// functions below are used by the renderthread
|
// functions below are used by the renderthread
|
||||||
// load mjb or xml model that has been requested by load()
|
// load mjb or xml model that has been requested by load()
|
||||||
void LoadOnRenderThread();
|
void LoadOnRenderThread();
|
||||||
@@ -173,6 +181,7 @@ class Simulate {
|
|||||||
std::atomic_int uiloadrequest = 0;
|
std::atomic_int uiloadrequest = 0;
|
||||||
|
|
||||||
// loadrequest
|
// loadrequest
|
||||||
|
// 3: display a loading message
|
||||||
// 2: render thread asked to update its model
|
// 2: render thread asked to update its model
|
||||||
// 1: showing "loading" label, about to load
|
// 1: showing "loading" label, about to load
|
||||||
// 0: model loaded or no load requested.
|
// 0: model loaded or no load requested.
|
||||||
|
|||||||
Reference in New Issue
Block a user