OpenVDB  9.0.1
pyopenvdb.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 //
4 /// @file pyopenvdb.h
5 ///
6 /// @brief Glue functions for access to pyOpenVDB objects from C++ code
7 /// @details Use these functions in your own Python function implementations
8 /// to extract an OpenVDB grid from or wrap a grid in a @c PyObject.
9 /// For example (using Boost.Python),
10 /// @code
11 /// #include <openvdb.h>
12 /// #include <pyopenvdb.h>
13 /// #include <boost/python.hpp>
14 ///
15 /// // Implementation of a Python function that processes pyOpenVDB grids
16 /// boost::python::object
17 /// processGrid(boost::python::object inObj)
18 /// {
19 /// boost::python::object outObj;
20 /// try {
21 /// // Extract an OpenVDB grid from the input argument.
22 /// if (openvdb::GridBase::Ptr grid =
23 /// pyopenvdb::getGridFromPyObject(inObj))
24 /// {
25 /// grid = grid->deepCopyGrid();
26 ///
27 /// // Process the grid...
28 ///
29 /// // Wrap the processed grid in a PyObject.
30 /// outObj = pyopenvdb::getPyObjectFromGrid(grid);
31 /// }
32 /// } catch (openvdb::TypeError& e) {
33 /// PyErr_Format(PyExc_TypeError, e.what());
34 /// boost::python::throw_error_already_set();
35 /// }
36 /// return outObj;
37 /// }
38 ///
39 /// BOOST_PYTHON_MODULE(mymodule)
40 /// {
41 /// openvdb::initialize();
42 ///
43 /// // Definition of a Python function that processes pyOpenVDB grids
44 /// boost::python::def(/*name=*/"processGrid", &processGrid, /*argname=*/"grid");
45 /// }
46 /// @endcode
47 /// Then, from Python,
48 /// @code
49 /// import openvdb
50 /// import mymodule
51 ///
52 /// grid = openvdb.read('myGrid.vdb', 'MyGrid')
53 /// grid = mymodule.processGrid(grid)
54 /// openvdb.write('myProcessedGrid.vdb', [grid])
55 /// @endcode
56 
57 #ifndef PYOPENVDB_HAS_BEEN_INCLUDED
58 #define PYOPENVDB_HAS_BEEN_INCLUDED
59 
60 #include <boost/python.hpp>
61 #include "openvdb/Grid.h"
62 
63 
64 namespace pyopenvdb {
65 
66 //@{
67 /// @brief Return a pointer to the OpenVDB grid held by the given Python object.
68 /// @throw openvdb::TypeError if the Python object is not one of the pyOpenVDB grid types.
69 /// (See the Python module's GridTypes global variable for the list of supported grid types.)
71 openvdb::GridBase::Ptr getGridFromPyObject(const boost::python::object&);
72 //@}
73 
74 /// @brief Return a new Python object that holds the given OpenVDB grid.
75 /// @return @c None if the given grid pointer is null.
76 /// @throw openvdb::TypeError if the grid is not of a supported type.
77 /// (See the Python module's GridTypes global variable for the list of supported grid types.)
78 boost::python::object getPyObjectFromGrid(const openvdb::GridBase::Ptr&);
79 
80 } // namespace pyopenvdb
81 
82 #endif // PYOPENVDB_HAS_BEEN_INCLUDED
Definition: pyopenvdb.h:64
SharedPtr< GridBase > Ptr
Definition: Grid.h:80
boost::python::object getPyObjectFromGrid(const openvdb::GridBase::Ptr &)
Return a new Python object that holds the given OpenVDB grid.
openvdb::GridBase::Ptr getGridFromPyObject(PyObject *)
Return a pointer to the OpenVDB grid held by the given Python object.