1#ifndef _BBM_PYTHON_INTERPRETER_H_
2#define _BBM_PYTHON_INTERPRETER_H_
5 #error Fatal Error: Python Support not enabled for BBM (-DBBM_PYTHON=ON)
10#include <pybind11/embed.h>
11namespace py = pybind11;
40 py::initialize_interpreter();
41 _scope = py::module::import(
"__main__").attr(
"__dict__");
44 std::string import_string = std::string(
"from ") + std::string(
BBM_STRINGIFY(BBM_NAME)) + std::string(
"_") + std::string(
get_config<CONF>::name) + std::string(
" import *");
45 py::exec(import_string,
_scope);
52 py::finalize_interpreter();
70 template<
typename RET_TYPE>
71 RET_TYPE
capture(
const std::string& prog)
const
74 return py::eval(prog,
_scope).cast<RET_TYPE>();
75 }
catch(...) {
throw std::runtime_error(
"BBM: python expression \"" + prog +
"\" did not return a valid BBM Bsdf."); }
91 void execute(
const std::string& prog)
const
95 }
catch(...) {
throw std::runtime_error(
"BBM: failed to execute python expression \"" + prog +
"\""); }
115 return intp.template capture<RET_TYPE>(prog);
config concept
Definition: config.h:31
load and unload a dynamic library
#define BBM_STRINGIFY(...)
Expand VA_ARGS before converting to a string of comma separated elements.
Definition: macro_util.h:74
void execute(const std::string &prog)
Helper function to execute a python string and ignore the output.
Definition: bbm_python_interpreter.h:125
RET_TYPE capture(const std::string &prog)
Helper function to execute a python string and capture the output.
Definition: bbm_python_interpreter.h:112
Definition: aggregatebsdf.h:29
typename std::decay_t< T >::Config get_config
get_config type trait
Definition: config.h:49
dl_handle_t loadDynamicLibrary(const std::string &name)
Definition: dynamic_library.h:37
void * dl_handle_t
Definition: dynamic_library.h:35
void closeDynamicLibrary(dl_handle_t handle)
Definition: dynamic_library.h:42
Python interpreter with BBM module imported.
Definition: bbm_python_interpreter.h:31
RET_TYPE capture(const std::string &prog) const
Execute Python code within the BBM environment.
Definition: bbm_python_interpreter.h:71
~interpreter(void)
Destructor releases scope and python interpreter appropriately.
Definition: bbm_python_interpreter.h:49
py::object _scope
Definition: bbm_python_interpreter.h:100
dl_handle_t _python
Definition: bbm_python_interpreter.h:99
void execute(const std::string &prog) const
Execute Python code within the BBM environment.
Definition: bbm_python_interpreter.h:91
interpreter(void)
Constructor: initializes BBM python interface.
Definition: bbm_python_interpreter.h:33