Skip to content

Support loading assets from arbitrary filesystem paths by specifying an absolute path as the asset path #16294

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

inodentry
Copy link
Contributor

@inodentry inodentry commented Nov 8, 2024

Objective / Problem

There is no way to access asset files outside of the designated asset directory. Bevy's FileReader/FileWriter always interpret paths relative to the configured root directory (based on either the executable's path, or env var).

Being able to load (and save) assets anywhere in the filesystem is important for many applications, most notably editors.

The goal of this PR is to implement the simplest possible solution to this problem.

Solution

If an absolute path is provided when loading/saving an asset, it is used as-is.

If a relative path is provided, everything works exactly the same as before. Technically this isn't a breaking change, as
previously only relative paths were accepted by Bevy.

Testing

  • Did you test these changes? If so, how?

I ran the various bevy examples to make sure they can still load their assets (from the assets folder, as before).

I then modified them (locally, not committed, ofc) by changing the paths in the source code to various absolute paths
on my computer.

I can confirm Bevy can now successfully load random images and GLTFs on my PC. :)

  • Are there any parts that need more testing?

I have not tested asset meta files.

I have not tested the "sync" loader (when the multi_threaded cargo feature is disabled).

I have not tested asset writing / saving.

I have not tested the file watcher / hot reloading.

Probably also worth testing loading a complex GLTF file that references external files (for textures, etc) using an absolute path, to make sure asset dependencies work when using the new absolute paths.

  • How can other people (reviewers) test your changes? Is there anything specific they need to know?

Nothing special. Just try to load assets from an absolute path.

asset_server.load("/home/me/Downloads/random_pic.png");

Windows:

asset_server.load("C:\\Users\\Me\\Downloads\\random_pic.png");
  • If relevant, what platforms did you test these changes on, and are there any important ones you can't test?

I have not tested on Windows yet. I will be able to do that soon. Will edit the PR description to remove this note when I do.

The changes in this PR are only relevant to desktop platforms (the file asset io is not used on mobile and web).


Migration guide not necessary.

This PR, while straightforward, does enable cool new use cases and is probably deserving of a release note.

@inodentry inodentry added C-Feature A new feature, making something new possible A-Assets Load files from disk to use for things like images, models, and sounds labels Nov 8, 2024
@alice-i-cecile alice-i-cecile added A-Editor Graphical tools to make Bevy games S-Needs-Review Needs reviewer attention (from anyone!) to move forward D-Straightforward Simple bug fixes and API improvements, docs, test and examples and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Nov 8, 2024
@mockersf mockersf added the X-Controversial There is active debate or serious implications around merging this PR label Nov 8, 2024
@mockersf
Copy link
Member

mockersf commented Nov 8, 2024

@andriyDev
Copy link
Contributor

Is it possible to instead just make this an AssetSource? For example, it could look like asset_server.load("absolute://C:\\Users\\Me\\Downloads\\random_pic.png"); This would make enabling "absolute asset paths" a matter of adding a AbsoluteAssetSource - we don't need to modify any part of the asset system, and users can choose whether they want to support absolute assets at all.

The cost as we can see is that the syntax is kinda ugly, especially on Windows.

@copygirl
Copy link
Contributor

copygirl commented Feb 8, 2025

@andriyDev This reminds me of the file: URI scheme which from the sounds of it only supports absolute paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds A-Editor Graphical tools to make Bevy games C-Feature A new feature, making something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward X-Controversial There is active debate or serious implications around merging this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants