Skip to content

Commit

Permalink
MediaType::guess_from_path: enforce .obj extension to be Wavefron…
Browse files Browse the repository at this point in the history
…t Obj (#3772)

[`mime_guess` maps `.obj` to `tgif`
files...](https://github.com/abonander/mime_guess/blob/fa5759c1bed87abea7af341ab2e50f51b0599326/src/mime_types.rs#L787)
but let's be real: nobody expects a `tgif` file.

This also made me realize that `mime_guess` is unmaintained and missing
some important stuff (e.g. JPEG XL) and we should switch to
`mime_guess2`... but for that we must first switch `egui_commonmark` to
`mime_guess2` 😭

Also, we don't load associated mtl files when loading objs, which makes
their use pretty limited...

```
$ rerun  /tmp/bm/bmw.obj
```

![image](https://github.com/rerun-io/rerun/assets/2910679/127b7be4-5b51-4450-b9cf-b246262e6c01)

- Fixes #3703
  • Loading branch information
teh-cmc authored Oct 10, 2023
1 parent 57b6842 commit 0a2a594
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/re_types/src/archetypes/asset3d.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions crates/re_types/src/components/media_type_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ impl MediaType {
/// Tries to guess the media type of the file at `path` based on its extension.
#[inline]
pub fn guess_from_path(path: impl AsRef<std::path::Path>) -> Option<Self> {
let path = path.as_ref();

// `mime_guess` considers `.obj` to be a a tgif… but really it's way more likely to be an obj.
if path
.extension()
.and_then(|ext| ext.to_str().map(|s| s.to_lowercase()))
.as_deref()
== Some("obj")
{
return Some(Self::obj());
}

mime_guess::from_path(path)
.first_raw()
.map(ToOwned::to_owned)
Expand Down
2 changes: 1 addition & 1 deletion docs/code-examples/asset3d_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int main(int argc, char* argv[]) {
std::vector<std::string> args(argv, argv + argc);

if (args.size() < 2) {
std::cerr << "Usage: " << args[0] << " <path_to_asset.[gltf|glb]>" << std::endl;
std::cerr << "Usage: " << args[0] << " <path_to_asset.[gltf|glb|obj]>" << std::endl;
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion docs/code-examples/asset3d_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import rerun as rr

if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} <path_to_asset.[gltf|glb]>")
print(f"Usage: {sys.argv[0]} <path_to_asset.[gltf|glb|obj]>")
sys.exit(1)

rr.init("rerun_example_asset3d_simple", spawn=True)
Expand Down
2 changes: 1 addition & 1 deletion docs/code-examples/asset3d_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rerun::external::anyhow;
fn main() -> anyhow::Result<()> {
let args = std::env::args().collect::<Vec<_>>();
let Some(path) = args.get(1) else {
anyhow::bail!("Usage: {} <path_to_asset.[gltf|glb]>", args[0]);
anyhow::bail!("Usage: {} <path_to_asset.[gltf|glb|obj]>", args[0]);
};

let (rec, storage) =
Expand Down
4 changes: 3 additions & 1 deletion rerun_cpp/src/rerun/archetypes/asset3d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rerun_cpp/src/rerun/archetypes/asset3d_ext.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <string>
Expand All @@ -17,6 +18,7 @@ namespace rerun {
) {
std::filesystem::path file_path(path);
std::string ext = file_path.extension().string();
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);

if (ext == ".glb") {
return rerun::components::MediaType::glb();
Expand Down
2 changes: 1 addition & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/asset3d.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rerun_py/rerun_sdk/rerun/archetypes/asset3d_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def guess_media_type(path: str) -> MediaType | None:

from ..components import MediaType

ext = Path(path).suffix
ext = Path(path).suffix.lower()
if ext == ".glb":
return MediaType.GLB
elif ext == ".gltf":
Expand Down

0 comments on commit 0a2a594

Please sign in to comment.