cnr-isti-vclab / PyMeshLab

The open source mesh processing python library
GNU General Public License v3.0
779 stars 63 forks source link

Single line update all mesh vertex colors? #233

Open ddm-j opened 2 years ago

ddm-j commented 2 years ago

I'm working on a small tool to generate an animation of physics field data on a mesh (stress, temperature, pressure, etc).

Basically, I take field data from any engineering software, map it onto my mesh, interpolate missing data, map data to a colormap, and possibly apply a laplacian smooth to the color data if I need.

In PyMeshLab I'm using transfer_vertex_color_to_texture to create a UV texture PNG for each timestep of my simulation data. Then using another library to stitch these texture images into a video that I can use as a video texture in Blender. My issue is that it seems I cannot update the color data for the vertices of the mesh for each timestep. My workaround is to regenerate the mesh, mesh set, triangle parameterization (UV unwrap), and color to texture transfer in a loop for every timestep.

    for i in range(frame_count):
        print("Generating texture for frame {0}".format(i))

        # Map color number to RGBA
        color = map(color_data[:, i])

        texture_dimension = 4096
        m = pml.Mesh(verts, face_matrix = np.asarray(mesh.triangles), v_normals_matrix=np.asarray(mesh.vertex_normals), v_color_matrix=color) # color is N x 4 with alpha info
        ms = pml.MeshSet()
        ms.add_mesh(m, "fea_{0}".format(i))
        ms.apply_filter("parametrization_trivial_per_triangle", textdim=4096)
        # create texture using UV map and vertex colors
        ms.apply_filter("transfer_vertex_color_to_texture", textname="fea_texture_{0}".format(i), textw=texture_dimension, texth=texture_dimension)
        # texture file won't be saved until you save the mesh
        ms.save_current_mesh("results/fea_textured_{0}.obj".format(i))

This seems inefficient. I'm not sure if there is a way to just update the vertex colors, then regenerate the texture for each iteration instead of creating a new mesh & mesh-set and UV unwrapping. Ideally the getter method Mesh.vertex_color_matrix would have a setter counterpart Mesh.set_vertex_color_matrix that I'd use to update the colors with an ndarray of equal size. This way I might move all of the initialization and unwrapping outside of the loop.

Is this possible in a way that might be faster than my method? Or would you be willing to add this feature as an enhancement?

One small unrelated question - would it be possible to export texture only? Without the corresponding OBJ file?

Regards, Brandon

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. The resources of the VCLab team are limited, and so we are asking for your help. If this is a bug and you can still reproduce this error on the last release of PyMeshLab, please reply with all of the information you have about it in order to keep the issue open. If this is a feature request, and you feel that it is still relevant and valuable, please tell us why. This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. The resources of the VCLab team are limited, and so we are asking for your help. If this is a bug and you can still reproduce this error on the last release of PyMeshLab, please reply with all of the information you have about it in order to keep the issue open. If this is a feature request, and you feel that it is still relevant and valuable, please tell us why. This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

alemuntoni commented 1 year ago

Sorry for the late reply.

would you be willing to add this feature as an enhancement?

We did not add setters to the Mesh data structure since the first version of pymeshlab on purpose: it could lead to dangerous situations in the internal state of the VCG Mesh. However I guess that setting vertex/face colors should be a safe operation. We will evaluate all the possible scenarios and, in case, we will add the feature to the next major version of pymeshlab.

One small unrelated question - would it be possible to export texture only? Without the corresponding OBJ file?

Yes, you can. Mesh class exposes textures, which can be saved to file.