Skip to content

Commit c80d78d

Browse files
authored
make SDF to USD a separate component of sdformat (#817)
Signed-off-by: Ashton Larkin <42042756+adlarkin@users.noreply.github.com> Co-authored-by: ahcorde <ahcorde@gmail.com>
1 parent 0a15990 commit c80d78d

File tree

14 files changed

+670
-1
lines changed

14 files changed

+670
-1
lines changed

.github/ci/before_cmake.sh

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/sh -l
2+
3+
set -x
4+
5+
BUILD_DIR=`pwd`
6+
7+
cd /tmp
8+
9+
# check that we can compile USD from sources (only Focal)
10+
mkdir cmake_test
11+
cd cmake_test
12+
13+
echo "cmake_minimum_required(VERSION 3.12)" > CMakeLists.txt
14+
15+
return_code=0
16+
cmake . || return_code=$(($return_code + $?))
17+
if [ $return_code -eq 0 ]
18+
then
19+
# compile USD from sources
20+
cd /tmp
21+
mkdir usd_binaries
22+
cd usd_binaries
23+
24+
apt-get install libboost-all-dev libtbb-dev p7zip-full -y
25+
26+
wget https://github.com/PixarAnimationStudios/USD/archive/refs/tags/v21.11.zip
27+
unzip v21.11.zip
28+
cd USD-21.11
29+
mkdir build
30+
cd build
31+
32+
cmake -DCMAKE_INSTALL_PREFIX="/tmp/USD" -DCMAKE_PREFIX_PATH="/tmp/USD" \
33+
-DCMAKE_BUILD_TYPE=Release \
34+
-DPXR_PREFER_SAFETY_OVER_SPEED=ON \
35+
-DPXR_ENABLE_PYTHON_SUPPORT=OFF \
36+
-DBUILD_SHARED_LIBS=ON \
37+
-DTBB_USE_DEBUG_BUILD=OFF \
38+
-DPXR_BUILD_DOCUMENTATION=OFF \
39+
-DPXR_BUILD_TESTS=OFF \
40+
-DPXR_BUILD_EXAMPLES=OFF \
41+
-DPXR_BUILD_TUTORIALS=OFF \
42+
-DPXR_BUILD_USD_TOOLS=OFF \
43+
-DPXR_BUILD_IMAGING=OFF \
44+
-DPXR_BUILD_USD_IMAGING=OFF \
45+
-DPXR_BUILD_USDVIEW=OFF \
46+
-DPXR_BUILD_ALEMBIC_PLUGIN=OFF \
47+
-DPXR_BUILD_DRACO_PLUGIN=OFF \
48+
-DPXR_ENABLE_MATERIALX_SUPPORT=OFF \
49+
-DBoost_INCLUDE_DIR=/usr/include \
50+
-DBoost_NO_BOOST_CMAKE=FALSE \
51+
..
52+
53+
make -j$(nproc) install
54+
fi
55+
56+
cd $BUILD_DIR

.github/ci/packages.apt

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
libignition-cmake2-dev
2+
libignition-common4-dev
23
libignition-math6-dev
34
libignition-tools-dev
45
libignition-utils1-dev
6+
libignition-utils1-cli-dev
57
libtinyxml2-dev
68
liburdfdom-dev
79
libxml2-utils

.github/workflows/ci.yml

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ jobs:
1818
runs-on: ubuntu-latest
1919
name: Ubuntu Focal CI
2020
steps:
21+
- name: Set env
22+
run: |
23+
echo "PATH=$PATH:/tmp/USD/bin" >> $GITHUB_ENV
24+
echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/tmp/USD/lib" >> $GITHUB_ENV
25+
echo "CMAKE_PREFIX_PATH=$CMAKE_PREFIX_PATH:/tmp/USD" >> $GITHUB_ENV
2126
- name: Checkout
2227
uses: actions/checkout@v2
2328
- name: Compile and test

CMakeLists.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,18 @@ if (BUILD_SDF)
109109
ign_find_package(ignition-utils1 VERSION REQUIRED)
110110
set(IGN_UTILS_VER ${ignition-utils1_VERSION_MAJOR})
111111

112+
########################################
113+
# Find ignition common
114+
ign_find_package(ignition-common4 COMPONENTS graphics REQUIRED_BY usd)
115+
set(IGN_COMMON_VER ${ignition-common4_VERSION_MAJOR})
116+
117+
########################################
118+
# Find PXR
119+
ign_find_package(pxr QUIET REQUIRED_BY usd PKGCONFIG pxr)
120+
121+
ign_configure_build(HIDE_SYMBOLS_BY_DEFAULT QUIT_IF_BUILD_ERRORS
122+
COMPONENTS usd)
112123

113-
ign_configure_build(HIDE_SYMBOLS_BY_DEFAULT QUIT_IF_BUILD_ERRORS)
114124
ign_create_packages()
115125

116126
add_subdirectory(sdf)

examples/usdConverter/README.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Converting between SDF and USD
2+
3+
This example shows how a world in a SDF file can be converted to [USD](https://graphics.pixar.com/usd/release/index.html).
4+
5+
## Requirements
6+
7+
You will need all of the dependencies for sdformat, along with the following additional dependencies:
8+
* USD: [installation instructions](https://github.com/PixarAnimationStudios/USD/blob/release/README.md#getting-and-building-the-code)
9+
* [ignition-common4](https://github.com/ignitionrobotics/ign-common)
10+
* [ignition-utils1 (including the CLI component)](https://github.com/ignitionrobotics/ign-utils)
11+
12+
## Setup
13+
14+
Build sdformat. The steps below follow a traditional cmake build, but sdformat
15+
can also be built with [colcon](https://colcon.readthedocs.io/en/released/index.html):
16+
```bash
17+
git clone https://github.com/ignitionrobotics/sdformat.git
18+
cd sdformat
19+
mkdir build
20+
cd build
21+
cmake ..
22+
make
23+
```
24+
25+
You should now have an executable named `sdf2usd` in the `sdformat/build/bin` directory.
26+
This executable can be used to convert a SDF world file to a USD file.
27+
To see how the executable works, run the following command from the `sdformat/build/bin` directory:
28+
```bash
29+
./sdf2usd -h
30+
```
31+
32+
To convert [shapes_world.sdf](https://github.com/ignitionrobotics/sdformat/blob/sdf12/test/sdf/shapes_world.sdf) to its USD representation as a file called `shapes.usd`, run the following commands:
33+
34+
```bash
35+
wget https://raw.githubusercontent.com/ignitionrobotics/sdformat/sdf12/test/sdf/shapes_world.sdf
36+
./sdf2usd shapes_world.sdf shapes.usd
37+
```
38+
39+
You can now view the contents of the generated USD file with `usdcat` (this should have been installed when setting up the USD dependency):
40+
```bash
41+
usdcat shapes.usd
42+
```
43+
44+
To see the visual representation of the USD world, run `usdview` (this should have also been installed when setting up the USD dependency):
45+
```bash
46+
usdview shapes.usd
47+
```
48+
49+
### Note about building with colcon
50+
You may need to add the USD library path to your `LD_LIBRARY_PATH` environment variable after sourcing the colcon workspace.
51+
If the USD library path is not a part of `LD_LIBRARY_PATH`, you will probably see the following error when running the `sdf2usd` executable:
52+
```bash
53+
sdf2usd: error while loading shared libraries: libusd_usd.so: cannot open shared object file: No such file or directory
54+
```
55+
The typical USD library path is `<usd_installation_path>/lib`.
56+
So, if you installed USD at `/usr/local/USD`, the following command on Linux properly updates the `LD_LIBRARY_PATH` environment variable:
57+
```bash
58+
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/USD/lib
59+
```
60+
61+
Another thing to note if building with colcon is that after sourcing the workspace with sdformat,
62+
the `sdf2usd` executable can be run without having to go to the `sdformat/build/bin` directory.
63+
So, instead of going to that directory and running `./sdf2usd ...`, you should be able to run `sdf2usd ...` from anywhere.

test/test_utils.hh

+22
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
#define SDF_TEST_UTILS_HH_
1919

2020
#include <ostream>
21+
#include <string>
22+
2123
#include "sdf/Console.hh"
24+
#include "sdf/Root.hh"
2225

2326
namespace sdf
2427
{
@@ -104,6 +107,25 @@ class RedirectConsoleStream
104107
private: sdf::Console::ConsoleStream oldStream;
105108
};
106109

110+
/// \brief Load an SDF file into a sdf::Root object
111+
/// \param[in] _fileName The name of the file to load
112+
/// \param[in] _root The sdf::Root object to load the file into
113+
/// \return True if a file named _fileName was successfully loaded into
114+
/// _root. False otherwise
115+
bool LoadSdfFile(const std::string &_fileName, sdf::Root &_root)
116+
{
117+
auto errors = _root.Load(_fileName);
118+
if (!errors.empty())
119+
{
120+
std::cerr << "Errors encountered:\n";
121+
for (const auto &e : errors)
122+
std::cerr << e << "\n";
123+
return false;
124+
}
125+
126+
return true;
127+
}
128+
107129
} // namespace testing
108130
} // namespace sdf
109131

usd/include/sdf/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ign_install_all_headers(COMPONENT usd)
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (C) 2022 Open Source Robotics Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
#ifndef SDF_USD_SDF_PARSER_WORLD_HH_
19+
#define SDF_USD_SDF_PARSER_WORLD_HH_
20+
21+
#include <string>
22+
23+
// TODO(ahcorde):this is to remove deprecated "warnings" in usd, these warnings
24+
// are reported using #pragma message so normal diagnostic flags cannot remove
25+
// them. This workaround requires this block to be used whenever usd is
26+
// included.
27+
#pragma push_macro ("__DEPRECATED")
28+
#undef __DEPRECATED
29+
#include <pxr/usd/usd/stage.h>
30+
#pragma pop_macro ("__DEPRECATED")
31+
32+
#include "sdf/config.hh"
33+
#include "sdf/system_util.hh"
34+
#include "sdf/World.hh"
35+
36+
namespace sdf
37+
{
38+
// Inline bracke to help doxygen filtering.
39+
inline namespace SDF_VERSION_NAMESPACE {
40+
//
41+
namespace usd
42+
{
43+
/// \brief Parse an SDF world into a USD stage.
44+
/// \param[in] _world The SDF world to parse.
45+
/// \param[in] _stage The stage that should contain the USD representation
46+
/// of _world. It must be initialized first
47+
/// \param[in] _path The USD path of the parsed world in _stage, which must be
48+
/// a valid USD path.
49+
/// \return Errors, which is a vector of Error objects. Each Error includes
50+
/// an error code and message. An empty vector indicates no error.
51+
sdf::Errors SDFORMAT_VISIBLE ParseSdfWorld(const sdf::World &_world,
52+
pxr::UsdStageRefPtr &_stage, const std::string &_path);
53+
}
54+
}
55+
}
56+
57+
#endif

usd/src/CMakeLists.txt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
set(sources
2+
sdf_parser/World.cc
3+
)
4+
5+
ign_add_component(usd SOURCES ${sources} GET_TARGET_NAME usd_target)
6+
7+
target_include_directories(${usd_target}
8+
PUBLIC
9+
${PXR_INCLUDE_DIRS}
10+
)
11+
12+
target_link_libraries(${usd_target}
13+
PUBLIC
14+
ignition-common${IGN_COMMON_VER}::ignition-common${IGN_COMMON_VER}
15+
${PXR_LIBRARIES}
16+
)
17+
18+
set(gtest_sources
19+
sdf_parser/sdf2usd_TEST.cc
20+
sdf_parser/World_Sdf2Usd_TEST.cc
21+
)
22+
23+
# Build the unit tests
24+
ign_build_tests(
25+
TYPE UNIT
26+
SOURCES ${gtest_sources}
27+
LIB_DEPS ${usd_target} ignition-cmake${IGN_CMAKE_VER}::utilities
28+
INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/test
29+
)
30+
31+
add_subdirectory(cmd)

usd/src/cmd/CMakeLists.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
if(TARGET ${usd_target})
2+
add_executable(sdf2usd
3+
sdf2usd.cc
4+
)
5+
6+
target_link_libraries(sdf2usd
7+
PUBLIC
8+
ignition-utils${IGN_UTILS_VER}::ignition-utils${IGN_UTILS_VER}
9+
${usd_target}
10+
)
11+
12+
install(
13+
TARGETS
14+
sdf2usd
15+
DESTINATION
16+
${BIN_INSTALL_DIR}
17+
)
18+
endif()

0 commit comments

Comments
 (0)