Install timers by default in Python bindings

PiperOrigin-RevId: 744663306
Change-Id: Ia322ace94d1f269ff9b0275567b2e453acc94843
This commit is contained in:
Yuval Tassa
2025-04-07 03:53:42 -07:00
committed by Copybara-Service
parent 26c14c500e
commit 93251f07d9
2 changed files with 33 additions and 0 deletions
+19
View File
@@ -18,6 +18,7 @@
#include <algorithm>
#include <array>
#include <chrono> // NOLINT(build/c++11)
#include <cctype>
#include <cstddef>
#include <cstdint>
@@ -553,6 +554,16 @@ MjDataWrapper* MjDataWrapper::FromRawPointer(raw::MjData* m) noexcept {
}
}
namespace {
// default timer callback (seconds)
mjtNum GetTime() {
using Clock = std::chrono::steady_clock;
using Seconds = std::chrono::duration<mjtNum>;
static const Clock::time_point tm_start = Clock::now();
return Seconds(Clock::now() - tm_start).count();
}
} // namespace
MjDataWrapper::MjWrapper(MjModelWrapper* model)
: WrapperBase(InterceptMjErrors(mj_makeData)(model->get()),
&MjDataCapsuleDestructor),
@@ -582,6 +593,14 @@ MjDataWrapper::MjWrapper(MjModelWrapper* model)
throw UnexpectedError(
"MjDataRawPointerMap already contains this raw mjData*");
}
// install default timer if not already installed
{
py::gil_scoped_acquire gil;
if (!mjcb_time) {
mjcb_time = GetTime;
}
}
}
MjDataWrapper::MjWrapper(const MjDataWrapper& other)