Skip to content

IECoreVDB : Python bindings renamed from pyopenvdb to openvdb in later versions #1455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: RB-10.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
10.5.x.x (relative to 10.5.13.0)
========

Features
--------

- NanoBindConverter : Added new class providing interoperability between Boost Python bindings and NanoBind bindings.

Improvements
------------

- IEcoreVDB : Support for the renamed python module `openvdb` in more recent versions of OpenVDB.

Build
-----

- SConstruct :
- Added `NANOBIND_INCLUDE_PATH` option.

10.5.13.0 (relative to 10.5.12.0)
=========
Expand Down
27 changes: 25 additions & 2 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,18 @@ o.Add(
"",
)

o.Add(
"NANOBIND_INCLUDE_PATH",
"The path to the nanobind include directory.",
"",
)

o.Add(
"NANOBIND_LIB_PATH",
"The path to the nanobind lib directory.",
"",
)

# Build options

o.Add(
Expand Down Expand Up @@ -2069,11 +2081,22 @@ vdbEnvPrepends = {
"LIBS" : ["openvdb$VDB_LIB_SUFFIX"],
"CXXFLAGS" : [
systemIncludeArgument, "$VDB_INCLUDE_PATH",
systemIncludeArgument, "$PYBIND11_INCLUDE_PATH",
]
}

vdbEnv.Prepend( **vdbEnvPrepends)
if env[ "PYBIND11_INCLUDE_PATH" ] != "" :
vdbEnvPrepends["CXXFLAGS"].append( [ systemIncludeArgument, "$PYBIND11_INCLUDE_PATH" ] )

if env[ "NANOBIND_INCLUDE_PATH" ] != "" :
vdbEnvPrepends["CXXFLAGS"].append( [ systemIncludeArgument, "$NANOBIND_INCLUDE_PATH" ] )

vdbEnv.Prepend( **vdbEnvPrepends )

if env[ "NANOBIND_LIB_PATH" ] != "" :
vdbEnvPrepends["LIBPATH"].append( "$NANOBIND_LIB_PATH" )

if env[ "NANOBIND_INCLUDE_PATH" ] != "" :
vdbEnvPrepends["LIBS"].append( "nanobind-static" )

vdbPythonModuleEnv = corePythonModuleEnv.Clone( **vdbEnvSets )
vdbPythonModuleEnv.Prepend( **vdbEnvPrepends )
Expand Down
103 changes: 103 additions & 0 deletions include/IECorePython/NanoBindConverter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

//////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2025, Alex Fuller. All rights reserved.
// Copyright (c) 2024, Cinesite VFX Ltd. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above
// copyright notice, this list of conditions and the following
// disclaimer.
//
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided with
// the distribution.
//
// * Neither the name of John Haddon nor the names of
// any other contributors to this software may be used to endorse or
// promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////

#ifndef IECOREPYTHON_NANOBINDCONVERTER_H
#define IECOREPYTHON_NANOBINDCONVERTER_H

#include "boost/python.hpp"

#include "nanobind/nanobind.h"

namespace IECorePython
{

// Registers `boost::python` converters for types
// wrapped using nanobind.
template<typename T>
struct NanoBindConverter
{

static void registerConverters()
{
boost::python::to_python_converter<T, ToNanoBind>();
boost::python::converter::registry::push_back(
&FromNanoBind::convertible,
&FromNanoBind::construct,
boost::python::type_id<T>()
);
}

private :

struct ToNanoBind
{
static PyObject *convert( const T &t )
{
nanobind::object o = nanobind::cast( t );
Py_INCREF( o.ptr() );
return o.ptr();
}
};

struct FromNanoBind
{

static void *convertible( PyObject *object )
{
nanobind::handle handle( object );
return nanobind::cast<T>( handle ) ? object : nullptr;
}

static void construct( PyObject *object, boost::python::converter::rvalue_from_python_stage1_data *data )
{
void *storage = ( ( boost::python::converter::rvalue_from_python_storage<T> * ) data )->storage.bytes;
T *t = new( storage ) T;
data->convertible = storage;

nanobind::handle handle( object );
*t = nanobind::cast<T>( handle );
}

};

};

} // namespace IECorePython

#endif // IECOREPYTHON_NANOBINDCONVERTER_H

5 changes: 4 additions & 1 deletion python/IECoreVDB/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@
# we use the warnings module to suppress these during the import
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import pyopenvdb
try:
import pyopenvdb
except:
import openvdb

from ._IECoreVDB import *

Expand Down
16 changes: 11 additions & 5 deletions src/IECoreVDB/bindings/IECoreVDBModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,19 @@
// OpenVDB 10.0 and earlier used `boost::python` for its Python bindings, but
// this was switched to PyBind11 in OpenVDB 10.1. We need to take a different
// approach to binding VDBObject's grid accessors in each case.
#if OPENVDB_LIBRARY_MAJOR_VERSION_NUMBER > 10 || OPENVDB_LIBRARY_MAJOR_VERSION_NUMBER == 10 && OPENVDB_LIBRARY_MINOR_VERSION_NUMBER >= 1
// For OpenVDB 12 they yet again changed bindings to nanobind instead.
#if OPENVDB_LIBRARY_MAJOR_VERSION_NUMBER == 11 || OPENVDB_LIBRARY_MAJOR_VERSION_NUMBER == 10 && OPENVDB_LIBRARY_MINOR_VERSION_NUMBER >= 1
#include "IECorePython/PyBindConverter.h"
#define IECOREVDB_USE_PYBIND
#elif OPENVDB_LIBRARY_MAJOR_VERSION_NUMBER > 11
#include "IECorePython/NanoBindConverter.h"
#define IECOREVDB_USE_NANOBIND
#endif

using namespace boost::python;
using namespace IECoreVDB;

#ifndef IECOREVDB_USE_PYBIND
#if !defined( IECOREVDB_USE_PYBIND ) && !defined( IECOREVDB_USE_NANOBIND )

namespace
{
Expand Down Expand Up @@ -148,7 +152,7 @@ void insertGrid( VDBObject::Ptr vdbObject, boost::python::object pyObject )

} // namespace

#endif // #ifndef IECOREVDB_USE_PYBIND
#endif // #if !defined( IECOREVDB_USE_PYBIND ) && !defined( IECOREVDB_USE_NANOBIND )

namespace
{
Expand All @@ -169,8 +173,10 @@ boost::python::list gridNames( VDBObject::Ptr vdbObject )
BOOST_PYTHON_MODULE( _IECoreVDB )
{

#ifdef IECOREVDB_USE_PYBIND
#if defined( IECOREVDB_USE_PYBIND )
IECorePython::PyBindConverter<openvdb::GridBase::Ptr>::registerConverters();
#elif defined( IECOREVDB_USE_NANOBIND )
IECorePython::NanoBindConverter<openvdb::GridBase::Ptr>::registerConverters();
#endif

IECorePython::RunTimeTypedClass<VDBObject>()
Expand All @@ -179,7 +185,7 @@ BOOST_PYTHON_MODULE( _IECoreVDB )
.def("gridNames", &::gridNames)
.def("metadata", &VDBObject::metadata)
.def("removeGrid", &VDBObject::removeGrid)
#ifdef IECOREVDB_USE_PYBIND
#if defined( IECOREVDB_USE_PYBIND ) || defined( IECOREVDB_USE_NANOBIND )
.def( "findGrid", (openvdb::GridBase::Ptr (VDBObject::*)( const std::string &name ))&VDBObject::findGrid )
.def( "insertGrid", &VDBObject::insertGrid )
#else
Expand Down
Loading