diff --git a/CMakeLists.txt b/CMakeLists.txt index cb11cef2..55d7bfd8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -980,37 +980,53 @@ endif() if(TINYUSDZ_WITH_PYTHON) - # For the time beging, we stick with pybind11, since PyPI manylinux2014(probably mostly used architectrue as of 2022/Aug) does not support C++17. - # We may switch to nanobind at some point(probably around 2024?) + # TODO: Fully Remove pybind11-based python binding + ## For the time beging, we stick with pybind11, since PyPI manylinux2014(probably mostly used architectrue as of 2022/Aug) does not support C++17. + ## We may switch to nanobind at some point(probably around 2024?) - # build monolithic .dll - #nanobind_add_module(${BUILD_TARGET_PY} ${TINYUSDZ_PYTHON_BINDING_SOURCES}) + ## build monolithic .dll + ##nanobind_add_module(${BUILD_TARGET_PY} ${TINYUSDZ_PYTHON_BINDING_SOURCES}) - pybind11_add_module(${BUILD_TARGET_PY} ${TINYUSDZ_PYTHON_BINDING_SOURCES}) - add_sanitizers(${BUILD_TARGET_PY}) - target_include_directories( - ${BUILD_TARGET_PY} PRIVATE ${PROJECT_SOURCE_DIR}/src) + #pybind11_add_module(${BUILD_TARGET_PY} ${TINYUSDZ_PYTHON_BINDING_SOURCES}) + #add_sanitizers(${BUILD_TARGET_PY}) + #target_include_directories( + # ${BUILD_TARGET_PY} PRIVATE ${PROJECT_SOURCE_DIR}/src) - target_link_libraries(${BUILD_TARGET_PY} PRIVATE ${TINYUSDZ_TARGET_STATIC}) + #target_link_libraries(${BUILD_TARGET_PY} PRIVATE ${TINYUSDZ_TARGET_STATIC}) - # Use 'c' prefix - # TODO: Use `cpp` prefix? - set_target_properties(${BUILD_TARGET_PY} PROPERTIES OUTPUT_NAME "ctinyusdz") + ## Use 'c' prefix + ## TODO: Use `cpp` prefix? + #set_target_properties(${BUILD_TARGET_PY} PROPERTIES OUTPUT_NAME "ctinyusdz") - # copy python binding .so file to python/ + ## copy python binding .so file to python/ + ## For developer + ## NOTE: `POST_BUILD` command is not triggered when building python module using + ## `python setup.py build` + #add_custom_command( + # TARGET ${BUILD_TARGET_PY} + # POST_BUILD + # COMMAND "${CMAKE_COMMAND}" -E copy "$" + # "${CMAKE_SOURCE_DIR}/python/tinyusdz/$" + # COMMENT "copying python module file to python/tinyusdz" + # VERBATIM) + ## For pypi packaging + #install(TARGETS ${BUILD_TARGET_PY} LIBRARY DESTINATION tinyusdz) + + + # copy C tinyusd .so file to python/tinyusdz/ # For developer # NOTE: `POST_BUILD` command is not triggered when building python module using # `python setup.py build` add_custom_command( - TARGET ${BUILD_TARGET_PY} + TARGET ${BUILD_TARGET_C} POST_BUILD - COMMAND "${CMAKE_COMMAND}" -E copy "$" - "${CMAKE_SOURCE_DIR}/python/$" - COMMENT "copying python module file to python/" + COMMAND "${CMAKE_COMMAND}" -E copy "$" + "${CMAKE_SOURCE_DIR}/python/tinyusdz/$" + COMMENT "Copying libc-tinyusd.so/c-tinyusd.dll file to /python/tinyusdz" VERBATIM) - # For pypi packaging - install(TARGETS ${BUILD_TARGET_PY} LIBRARY DESTINATION tinyusdz) + # TODO: Run ctypesgen? + endif() diff --git a/python/README.md b/python/README.md index 5fd9a6f7..ced7b441 100644 --- a/python/README.md +++ b/python/README.md @@ -73,6 +73,20 @@ $ python setup.py build # Then copy `./_skbuild/-/cmake-install/tinyusdz/ctinyusdz.*.so/dll to `/python` folder. ``` +### Re-generate ctinyusdz.py + +When TinyUSDZ C API has been updated, need to re-genrerate `/python/tinyusdz/ctinyusd.py` +using ctypesgen https://github.com/ctypesgen/ctypesgen . + +``` +# if you do not install ctypesgen +$ python -m pip install ctypesgen + + +$ cd /python +$ sh gen-ctypes.sh +``` + ### Asan support If you built ctinyusdz with ASAN enabled, use `LD_PRELOAD` to load asan modules. diff --git a/python/gen-ctypes.sh b/python/gen-ctypes.sh new file mode 100644 index 00000000..d27d16d1 --- /dev/null +++ b/python/gen-ctypes.sh @@ -0,0 +1 @@ +ctypesgen -lc-tinyusd ../src/c-tinyusd.h -o tinyusdz/ctinyusd.py diff --git a/python/tinyusdz/__init__.py b/python/tinyusdz/__init__.py index a0f97c84..d797f72a 100644 --- a/python/tinyusdz/__init__.py +++ b/python/tinyusdz/__init__.py @@ -19,6 +19,13 @@ from .compat_typing_extensions import Literal, TypeAlias from . import version from .prims import Prim +try: + from . import ctinyusd +except ImportError: + import warnings + warnings.warn( + "Failed to import native module `ctinyusd`(No corresponding dll/so exists?). Loading USDA/USDC/USDZ feature is disabled.") + FILE_LIKE: TypeAlias = Union[str, os.PathLike, IO[str], IO[bytes]] try: @@ -32,12 +39,6 @@ except ImportError: return cls -try: - import ctinyusdz -except ImportError: - import warnings - warnings.warn( - "Failed to import native module `ctinyusdz`(No corresponding dll/so exists?). Loading USDA/USDC/USDZ feature is disabled.") try: import numpy as np @@ -49,10 +50,11 @@ try: except: pass -def is_ctinyusdz_available(): +def is_ctinyusd_available(): import importlib.util - if importlib.util.find_spec("ctinyusdz"): + # Seems '.' prefix required for relative module + if importlib.util.find_spec(".ctinyusd", package='tinyusdz'): return True return False diff --git a/python/tutorial.py b/python/tutorial.py index 0eab4ee3..0b14e673 100644 --- a/python/tutorial.py +++ b/python/tutorial.py @@ -5,7 +5,8 @@ print("TinyUSDZ major_version", tinyusdz.version.major_version) # int print("TinyUSDZ minor_version", tinyusdz.version.minor_version) # int print("TinyUSDZ micro_version", tinyusdz.version.micro_version) # int -print("ctinyusdz(Native TinyUSDZ module) available? ", tinyusdz.is_ctinyusdz_available()) # bool +# NOTE: no 'z' suffix. +print("ctinyusd(Native TinyUSDZ module) available? ", tinyusdz.is_ctinyusd_available()) # bool print("typeguard available? ", tinyusdz.is_typeguard_available()) # bool print("numpy available? ", tinyusdz.is_numpy_available()) # bool print("pandas available? ", tinyusdz.is_pandas_available()) # bool diff --git a/sandbox/python/gen-ctypes.sh b/sandbox/python/gen-ctypes.sh deleted file mode 100644 index fee46544..00000000 --- a/sandbox/python/gen-ctypes.sh +++ /dev/null @@ -1 +0,0 @@ -ctypesgen -lc-tinyusd ../../src/c-tinyusd.h -o ctinyusd.py