Loading...
Searching...
No Matches
py_bsdf.h
Go to the documentation of this file.
1#ifndef _BBM_PY_BSDF_H_
2#define _BBM_PY_BSDF_H_
3
4#include "pybind11/pybind11.h"
5namespace py = pybind11;
6
8#include "util/typestring.h"
9#include "python/py_arg.h"
10#include "bbm/bsdf_ptr.h"
11#include "bbm/bsdf.h"
12
13/***********************************************************************/
14/*! \file py_bsdf.h
15 \brief Export a BSDF model to python
16************************************************************************/
17
18namespace bbm {
19 namespace python {
20
21 /*******************************************************************/
22 /*! \brief Provide a human readable string of an bbm::args type.
23
24 Prints the name and default value of the bbm::arg, and optinally the
25 type if TYPE is set to true.
26 ********************************************************************/
27 template<typename ARGS, bool TYPE=false> requires is_args_v<ARGS>
28 std::string args_to_string(void)
29 {
30 std::string result;
31
32 CONSTFOR(idx, ARGS::size,
33 {
34 using arg = std::decay_t<decltype(std::declval<ARGS>().template get<idx>())>;
35
36 if constexpr (idx != 0) result += ", ";
37
38 // add type (if requested)
39 if constexpr (TYPE) result += bbm::toString(typestring<typename arg::type>) + " ";
40
41 // add name (use <unnamed> if empty)
42 if constexpr (arg::name.empty) result += "<unnamed>";
43 else result += bbm::toString(arg::name);
44
45 // add default value (if exists)
46 if constexpr (std::is_constructible_v<arg>)
47 result += std::string(" = ") + bbm::toString(arg().value());
48 });
49
50 // Done.
51 return result;
52 }
53
54 /*******************************************************************/
55 /*! \brief export a BSDF model to Python
56
57 \tparam BSDFMODEL = BSDF model to export
58 \param m = python module
59
60 Creates a bsdf_ptr wrapper for a BSDF __model__, and exports it to the
61 python module 'm' with 'name'. Using a trampoline function to set the
62 attributes instead of direcly defining the attributes and default
63 values, otherwise there might be issues with embedding the module more than
64 once for certain types of default values (e.g., numpy).
65 *******************************************************************/
66 template<typename BSDFMODEL> requires concepts::bsdfmodel<BSDFMODEL> && concepts::constructor<BSDFMODEL>
67 void def_bsdf(py::module& m)
68 {
69 std::string name = bbm::toString(BSDFMODEL::name);
70
71 // message string
72 std::string msg = "Constructs: " + name + "(" + args_to_string<typename BSDFMODEL::constructor_args_t>() + ")";
73
74 // default trampoline function
75 m.def(name.c_str(), [](py::args args, py::kwargs kwargs)
76 {
77 // create bsdf_ptr
78 return make_bsdf_ptr( create_object<BSDFMODEL>(args, kwargs) );
79 }, msg.c_str());
80 }
81
82 } // end python namespace
83} // end bbm namespace
84
85#endif /* _BBM_BBMBSDF_H_ */
Connects a BSDF model and a BSDF.
A shared_ptr wrapper for bsdfs.
bsdfmodel concept
Definition: bsdfmodel.h:33
constructor concept
Definition: constructor.h:23
#define CONSTFOR(IDX, NUMITR,...)
HELPER MACRO.
Definition: constfor.h:63
concept to check if a type has a valid string_converter.
std::string args_to_string(void)
Provide a human readable string of an bbm::args type.
Definition: py_bsdf.h:28
void def_bsdf(py::module &m)
export a BSDF model to Python
Definition: py_bsdf.h:67
Definition: aggregatebsdf.h:29
std::string toString(const T &)
toString alias
Definition: stringconvert.h:594
decltype(auto) value(T &&t)
return the value of an attribute, or if not an attribute the object
Definition: attribute_value.h:20
Forward declaration of bbm::arg.
Definition: arg.h:27
Create an object with a bbm constructor (with bbm::args) from py::args and py::kwargs arguments.
Forward declaration.
Definition: args.h:248
produce stringview of type name of a type. Avoids using typeid for GCC, MSVC, and CLANG....