Skip to content

Commit

Permalink
Resolved Python module import dependencies (#1595)
Browse files Browse the repository at this point in the history
This patch adds pybind11::module::import() calls to MaterialX Python C extension modules that depend on other MaterialX Python C extension modules.

The intention is to allow any of the MaterialX Python C extension modules to be imported in any order.

A couple of the Python scripts are updated to remove unused MaterialX imports, and to sort imports alphabetically (which is now possible as the modules import their own dependencies).
  • Loading branch information
StefanHabel committed Nov 9, 2023
1 parent f086b86 commit 83ae82e
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 8 deletions.
3 changes: 1 addition & 2 deletions python/Scripts/baketextures.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

import sys, os, argparse
from sys import platform

import MaterialX as mx
from MaterialX import PyMaterialXGenShader
from MaterialX import PyMaterialXGenGlsl
from MaterialX import PyMaterialXRender as mx_render
from MaterialX import PyMaterialXRenderGlsl as mx_render_glsl
if platform == "darwin":
Expand Down
5 changes: 3 additions & 2 deletions python/Scripts/generateshader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
'''

import sys, os, argparse, subprocess

import MaterialX as mx
import MaterialX.PyMaterialXGenShader as mx_gen_shader
import MaterialX.PyMaterialXGenGlsl as mx_gen_glsl
import MaterialX.PyMaterialXGenOsl as mx_gen_osl
import MaterialX.PyMaterialXGenMdl as mx_gen_mdl
import MaterialX.PyMaterialXGenMsl as mx_gen_msl
import MaterialX.PyMaterialXGenOsl as mx_gen_osl
import MaterialX.PyMaterialXGenShader as mx_gen_shader

def validateCode(sourceCodeFile, codevalidator, codevalidatorArgs):
if codevalidator:
Expand Down
6 changes: 2 additions & 4 deletions python/Scripts/translateshader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
'''

import sys, os, argparse
import MaterialX as mx

from sys import platform

import MaterialX as mx
from MaterialX import PyMaterialXGenShader as mx_gen_shader
from MaterialX import PyMaterialXGenGlsl as ms_gen_glsl
from MaterialX import PyMaterialXRender as mx_render
from MaterialX import PyMaterialXRenderGlsl as mx_render_glsl
if platform == "darwin":
from MaterialX import PyMaterialXGenMsl as ms_gen_msl
from MaterialX import PyMaterialXRenderMsl as mx_render_msl

def main():
Expand Down
12 changes: 12 additions & 0 deletions source/PyMaterialX/PyMaterialX.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,16 @@
#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
3 changes: 3 additions & 0 deletions source/PyMaterialX/PyMaterialXFormat/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ PYBIND11_MODULE(PyMaterialXFormat, mod)
{
mod.doc() = "Module containing Python bindings for the MaterialXFormat library";

// PyMaterialXFormat depends on types defined in PyMaterialXCore
PYMATERIALX_IMPORT_MODULE(PyMaterialXCore);

bindPyFile(mod);
bindPyXmlIo(mod);
bindPyUtil(mod);
Expand Down
3 changes: 3 additions & 0 deletions source/PyMaterialX/PyMaterialXGenGlsl/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ PYBIND11_MODULE(PyMaterialXGenGlsl, mod)
{
mod.doc() = "Module containing Python bindings for the MaterialXGenGlsl library";

// PyMaterialXGenGlsl depends on types defined in PyMaterialXGenShader
PYMATERIALX_IMPORT_MODULE(PyMaterialXGenShader);

bindPyGlslShaderGenerator(mod);
bindPyGlslResourceBindingContext(mod);

Expand Down
3 changes: 3 additions & 0 deletions source/PyMaterialX/PyMaterialXGenMdl/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ PYBIND11_MODULE(PyMaterialXGenMdl, mod)
{
mod.doc() = "Module containing Python bindings for the MaterialXGenMdl library";

// PyMaterialXGenMdl depends on types defined in PyMaterialXGenShader
PYMATERIALX_IMPORT_MODULE(PyMaterialXGenShader);

bindPyMdlShaderGenerator(mod);
};
3 changes: 3 additions & 0 deletions source/PyMaterialX/PyMaterialXGenMsl/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ PYBIND11_MODULE(PyMaterialXGenMsl, mod)
{
mod.doc() = "Module containing Python bindings for the MaterialXGenMsl library";

// PyMaterialXGenMsl depends on types defined in PyMaterialXGenShader
PYMATERIALX_IMPORT_MODULE(PyMaterialXGenShader);

bindPyMslShaderGenerator(mod);
bindPyMslResourceBindingContext(mod);
}
3 changes: 3 additions & 0 deletions source/PyMaterialX/PyMaterialXGenOsl/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ PYBIND11_MODULE(PyMaterialXGenOsl, mod)
{
mod.doc() = "Module containing Python bindings for the MaterialXGenOsl library";

// PyMaterialXGenOsl depends on types defined in PyMaterialXGenShader
PYMATERIALX_IMPORT_MODULE(PyMaterialXGenShader);

bindPyOslShaderGenerator(mod);
}
3 changes: 3 additions & 0 deletions source/PyMaterialX/PyMaterialXRender/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ PYBIND11_MODULE(PyMaterialXRender, mod)
{
mod.doc() = "Module containing Python bindings for the MaterialXRender library";

// PyMaterialXRender depends on types defined in PyMaterialXCore
PYMATERIALX_IMPORT_MODULE(PyMaterialXCore);

bindPyMesh(mod);
bindPyGeometryHandler(mod);
bindPyLightHandler(mod);
Expand Down
3 changes: 3 additions & 0 deletions source/PyMaterialX/PyMaterialXRenderGlsl/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ PYBIND11_MODULE(PyMaterialXRenderGlsl, mod)
{
mod.doc() = "Module containing Python bindings for the MaterialXRenderGlsl library";

// PyMaterialXRenderGlsl depends on types defined in PyMaterialXRender
PYMATERIALX_IMPORT_MODULE(PyMaterialXRender);

bindPyGlslProgram(mod);
bindPyGlslRenderer(mod);
bindPyGLTextureHandler(mod);
Expand Down
3 changes: 3 additions & 0 deletions source/PyMaterialX/PyMaterialXRenderMsl/PyModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
{
mod.doc() = "Module containing Python bindings for the MaterialXRenderMsl library";

// PyMaterialXRenderMsl depends on types defined in PyMaterialXRender
PYMATERIALX_IMPORT_MODULE(PyMaterialXRender);

bindPyMslProgram(mod);
bindPyMslRenderer(mod);
bindPyMetalTextureHandler(mod);
Expand Down
3 changes: 3 additions & 0 deletions source/PyMaterialX/PyMaterialXRenderOsl/PyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ PYBIND11_MODULE(PyMaterialXRenderOsl, mod)
{
mod.doc() = "Module containing Python bindings for the MaterialXRenderOsl library";

// PyMaterialXRenderOsl depends on types defined in PyMaterialXRender
PYMATERIALX_IMPORT_MODULE(PyMaterialXRender);

bindPyOslRenderer(mod);
}

0 comments on commit 83ae82e

Please sign in to comment.