facebookresearch / pytorch3d

PyTorch3D is FAIR's library of reusable components for deep learning with 3D data
https://pytorch3d.org/
Other
8.85k stars 1.32k forks source link

load_obj save_obj seems to modify the original obj (has anyone else seen this) #1766

Closed jloveric closed 8 months ago

jloveric commented 8 months ago

I have a model obj exported from blender. When I load that obj and immediately save it with pytorch3d save/load the resulting obj is missing or has some additional triangles added. I'm not sure what, but the resulting model visualized in blender is slightly incorrect. I'm not seeing any errors

🐛 Bugs / Unexpected behaviors

Instructions To Reproduce the Issue:

The script to reproduce is the following, where some.obj is a file we have. So also curious if others have seen an issue like this.
original.txt ( I added the .txt extension to the obj file so I could upload to github)

from pytorch3d.io import load_objs_as_meshes, load_obj, save_obj

mesh_path = "original.obj"
mesh = load_objs_as_meshes([mesh_path], device='cpu')
save_obj("test_mesh.obj", mesh.verts_list()[0], mesh.faces_list()[0])

The right obj is the original, the left is that produced by the above script. In the left image there are extra triangles at the base which prevent you from seeing through the object. Same issue with a bunch of other files (extra triangles). No .mtl file in either. Screenshot from 2024-03-27 10-55-45

  1. Any changes you made (git diff) or code you wrote I'm using pytorch3d = "0.7.6+pt2.0.1cu117"

  2. The exact command(s) you ran: I just ran the script above on the command line with the given file

  3. What you observed (including the full logs):

    python3 test_loader_saver.py 
    /home/john/.cache/pypoetry/virtualenvs/syncmvd-ABAMiSOX-py3.11/lib/python3.11/site-packages/pytorch3d/io/obj_io.py:548: UserWarning: Mtl file does not exist: guangzhou.mtl
    warnings.warn(f"Mtl file does not exist: {f}")
    mesh <pytorch3d.structures.meshes.Meshes object at 0x7fef87853190>
    Saving the loaded mesh

Please also simplify the steps as much as possible so they do not require additional resources to run, such as a private dataset.

bottler commented 8 months ago

Your OBJ file contains a lot of polygonal faces with more than 3 vertices. PyTorch3D doesn't support them and has to subdivide them into triangles, and it makes an assumption about order when doing so. See https://github.com/facebookresearch/pytorch3d/blob/main/pytorch3d/io/obj_io.py#L126 . I wonder if that assumption isn't valid for your data. I can't think of anything else.

jloveric commented 8 months ago

Thanks! That's probably the issue. I'll see if I can fix the problem with some preprocessing.

jloveric commented 8 months ago

Just verified that triangulating all faces in blender and then exporting to obj solved this issue.