Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python pathlib support for file IO #6619

Merged
merged 3 commits into from
Feb 7, 2024
Merged

Conversation

sitic
Copy link
Contributor

@sitic sitic commented Jan 22, 2024

Adds support for pathlib.Path/os.PathLike type arguments to IO functions. E.g:

from pathlib import Path
import open3d as o3d

folder = Path('.')
mesh = o3d.io.read_triangle_mesh(folder / 'test.ply')
o3d.io. write_triangle_mesh(folder / 'test.ply', mesh)

is now supported in addition to str-type arguments.

Type

  • Bug fix (non-breaking change which fixes an issue): Fixes #
  • New feature (non-breaking change which adds functionality). Resolves Add pathlib support to IO methods in Python #6344
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) Resolves #

Motivation and Context

Currently, only string-type filenames are supported in all IO functions. Python's pathlib is quite popular and should be supported. This commit adds support for all libraries which are compatible with os.PathLike as pathlib is.

Checklist:

  • I have run python util/check_style.py --apply to apply Open3D code style
    to my code.
  • This PR changes Open3D behavior or adds new functionality.
    • Both C++ (Doxygen) and Python (Sphinx / Google style) documentation is
      updated accordingly.
    • I have added or updated C++ and / or Python unit tests OR included test
      results
      (e.g. screenshots or numbers) here.
  • I will follow up and update the code if CI fails.
  • For fork PRs, I have selected Allow edits from maintainers.

Description

Using std::filesystem::path as type for filename arguments allows pybind to support both pathlib.Path and string through <pybind11/stl/filesystem.h> (which I backported to C++14 with minimal changes). Unfortunately this means we have to call filename.string() in the wrappers when calling the open3d C++ code.

I've added support everywhere except for o3d.data classes and some o3d.t.io.*Sensor class functions.

The documentation shows will os.PathLike as type for filenames:

image

Copy link

update-docs bot commented Jan 22, 2024

Thanks for submitting this pull request! The maintainers of this repository would appreciate if you could update the CHANGELOG.md based on your changes.

@sitic sitic marked this pull request as draft January 22, 2024 18:38
@ssheorey ssheorey self-requested a review January 26, 2024 21:50
@sitic sitic changed the title [WIP] Add pathlib support for IO functions Add Python pathlib support for file IO Jan 29, 2024
@sitic sitic marked this pull request as ready for review January 29, 2024 08:48
Copy link
Member

@ssheorey ssheorey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much @sitic . This will PR really makes using Open3D easier!
PS: Regarding C++17 - we are ready to switch to that. I think the only issue was MSVC errors when built in C++17 mode but they go away in c++-latest mode. Let's merge this PR with C++14 in mind, but OK to use C++17 in the future. We can help fixing any build issues.

@ssheorey ssheorey merged commit 0a57855 into isl-org:main Feb 7, 2024
35 of 36 checks passed
@Kroppeb
Copy link

Kroppeb commented Mar 19, 2024

I love you. This is awesome!

@sitic sitic deleted the pathlib branch March 20, 2024 03:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add pathlib support to IO methods in Python
3 participants