Skip to content

import MaterialX fails on Windows after cmake --install of a shared-library build #3129

Open

Description

@ppenenko

Steps to reproduce

  1. On Windows, with Python 3.8 or later, configure with -DMATERIALX_BUILD_SHARED_LIBS=ON -DMATERIALX_BUILD_PYTHON=ON. MATERIALX_INSTALL_PYTHON defaults to ON.
  2. Run cmake --build build --config Release, then cmake --install build --config Release.
  3. Run python -c "import MaterialX" from a new shell:
ImportError: DLL load failed while importing PyMaterialXCore: The specified module could not be found.

Cause
The last install step runs pip install . in <prefix>/python. That copies the extension modules into site-packages, but not the MaterialX DLLs they depend on, which are in <prefix>/bin. Since Python 3.8, Windows doesn't search PATH for an extension module's DLL dependencies. MaterialX/__init__.py has a fallback that calls os.add_dll_directory(<prefix>/bin), but it only runs when the package has no pip metadata, so it never runs for the copy pip installed. Wheels aren't affected, because pyproject.toml forces static libraries.

CI doesn't catch this, because the Windows_VS2022_x64_SharedLibs job runs with python: None.

A related problem with --prefix
The pip install step and python/CMakeLists.txt, which writes pyproject.toml directly into the install prefix, both use the prefix from configure time. So cmake --install --prefix <other> runs pip in the original install folder instead of the requested one.

Proposed fix
For Windows shared builds, copy the DLLs from <prefix>/bin into <prefix>/python/MaterialX before pip runs, so the installed package is self-contained. That's the same idea as delvewheel, minus the renaming of the DLLs. Also resolve the prefix at install time, and install pyproject.toml with an install rule. This is in use on our fork: metashade#102. It adds about 180 MB to site-packages with the Slang libraries enabled. I can open a PR against main.

Related: #2503.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions