Skip to content

Commit

Permalink
Added PYMATERIALX_IMPORT_MODULE macro to import a PyMaterialX module
Browse files Browse the repository at this point in the history
either within the `MaterialX` Python package or as a standalone module.

Signed-off-by: Stefan Habel <19556655+StefanHabel@users.noreply.github.com>
  • Loading branch information
StefanHabel committed Oct 20, 2023
1 parent c0d65ed commit ab6860b
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 9 deletions.
11 changes: 11 additions & 0 deletions source/PyMaterialX/PyMaterialX.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@
#include <pybind11/operators.h>
#include <pybind11/stl.h>

// Define a macro to import a PyMaterialX module, e.g. `PyMaterialXCore`,
// either within the `MaterialX` Python package, e.g. in `installed/python/`,
// or as a standalone module, e.g. in `lib/`
#define PYMATERIALX_IMPORT_MODULE(MODULE_NAME) \
try { \
pybind11::module::import("MaterialX." #MODULE_NAME); \
} \
catch (const py::error_already_set&) { \
pybind11::module::import(#MODULE_NAME); \
}

#endif
2 changes: 1 addition & 1 deletion source/PyMaterialX/PyMaterialXFormat/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ PYBIND11_MODULE(PyMaterialXFormat, mod)
)docstring";

// PyMaterialXFormat depends on types defined in PyMaterialXCore
pybind11::module::import("PyMaterialXCore");
PYMATERIALX_IMPORT_MODULE(PyMaterialXCore);

bindPyFile(mod);
bindPyXmlIo(mod);
Expand Down
2 changes: 1 addition & 1 deletion source/PyMaterialX/PyMaterialXGenGlsl/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ PYBIND11_MODULE(PyMaterialXGenGlsl, mod)
)docstring";

// PyMaterialXGenGlsl depends on types defined in PyMaterialXGenShader
pybind11::module::import("PyMaterialXGenShader");
PYMATERIALX_IMPORT_MODULE(PyMaterialXGenShader);

bindPyGlslShaderGenerator(mod);
bindPyGlslResourceBindingContext(mod);
Expand Down
2 changes: 1 addition & 1 deletion source/PyMaterialX/PyMaterialXGenMdl/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ PYBIND11_MODULE(PyMaterialXGenMdl, mod)
)docstring";

// PyMaterialXGenMdl depends on types defined in PyMaterialXGenShader
pybind11::module::import("PyMaterialXGenShader");
PYMATERIALX_IMPORT_MODULE(PyMaterialXGenShader);

bindPyMdlShaderGenerator(mod);
};
2 changes: 1 addition & 1 deletion source/PyMaterialX/PyMaterialXGenMsl/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ PYBIND11_MODULE(PyMaterialXGenMsl, mod)
)docstring";

// PyMaterialXGenMsl depends on types defined in PyMaterialXGenShader
pybind11::module::import("PyMaterialXGenShader");
PYMATERIALX_IMPORT_MODULE(PyMaterialXGenShader);

bindPyMslShaderGenerator(mod);
bindPyMslResourceBindingContext(mod);
Expand Down
2 changes: 1 addition & 1 deletion source/PyMaterialX/PyMaterialXGenOsl/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ PYBIND11_MODULE(PyMaterialXGenOsl, mod)
)docstring";

// PyMaterialXGenOsl depends on types defined in PyMaterialXGenShader
pybind11::module::import("PyMaterialXGenShader");
PYMATERIALX_IMPORT_MODULE(PyMaterialXGenShader);

bindPyOslShaderGenerator(mod);
}
2 changes: 1 addition & 1 deletion source/PyMaterialX/PyMaterialXRender/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ PYBIND11_MODULE(PyMaterialXRender, mod)
)docstring";

// PyMaterialXRender depends on types defined in PyMaterialXCore
pybind11::module::import("PyMaterialXCore");
PYMATERIALX_IMPORT_MODULE(PyMaterialXCore);

bindPyMesh(mod);
bindPyGeometryHandler(mod);
Expand Down
2 changes: 1 addition & 1 deletion source/PyMaterialX/PyMaterialXRenderGlsl/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ PYBIND11_MODULE(PyMaterialXRenderGlsl, mod)
)docstring";

// PyMaterialXRenderGlsl depends on types defined in PyMaterialXRender
pybind11::module::import("PyMaterialXRender");
PYMATERIALX_IMPORT_MODULE(PyMaterialXRender);

bindPyGlslProgram(mod);
bindPyGlslRenderer(mod);
Expand Down
2 changes: 1 addition & 1 deletion source/PyMaterialX/PyMaterialXRenderMsl/PyModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
)docstring";

// PyMaterialXRenderMsl depends on types defined in PyMaterialXRender
pybind11::module::import("PyMaterialXRender");
PYMATERIALX_IMPORT_MODULE(PyMaterialXRender);

bindPyMslProgram(mod);
bindPyMslRenderer(mod);
Expand Down
2 changes: 1 addition & 1 deletion source/PyMaterialX/PyMaterialXRenderOsl/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ PYBIND11_MODULE(PyMaterialXRenderOsl, mod)
)docstring";

// PyMaterialXRenderOsl depends on types defined in PyMaterialXRender
pybind11::module::import("PyMaterialXRender");
PYMATERIALX_IMPORT_MODULE(PyMaterialXRender);

bindPyOslRenderer(mod);
}

0 comments on commit ab6860b

Please sign in to comment.