|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
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> |
16 | 24 |
|
| 25 | +#include <mujoco/experimental/usd/utils.h> |
17 | 26 | #include <mujoco/mujoco.h> |
18 | 27 | #include <pxr/usd/sdf/path.h> |
19 | 28 |
|
@@ -42,3 +51,56 @@ pxr::SdfPath GetUsdPrimPathUserValue(mjsElement* element) { |
42 | 51 |
|
43 | 52 | } // namespace usd |
44 | 53 | } // 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