Boost python hooks

Hello, I’m considering taking some of our more time consuming stage manipulation functionality currently in Python and making a C++ library with boost python hooks. I was wondering if anyone can point me to some resources or examples?

There are several examples on how to do it for an asset resolver, but I haven’t seen anything for a regular library.

The C++ code is very easy and straightforward for me but Boost isn’t my strong point

We use CMake so examples that include CMake files are most appreciated.

Thank you.

Hi @silviapalara ! Maybe have a look at utils.h/wrapUtils.cpp in usdviewq. We made a wrapper class, but really it’s just a few utility functions. usdUtils/wrapIntrospection.cpp also has examples of truly free functions being wrapped out, as part of a larger module.

Thank you, I did look at usdUtils and usdviewq but the CMake situation is a nightmare. Specifically I can’t get the part that creates the python library to work. The build of individual libraries is very tightly woven with the build of the whole of USD and I can’t pick out just what I need. After spending several days in that CMake jungle I was hoping for a simpler example.

@silviapalara, I find the examples from Wētā to be interesting and helpful. I wonder if any of these might provide some pointers to guide you?

Thank you very much for the link Nick, I was able to build and try the examples successfully! That should be enough to point me in the right direction with my work. Thank you so much for the help, so appreciated!

Maybe this will be useful for others in the future so I’m going to post what seems to be close to the bare minimum code and CMake file to do this:

file hello_ext.cpp

#include <boost/python.hpp>
#include "pxr/base/tf/type.h"
#include "pxr/usd/usd/prim.h"
#include "pxr/usd/usdGeom/pointBased.h"

#include <vector>
#include <iostream>

using namespace pxr;
using namespace std;

void scale_by(const UsdPrim &prim, float scale) {
    // Functionality goes here

  using namespace boost::python;
  def("scale_by", scale_by, args("prim"), args("scale"));

File CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

set(definition "NOMINMAX")
list(APPEND _PXR_CXX_DEFINITIONS "-D${definition}")


# Assume we already set a var or env var USD_ROOT
set(USD_LIB_DIR ${USD_ROOT}/lib)

# Assume we already set a var or env var PYTHON_ROOT
# Replace lib name according to your version of Python
set(PYTHON_LIBRARIES "python39.lib")

# Replace lib name and include dir according to your version of USD
set(Boost_LIBRARIES "${USD_LIB_DIR}/boost_python39-vc142-mt-x64-1_70.lib")
set(Boost_INCLUDE_DIRS "${USD_INCLUDE_DIRS}/boost-1_70")

# Build libraries without suffix "lib"

# Add a shared module
add_library(hello_ext MODULE hello_ext.cpp)

target_link_libraries(hello_ext ${USD_LIBRARIES})

# Copy the .so or .dll into a .pyd so Python can use it
add_custom_command(TARGET hello_ext POST_BUILD 

From python I used it by doing the following:

import os, sys
# Replace these paths with your Python and USD paths and with the location of your .pyd file

sys.path.insert(0, r'C:/usd/usd-py39/22.03.1/usd/lib/python')
sys.path.insert(0, r'C:/Users/silvia/work/test/build/Release')

from pxr import Usd, UsdGeom
from hello_ext import scale_by

This worked for me in Windows. I’m sure people who know more than me could cut it down even more.