Skip to content

Commit

Permalink
Adsk Contrib - Allow PyOpenColorIO module to load DLLs from Windows P…
Browse files Browse the repository at this point in the history
…ATH environment variable (AcademySoftwareFoundation#1759)

* Allow PyOpenColorIO module to load DLLs from Windows PATH environment variable with an opt-out option in case the user want the default behavior of Python 3.8+.

Signed-off-by: Cédrik Fuoco <[email protected]>

* Fixing typos in comments

Signed-off-by: Cédrik Fuoco <[email protected]>

---------

Signed-off-by: Cédrik Fuoco <[email protected]>
Co-authored-by: Doug Walker <[email protected]>
Signed-off-by: Cédrik Fuoco <[email protected]>
  • Loading branch information
cedrik-fuoco-adsk and doug-walker committed Mar 24, 2023
1 parent 0221599 commit caa2fce
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/bindings/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,22 @@ target_compile_definitions(PyOpenColorIO
PY_VERSION_PATCH=${Python_VERSION_PATCH}
)

# Set to site-package location.
if(WIN32)
set(_Python_VARIANT_PATH "${CMAKE_INSTALL_LIBDIR}/site-packages")
else()
set(_Python_VARIANT_PATH "${CMAKE_INSTALL_LIBDIR}/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}/site-packages")
endif()

# Create an internal global variable to access it in another scope but not publicly visible
# using ccmake.
# Create an internal global variable to access it in another scope but not publicly visible.
# The site-package location is needed in setup_ocio.bat.in and setup_ocio.sh.in.
set(PYTHON_VARIANT_PATH ${_Python_VARIANT_PATH} CACHE INTERNAL "")

# Set to PyOpenColorIO site-package location.
set(_PyOpenColorIO_SITE_PACKAGE_DIR "${PYTHON_VARIANT_PATH}/PyOpenColorIO")

install(TARGETS PyOpenColorIO
LIBRARY DESTINATION ${_Python_VARIANT_PATH}
LIBRARY DESTINATION ${_PyOpenColorIO_SITE_PACKAGE_DIR}
)

install(FILES __init__.py DESTINATION ${_PyOpenColorIO_SITE_PACKAGE_DIR})
24 changes: 24 additions & 0 deletions src/bindings/python/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright Contributors to the OpenColorIO Project.

import os, sys, platform

#
# Python 3.8+ has stopped loading DLLs from PATH environment variable on Windows.
#
# This code reproduce the old behavior (loading DLLs from PATH) by doing the following:
# 1 - Tokenizing PATH
# 2 - Checking that the directories exist and are not "."
# 3 - Add them to the DLL load path.
#
# The behavior described above is opt-out which means that it is activated by default.
# A user can opt-out and use the default behavior of Python 3.8+ by setting OCIO_PYTHON_LOAD_DLLS_FROM_PATH
# environment variable to 0.
#

if sys.version_info >= (3, 8) and platform.system() == "Windows" and os.getenv("OCIO_PYTHON_LOAD_DLLS_FROM_PATH", "1") == "1":
for path in os.getenv("PATH", "").split(os.pathsep):
if os.path.exists(path) and path != ".":
os.add_dll_directory(path)

from .PyOpenColorIO import *

0 comments on commit caa2fce

Please sign in to comment.