Skip to content

Commit 225d215

Browse files
committed
VDBObject bindings : Adapt for OpenVDB 10.1
OpenVDB 10.1 switched from Boost to PyBind11 for its bindings, so we need to bind VDBObject differently based on which version of OpenVDB we're building for. But with the new PyBindConverter class, the new bindings actually come out simpler than the old ones.
1 parent 92d4b91 commit 225d215

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Fixes
1515
-----
1616

1717
- CompoundObject : Fixed crashes in Python bindings caused by passing `None` as a key.
18+
- VDBObject : Fixed Python bindings for OpenVDB 10.1.
1819

1920
10.5.4.2 (relative to 10.5.4.1)
2021
========

src/IECoreVDB/bindings/IECoreVDBModule.cpp

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,19 @@
4141

4242
#include "IECoreVDB/VDBObject.h"
4343

44+
// OpenVDB 10.0 and earlier used `boost::python` for its Python bindings, but
45+
// this was switched to PyBind11 in OpenVDB 10.1. We need to take a different
46+
// approach to binding VDBObject's grid accessors in each case.
47+
#if OPENVDB_LIBRARY_MAJOR_VERSION_NUMBER > 10 || OPENVDB_LIBRARY_MAJOR_VERSION_NUMBER == 10 && OPENVDB_LIBRARY_MINOR_VERSION_NUMBER >= 1
48+
#include "IECorePython/PyBindConverter.h"
49+
#define IECOREVDB_USE_PYBIND
50+
#endif
4451

4552
using namespace boost::python;
4653
using namespace IECoreVDB;
4754

55+
#ifndef IECOREVDB_USE_PYBIND
56+
4857
namespace
4958
{
5059

@@ -118,18 +127,6 @@ openvdb::GridBase::Ptr getGridFromPyObject( const boost::python::object &gridObj
118127

119128
} // namespace iepyopenvdb
120129

121-
122-
boost::python::list gridNames( VDBObject::Ptr vdbObject )
123-
{
124-
boost::python::list result;
125-
std::vector<std::string> names = vdbObject->gridNames();
126-
for( const auto &name : names )
127-
{
128-
result.append( name );
129-
}
130-
return result;
131-
}
132-
133130
boost::python::object findGrid( VDBObject::Ptr vdbObject, const std::string &gridName )
134131
{
135132
openvdb::GridBase::Ptr grid = vdbObject->findGrid( gridName );
@@ -151,17 +148,44 @@ void insertGrid( VDBObject::Ptr vdbObject, boost::python::object pyObject )
151148

152149
} // namespace
153150

151+
#endif // #ifndef IECOREVDB_USE_PYBIND
152+
153+
namespace
154+
{
155+
156+
boost::python::list gridNames( VDBObject::Ptr vdbObject )
157+
{
158+
boost::python::list result;
159+
std::vector<std::string> names = vdbObject->gridNames();
160+
for( const auto &name : names )
161+
{
162+
result.append( name );
163+
}
164+
return result;
165+
}
166+
167+
} // namespace
168+
154169
BOOST_PYTHON_MODULE( _IECoreVDB )
155170
{
156171

172+
#ifdef IECOREVDB_USE_PYBIND
173+
IECorePython::PyBindConverter<openvdb::GridBase::Ptr>::registerConverters();
174+
#endif
175+
157176
IECorePython::RunTimeTypedClass<VDBObject>()
158177
.def(init<const std::string &>())
159178
.def(init<>())
160179
.def("gridNames", &::gridNames)
161180
.def("metadata", &VDBObject::metadata)
162181
.def("removeGrid", &VDBObject::removeGrid)
182+
#ifdef IECOREVDB_USE_PYBIND
183+
.def( "findGrid", (openvdb::GridBase::Ptr (VDBObject::*)( const std::string &name ))&VDBObject::findGrid )
184+
.def( "insertGrid", &VDBObject::insertGrid )
185+
#else
163186
.def("findGrid", &::findGrid)
164187
.def("insertGrid", &::insertGrid)
188+
#endif
165189
.def("unmodifiedFromFile", &VDBObject::unmodifiedFromFile)
166190
.def("fileName", &VDBObject::fileName)
167191
;

0 commit comments

Comments
 (0)