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

how to use save_obj() #913

Closed
weihui1308 opened this issue Nov 1, 2021 · 13 comments
Closed

how to use save_obj() #913

weihui1308 opened this issue Nov 1, 2021 · 13 comments

Comments

@weihui1308
Copy link

weihui1308 commented Nov 1, 2021

❓ Questions on how to use PyTorch3D

Tutorial :https://pytorch3d.org/tutorials/fit_textured_mesh

4. Save the final predicted mesh

# Fetch the verts and faces of the final predicted mesh
final_verts, final_faces = new_src_mesh.get_mesh_verts_faces(0)

# Scale normalize back to the original target size
final_verts = final_verts * scale + center

# Store the predicted mesh using save_obj
final_obj = os.path.join('./', 'final_model.obj')
save_obj(final_obj, final_verts, final_faces)

the API shows:

pytorch3d.io.save_obj(
    f: Union[pathlib.Path, str], 
    verts,
    faces,
    decimal_places: Optional[int] = None,
    path_manager: Optional[iopath.common.file_io.PathManager] = None,
    *,
    verts_uvs: Optional[torch.Tensor] = None,
    faces_uvs: Optional[torch.Tensor] = None,
    texture_map: Optional[torch.Tensor] = None) → None

Save a mesh to an .obj file.

Parameters:	
f – File (str or path) to which the mesh should be written.
verts – FloatTensor of shape (V, 3) giving vertex coordinates.
faces – LongTensor of shape (F, 3) giving faces.
decimal_places – Number of decimal places for saving.
path_manager – Optional PathManager for interpreting f if it is a str.
verts_uvs – FloatTensor of shape (V, 2) giving the uv coordinate per vertex.
faces_uvs – LongTensor of shape (F, 3) giving the index into verts_uvs for each vertex in the face.
texture_map – FloatTensor of shape (H, W, 3) representing the texture map for the mesh which 
will be saved as an image. The values are expected to be in the range [0, 1],

how to use save_obj in Tutorial :https://pytorch3d.org/tutorials/fit_textured_mesh contain the parameters: verts_uvs, faces_uvs and texture_map.
In the other word, how to get the value of verts_uvs, faces_uvs and texture_map.

@bottler
Copy link
Contributor

bottler commented Nov 1, 2021

In the tutorial, the texture is a TexturesVertex object - i.e. one color per vertex. This format of colors is not mentioned in the normal OBJ file format description, and is not in our implementation of OBJ.

What are you trying to do? If you want to save the learned mesh from that tutorial to a file and then load it with other software, the OFF format or the PLY format could work.

@weihui1308
Copy link
Author

weihui1308 commented Nov 2, 2021

Thank you for your quick reply.

I want to save a mesh with texture to a .obj or .ply or other format file, and use this file for 3D printing.

I have try to use the function save_ply().

In the tutorial, I change:

# Store the predicted mesh using save_obj
final_obj = os.path.join('./', 'final_model.obj')
save_obj(final_obj, final_verts, final_faces)

to

# Store the predicted mesh using save_ply
final_obj = os.path.join('./', 'final_model.ply')
save_ply(final_obj, final_verts, final_faces)

but when I open the saved .ply file in Blender, it has no color, too.
What's the reason. Please tell me my mistake. Did I give less parameters? Thank you.

@bottler
Copy link
Contributor

bottler commented Nov 2, 2021

The save_ply function doesn't have the full functionality. You should construct a Meshes object with the final values including a TexturesVertex, and then do

from pytorch3d.io import IO
IO().save_mesh(mesh, "final_model.ply")

@weihui1308
Copy link
Author

weihui1308 commented Nov 2, 2021

I visualized the img as following:

new_mesh.textures = TexturesVertex(verts_features=sphere_verts_rgb)
images_predicted = renderer_textured(new_mesh, cameras=target_cameras[j], lights=lights)

img = images_predicted.permute(0, 3, 1, 2)[:, 0:3]
torchvision.utils.save_image(img_cls.data, "1.png", normalize=True)

200

But when I use the function IO().save_mesh(new_mesh, "final_model.ply"), the file final_model.ply in Blender is:

image

It has no color!

@bottler
Copy link
Contributor

bottler commented Nov 2, 2021

Which version of PyTorch3D? Can you paste the header from the file final_model.ply?

@weihui1308
Copy link
Author

weihui1308 commented Nov 3, 2021

The version of PyTorch3D is:
import pytorch3d
print(pytorch3d.__version__)
0.6.0

The header from the file final_model.ply is:

ply
format binary_little_endian 1.0
element vertex 2930
property float x
property float y
property float z
property float nx
property float ny
property float nz
property float red
property float green
property float blue
element face 5856
property list uchar int vertex_index
end_header

Thank you for your help.

@bottler
Copy link
Contributor

bottler commented Nov 3, 2021

The colors are being saved. Blender is not reading them. I have heard of something like this before, caused by the color fomat. Can you try
IO().save_mesh(new_mesh, "final_model.ply", colors_as_uint8=True)?

@weihui1308
Copy link
Author

I tried IO().save_mesh(new_mesh, "final_model.ply", colors_as_uint8=True).
It is useless. The code can run successfully. But still has no color!

and I tried IO().save_mesh(new_mesh, "final_model.ply", binary=True). It also has no color!

I open the .ply file use vim. It shows as following:

image

@bottler
Copy link
Contributor

bottler commented Nov 3, 2021

binary=True makes no difference. It is the default.
Can you try other combinations - maybe binary=False, colors_as_uint8=True works?

If you have other mesh software, like meshlab, could you try opening in that?

@weihui1308
Copy link
Author

Oh, yes!
The combination binary=False, colors_as_uint8=True is works. And I open the .ply file use meshlab.
Thanks so much.

By the way, the .obj file support for 3D printing is more friendly. Are there any tutorials that save mesh with texture or color using the function save_obj() .

@bottler
Copy link
Contributor

bottler commented Nov 7, 2021

No.

@bottler bottler closed this as completed Nov 7, 2021
@YaroslavShchekaturov
Copy link

Hi everyone!

I'm trying to save mesh as ply file and get the following error:
image

image

@bottler
Copy link
Contributor

bottler commented Nov 18, 2021

@YaroslavShchekaturov Please start a new issue and ask there. Your question is not related to this issue.

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

No branches or pull requests

3 participants