OpenVDB 5.1.0

Namespaces | Functions
pyopenvdb.h File Reference

Glue functions for access to pyOpenVDB objects from C++ code. More...

`#include <boost/python.hpp>`
`#include "openvdb/Grid.h"`

Go to the source code of this file.

## Namespaces
| pyopenvdb

## Functions
boost::python::object | getPyObjectFromGrid (const openvdb::GridBase::Ptr &)
| Return a new Python object that holds the given OpenVDB grid. More...
openvdb::GridBase::Ptr | getGridFromPyObject (PyObject *)
| Return a pointer to the OpenVDB grid held by the given Python object. More...
openvdb::GridBase::Ptr | getGridFromPyObject (const boost::python::object &)
| Return a pointer to the OpenVDB grid held by the given Python object. More...

## Detailed Description

Glue functions for access to pyOpenVDB objects from C++ code.

Use these functions in your own Python function implementations to extract an OpenVDB grid from or wrap a grid in a `PyObject`. For example (using Boost.Python),

#include <openvdb.h>
#include <pyopenvdb.h>
#include <boost/python.hpp>
// Implementation of a Python function that processes pyOpenVDB grids
boost::python::object
processGrid(boost::python::object inObj)
{
boost::python::object outObj;
try {
// Extract an OpenVDB grid from the input argument.
if (openvdb::GridBase::Ptr grid =
pyopenvdb::getGridFromPyObject(inObj))
{
grid = grid->deepCopyGrid();
// Process the grid...
// Wrap the processed grid in a PyObject.
outObj = pyopenvdb::getPyObjectFromGrid(grid);
}
} catch (openvdb::TypeError& e) {
PyErr_Format(PyExc_TypeError, e.what());
boost::python::throw_error_already_set();
}
return outObj;
}
BOOST_PYTHON_MODULE(mymodule)
{
openvdb::initialize();
// Definition of a Python function that processes pyOpenVDB grids
boost::python::def(/*name=*/"processGrid", &processGrid, /*argname=*/"grid");
}

Then, from Python,

import openvdb
import mymodule
grid = openvdb.read('myGrid.vdb', 'MyGrid')
grid = mymodule.processGrid(grid)
openvdb.write('myProcessedGrid.vdb', [grid])