Skip to content

Commit 44c8ef8

Browse files
havesscopybara-github
authored andcommitted
When building with USD enabled, add functionality that automatically registers the mujoco USD plugins.
PiperOrigin-RevId: 846767370 Change-Id: Ieedbe127353623c11150551c1409d340dbc57421
1 parent 3d9c195 commit 44c8ef8

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

src/experimental/usd/utils.cc

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include <mujoco/experimental/usd/utils.h>
15+
#include <cstddef>
16+
#include <string>
17+
18+
#if defined(_WIN32) || defined(__CYGWIN__)
19+
#include <windows.h>
20+
#else
21+
#include <dlfcn.h>
22+
#endif
23+
#include <pxr/base/plug/registry.h>
1624

25+
#include <mujoco/experimental/usd/utils.h>
1726
#include <mujoco/mujoco.h>
1827
#include <pxr/usd/sdf/path.h>
1928

@@ -42,3 +51,56 @@ pxr::SdfPath GetUsdPrimPathUserValue(mjsElement* element) {
4251

4352
} // namespace usd
4453
} // namespace mujoco
54+
55+
namespace {
56+
//Automatically register USD plugins when the library is loaded.
57+
//This avoids the need for users to manually set PXR_PLUGINPATH_NAME.
58+
[[maybe_unused]]
59+
bool RegisterMujocoUsdPlugins() {
60+
std::string plugin_path;
61+
62+
#if defined(_WIN32) || defined(__CYGWIN__)
63+
HMODULE hModule = NULL;
64+
if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
65+
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
66+
(LPCTSTR)&mujoco::usd::SetUsdPrimPathUserValue,
67+
&hModule)) {
68+
char path[MAX_PATH];
69+
if (GetModuleFileName(hModule, path, MAX_PATH)) {
70+
std::string lib_path(path);
71+
// Remove filename to get directory
72+
size_t last_slash = lib_path.find_last_of("\\/");
73+
if (last_slash != std::string::npos) {
74+
plugin_path = lib_path.substr(0, last_slash);
75+
}
76+
}
77+
}
78+
#else
79+
Dl_info info;
80+
if (dladdr((void*)&mujoco::usd::SetUsdPrimPathUserValue, &info)) {
81+
std::string lib_path(info.dli_fname);
82+
// Remove filename to get directory
83+
size_t last_slash = lib_path.find_last_of('/');
84+
if (last_slash != std::string::npos) {
85+
plugin_path = lib_path.substr(0, last_slash);
86+
}
87+
}
88+
#endif
89+
90+
if (!plugin_path.empty()) {
91+
#if defined(_WIN32) || defined(__CYGWIN__)
92+
std::string full_path =
93+
plugin_path + "\\" + "mujoco-usd-resources" + "\\**\\plugInfo.json";
94+
#else
95+
std::string full_path =
96+
plugin_path + "/" + "mujoco-usd-resources" + "/**/plugInfo.json";
97+
#endif
98+
pxr::PlugRegistry::GetInstance().RegisterPlugins(full_path);
99+
}
100+
return true;
101+
}
102+
103+
// Force registration at library load time.
104+
bool registered = RegisterMujocoUsdPlugins();
105+
106+
} // namespace

0 commit comments

Comments
 (0)