Skip to content

Commit ab63a7e

Browse files
havesscopybara-github
authored andcommitted
Add CMakeLists for usdMjcf file format plugin.
This adds an option MUJOCO_BUILD_USD_PLUGINS to the top level CMakeLists which allows us to build the usdMjcf file format plugin with the rest of mujoco. In this first version, we allow building against either standalone USD or against Houdini (DCC) since it has it's own fork of USD. When building against standalone USD we also build the file format tests. PiperOrigin-RevId: 753969974 Change-Id: If691eecbddf45d18b7c4ff76f7650999d42e99c5
1 parent 0265653 commit ab63a7e

File tree

8 files changed

+207
-8
lines changed

8 files changed

+207
-8
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ jobs:
179179
cp lib/libactuator.* ${{ matrix.tmpdir }}/mujoco_install/mujoco_plugin &&
180180
cp lib/libelasticity.* ${{ matrix.tmpdir }}/mujoco_install/mujoco_plugin &&
181181
cp lib/libsensor.* ${{ matrix.tmpdir }}/mujoco_install/mujoco_plugin &&
182-
cp lib/libsdf.* ${{ matrix.tmpdir }}/mujoco_install/mujoco_plugin
182+
cp lib/libsdf_plugin.* ${{ matrix.tmpdir }}/mujoco_install/mujoco_plugin
183183
- name: Copy plugins (Windows)
184184
if: ${{ runner.os == 'Windows' }}
185185
working-directory: build

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ option(MUJOCO_BUILD_EXAMPLES "Build samples for MuJoCo" ON)
4242
option(MUJOCO_BUILD_SIMULATE "Build simulate library for MuJoCo" ON)
4343
option(MUJOCO_BUILD_TESTS "Build tests for MuJoCo" ON)
4444
option(MUJOCO_TEST_PYTHON_UTIL "Build and test utility libraries for Python bindings" ON)
45+
option(MUJOCO_BUILD_USD_PLUGINS "Build OpenUSD plugins" OFF)
46+
47+
# USD libs to compile against.
48+
set(MUJOCO_USD_ALLOWED_TARGET_VALUES "USD" "Houdini")
49+
set(MUJOCO_USD_TARGET "USD" CACHE STRING "Select the USD target for the project.")
50+
set_property(CACHE MUJOCO_USD_TARGET
51+
PROPERTY STRINGS ${MUJOCO_USD_ALLOWED_TARGET_VALUES}
52+
)
4553

4654
if(APPLE AND (MUJOCO_BUILD_EXAMPLES OR MUJOCO_BUILD_SIMULATE))
4755
enable_language(OBJC)
@@ -181,6 +189,10 @@ if(MUJOCO_BUILD_EXAMPLES)
181189
add_subdirectory(sample)
182190
endif()
183191

192+
if(MUJOCO_BUILD_USD_PLUGINS)
193+
add_subdirectory(src/experimental/usd/plugins)
194+
endif()
195+
184196
if(BUILD_TESTING AND MUJOCO_BUILD_TESTS)
185197
enable_testing()
186198
add_subdirectory(test)

plugin/sdf/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ set(MUJOCO_SDF_SRCS
3434
torus.h
3535
)
3636

37-
add_library(sdf SHARED)
38-
target_sources(sdf PRIVATE ${MUJOCO_SDF_SRCS})
39-
target_include_directories(sdf PRIVATE ${MUJOCO_SDF_INCLUDE})
40-
target_link_libraries(sdf PRIVATE mujoco SdfLib)
37+
add_library(sdf_plugin SHARED)
38+
target_sources(sdf_plugin PRIVATE ${MUJOCO_SDF_SRCS})
39+
target_include_directories(sdf_plugin PRIVATE ${MUJOCO_SDF_INCLUDE})
40+
target_link_libraries(sdf_plugin PRIVATE mujoco SdfLib)
4141
target_compile_options(
42-
sdf
42+
sdf_plugin
4343
PRIVATE ${AVX_COMPILE_OPTIONS}
4444
${MUJOCO_MACOS_COMPILE_OPTIONS}
4545
${EXTRA_COMPILE_OPTIONS}
4646
${MUJOCO_CXX_FLAGS}
4747
)
4848
target_link_options(
49-
sdf
49+
sdf_plugin
5050
PRIVATE
5151
${MUJOCO_MACOS_LINK_OPTIONS}
5252
${EXTRA_LINK_OPTIONS}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Copyright 2025 DeepMind Technologies Limited
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
# Plugin target name (used for library and plugInfo.json)
17+
set(MJCF_PLUGIN_TARGET_NAME usdMjcf)
18+
19+
add_library(${MJCF_PLUGIN_TARGET_NAME} SHARED
20+
mjcf/mjcf_file_format.cc
21+
mjcf/mjcf_file_format.h
22+
mjcf/mujoco_to_usd.cc
23+
mjcf/mujoco_to_usd.h
24+
mjcf/utils.cc
25+
mjcf/utils.h
26+
)
27+
28+
# We need to set the visibility to default until core type symbol visibility
29+
# is resolved in OpenUSD https://github.com/PixarAnimationStudios/OpenUSD/issues/1475
30+
# Otherwise we will run into issues during composition on MacOS due to std::type_info
31+
# comparisons failing for pxr::TfTokenVector and the like that we place in SdfAbstractData.
32+
set_target_properties(${MJCF_PLUGIN_TARGET_NAME} PROPERTIES
33+
OUTPUT_NAME ${MJCF_PLUGIN_TARGET_NAME}
34+
CXX_VISIBILITY_PRESET default
35+
)
36+
37+
target_include_directories(${MJCF_PLUGIN_TARGET_NAME} PRIVATE
38+
"${CMAKE_CURRENT_SOURCE_DIR}"
39+
)
40+
41+
if (MUJOCO_USD_TARGET STREQUAL "USD")
42+
find_package(pxr REQUIRED)
43+
44+
# --- Link Dependencies ---
45+
# Link against the necessary OpenUSD components
46+
target_link_libraries(${MJCF_PLUGIN_TARGET_NAME} PRIVATE
47+
usd
48+
ar
49+
kind
50+
tf
51+
gf
52+
vt
53+
usdShade
54+
usdLux
55+
usdGeom
56+
usdImaging
57+
usdPhysics
58+
mujoco
59+
tinyxml2
60+
)
61+
elseif (MUJOCO_USD_TARGET STREQUAL "Houdini")
62+
63+
if (NOT DEFINED ENV{HFS})
64+
message(FATAL_ERROR "Environment variable 'HFS' is not defined: $ENV{HFS}. Please run houdini_setup.")
65+
endif()
66+
67+
# In Houdini, the Houdini package we would typically use via find_package
68+
# does not have all the USD dependencies that we need (namely UsdPhysics)
69+
# so we need to manually link all the required libraries.
70+
71+
set(HFS_ENV "$ENV{HFS}")
72+
set(HOUDINI_LIBS "${HFS_ENV}/../Libraries")
73+
get_filename_component(HOUDINI_LIBS "${HOUDINI_LIBS}" ABSOLUTE) # Normalize the path
74+
target_link_directories(${MJCF_PLUGIN_TARGET_NAME} PRIVATE ${HOUDINI_LIBS})
75+
76+
target_include_directories(${MJCF_PLUGIN_TARGET_NAME} PRIVATE
77+
"${HFS_ENV}/toolkit/include"
78+
"${HFS_ENV}/toolkit/include/python3.11"
79+
)
80+
81+
# Assume everyone using Houdini on 3.11 for now.
82+
set(USD_MJCF_PYTHON_LIB python3.11)
83+
set(USD_MJCF_PYTHON_LIB_NUMBER python311)
84+
set(PYTHON_LIB "${HFS_ENV}/Frameworks/Python.framework/Versions/3.11/Python")
85+
set(PXR_LIB_PREFIX "pxr_")
86+
87+
# --- Link Dependencies ---
88+
# Link against the necessary OpenUSD components
89+
target_link_libraries(${MJCF_PLUGIN_TARGET_NAME} PRIVATE
90+
${PXR_LIB_PREFIX}usd
91+
${PXR_LIB_PREFIX}ar
92+
${PXR_LIB_PREFIX}kind
93+
${PXR_LIB_PREFIX}tf
94+
${PXR_LIB_PREFIX}gf
95+
${PXR_LIB_PREFIX}vt
96+
${PXR_LIB_PREFIX}sdf
97+
${PXR_LIB_PREFIX}usdShade
98+
${PXR_LIB_PREFIX}usdLux
99+
${PXR_LIB_PREFIX}usdGeom
100+
${PXR_LIB_PREFIX}usdImaging
101+
${PXR_LIB_PREFIX}usdPhysics
102+
tbb
103+
hboost_${USD_MJCF_PYTHON_LIB_NUMBER}
104+
${PYTHON_LIB}
105+
mujoco
106+
tinyxml2
107+
)
108+
endif()
109+
110+
111+
# --- Generate plugInfo.json ---
112+
if(CMAKE_SHARED_LIBRARY_PREFIX)
113+
set(LIB_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX}) # Usually "lib" on Unix
114+
else()
115+
set(LIB_PREFIX "")
116+
endif()
117+
set(PLUG_INFO_LIBRARY_PATH "${LIB_PREFIX}${MJCF_PLUGIN_TARGET_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}")
118+
119+
# --- Installation ---
120+
121+
set(USD_PLUGIN_INSTALL_DIR_LIB ${CMAKE_INSTALL_LIBDIR}/usdMjcf)
122+
123+
message(STATUS "Copying plugInfo.json to ${CMAKE_BINARY_DIR}/${USD_PLUGIN_INSTALL_DIR_LIB}/plugInfo.json")
124+
configure_file(
125+
mjcf/plugInfo.json
126+
${CMAKE_BINARY_DIR}/${USD_PLUGIN_INSTALL_DIR_LIB}/plugInfo.json
127+
)
128+
129+
install(FILES ${CMAKE_BINARY_DIR}/${USD_PLUGIN_INSTALL_DIR_LIB}/plugInfo.json DESTINATION ${USD_PLUGIN_INSTALL_DIR_LIB})
130+
131+
# Install shared lib and plugInfo to same location for simplicity.
132+
install(TARGETS ${MJCF_PLUGIN_TARGET_NAME}
133+
LIBRARY DESTINATION ${USD_PLUGIN_INSTALL_DIR_LIB}
134+
)
135+
136+
message(STATUS "USD MJCF Plugin will be installed to: ${CMAKE_INSTALL_PREFIX}/${USD_PLUGIN_INSTALL_DIR_LIB}")
137+
message(STATUS "Make sure PXR_PLUGINPATH_NAME includes: ${CMAKE_INSTALL_PREFIX}/${USD_PLUGIN_INSTALL_DIR_LIB}")

src/experimental/usd/plugins/mjcf/plugInfo.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
}
1616
}
1717
},
18-
"LibraryPath": "",
18+
"LibraryPath": "@PLUG_INFO_LIBRARY_PATH@",
1919
"Name": "usdMjcf",
2020
"ResourcePath": "",
2121
"Root": ".",

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,4 @@ add_subdirectory(user)
100100
add_subdirectory(xml)
101101
add_subdirectory(plugin/elasticity)
102102
add_subdirectory(plugin/actuator)
103+
add_subdirectory(experimental)

test/experimental/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2021 DeepMind Technologies Limited
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
if(MUJOCO_BUILD_USD_PLUGINS AND MUJOCO_USD_TARGET STREQUAL "USD")
16+
add_subdirectory(usd/plugins/mjcf)
17+
endif()
18+
19+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2025 DeepMind Technologies Limited
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
find_package(pxr REQUIRED)
16+
17+
add_library(usd_fixture STATIC fixture.h fixture.cc)
18+
target_include_directories(usd_fixture PUBLIC ${MUJOCO_TEST_INCLUDE})
19+
target_compile_definitions(usd_fixture PUBLIC MJSTATIC)
20+
21+
target_link_libraries(
22+
usd_fixture
23+
PUBLIC usd
24+
tf
25+
gtest
26+
gmock
27+
mujoco
28+
)
29+
30+
mujoco_test(mjcf_file_format_test ADDITIONAL_LINK_LIBRARIES usd tf usdGeom usdImaging usdPhysics usdShade usd_fixture)

0 commit comments

Comments
 (0)