Skip to content

Commit 83ae82e

Browse files
authored
Resolved Python module import dependencies (AcademySoftwareFoundation#1595)
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).
1 parent f086b86 commit 83ae82e

13 files changed

Lines changed: 45 additions & 8 deletions

File tree

python/Scripts/baketextures.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
import sys, os, argparse
77
from sys import platform
8+
89
import MaterialX as mx
9-
from MaterialX import PyMaterialXGenShader
10-
from MaterialX import PyMaterialXGenGlsl
1110
from MaterialX import PyMaterialXRender as mx_render
1211
from MaterialX import PyMaterialXRenderGlsl as mx_render_glsl
1312
if platform == "darwin":

python/Scripts/generateshader.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
'''
66

77
import sys, os, argparse, subprocess
8+
89
import MaterialX as mx
9-
import MaterialX.PyMaterialXGenShader as mx_gen_shader
1010
import MaterialX.PyMaterialXGenGlsl as mx_gen_glsl
11-
import MaterialX.PyMaterialXGenOsl as mx_gen_osl
1211
import MaterialX.PyMaterialXGenMdl as mx_gen_mdl
1312
import MaterialX.PyMaterialXGenMsl as mx_gen_msl
13+
import MaterialX.PyMaterialXGenOsl as mx_gen_osl
14+
import MaterialX.PyMaterialXGenShader as mx_gen_shader
1415

1516
def validateCode(sourceCodeFile, codevalidator, codevalidatorArgs):
1617
if codevalidator:

python/Scripts/translateshader.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
'''
66

77
import sys, os, argparse
8-
import MaterialX as mx
9-
108
from sys import platform
9+
10+
import MaterialX as mx
1111
from MaterialX import PyMaterialXGenShader as mx_gen_shader
12-
from MaterialX import PyMaterialXGenGlsl as ms_gen_glsl
1312
from MaterialX import PyMaterialXRender as mx_render
1413
from MaterialX import PyMaterialXRenderGlsl as mx_render_glsl
1514
if platform == "darwin":
16-
from MaterialX import PyMaterialXGenMsl as ms_gen_msl
1715
from MaterialX import PyMaterialXRenderMsl as mx_render_msl
1816

1917
def main():

source/PyMaterialX/PyMaterialX.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,16 @@
1616
#include <pybind11/operators.h>
1717
#include <pybind11/stl.h>
1818

19+
// Define a macro to import a PyMaterialX module, e.g. `PyMaterialXCore`,
20+
// either within the `MaterialX` Python package, e.g. in `installed/python/`,
21+
// or as a standalone module, e.g. in `lib/`
22+
#define PYMATERIALX_IMPORT_MODULE(MODULE_NAME) \
23+
try \
24+
{ \
25+
pybind11::module::import("MaterialX." #MODULE_NAME); \
26+
} \
27+
catch (const py::error_already_set&) \
28+
{ \
29+
pybind11::module::import(#MODULE_NAME); \
30+
}
1931
#endif

source/PyMaterialX/PyMaterialXFormat/PyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ PYBIND11_MODULE(PyMaterialXFormat, mod)
1515
{
1616
mod.doc() = "Module containing Python bindings for the MaterialXFormat library";
1717

18+
// PyMaterialXFormat depends on types defined in PyMaterialXCore
19+
PYMATERIALX_IMPORT_MODULE(PyMaterialXCore);
20+
1821
bindPyFile(mod);
1922
bindPyXmlIo(mod);
2023
bindPyUtil(mod);

source/PyMaterialX/PyMaterialXGenGlsl/PyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ PYBIND11_MODULE(PyMaterialXGenGlsl, mod)
1616
{
1717
mod.doc() = "Module containing Python bindings for the MaterialXGenGlsl library";
1818

19+
// PyMaterialXGenGlsl depends on types defined in PyMaterialXGenShader
20+
PYMATERIALX_IMPORT_MODULE(PyMaterialXGenShader);
21+
1922
bindPyGlslShaderGenerator(mod);
2023
bindPyGlslResourceBindingContext(mod);
2124

source/PyMaterialX/PyMaterialXGenMdl/PyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ PYBIND11_MODULE(PyMaterialXGenMdl, mod)
1313
{
1414
mod.doc() = "Module containing Python bindings for the MaterialXGenMdl library";
1515

16+
// PyMaterialXGenMdl depends on types defined in PyMaterialXGenShader
17+
PYMATERIALX_IMPORT_MODULE(PyMaterialXGenShader);
18+
1619
bindPyMdlShaderGenerator(mod);
1720
};

source/PyMaterialX/PyMaterialXGenMsl/PyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ PYBIND11_MODULE(PyMaterialXGenMsl, mod)
1414
{
1515
mod.doc() = "Module containing Python bindings for the MaterialXGenMsl library";
1616

17+
// PyMaterialXGenMsl depends on types defined in PyMaterialXGenShader
18+
PYMATERIALX_IMPORT_MODULE(PyMaterialXGenShader);
19+
1720
bindPyMslShaderGenerator(mod);
1821
bindPyMslResourceBindingContext(mod);
1922
}

source/PyMaterialX/PyMaterialXGenOsl/PyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ PYBIND11_MODULE(PyMaterialXGenOsl, mod)
1313
{
1414
mod.doc() = "Module containing Python bindings for the MaterialXGenOsl library";
1515

16+
// PyMaterialXGenOsl depends on types defined in PyMaterialXGenShader
17+
PYMATERIALX_IMPORT_MODULE(PyMaterialXGenShader);
18+
1619
bindPyOslShaderGenerator(mod);
1720
}

source/PyMaterialX/PyMaterialXRender/PyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ PYBIND11_MODULE(PyMaterialXRender, mod)
2525
{
2626
mod.doc() = "Module containing Python bindings for the MaterialXRender library";
2727

28+
// PyMaterialXRender depends on types defined in PyMaterialXCore
29+
PYMATERIALX_IMPORT_MODULE(PyMaterialXCore);
30+
2831
bindPyMesh(mod);
2932
bindPyGeometryHandler(mod);
3033
bindPyLightHandler(mod);

0 commit comments

Comments
 (0)