Skip to content

🚀Bake textures using the transfer_attributes_to_texture_per_vertex() method errors.🚀 #437

@yfq512

Description

@yfq512
  • Thanks for your job.
  • I have a street scene model (A: Tile_+117_-034.obj) and an isolated building model (B: city.obj). I tried to bake the textures from A onto B, but encountered some issues. Could you help check my code? It’s not working right now.
  • such as:

Image

+

Image

=

Image

import trimesh
import pymeshlab
import traceback

def bake_low_poly_model(high_poly_obj_path, low_poly_obj_path, output_obj_path, high_poly_texture_path, texture_size = 1024):
    try:
        # Load the high-poly model (Model A)
        high_poly_mesh = trimesh.load(high_poly_obj_path, process=False)
        if isinstance(high_poly_mesh, list):
            high_poly_mesh = high_poly_mesh[0]

        # Load the low-poly model (Model B)
        low_poly_mesh = trimesh.load(low_poly_obj_path, process=False)
        if isinstance(low_poly_mesh, list):
            low_poly_mesh = low_poly_mesh[0]

        # Create a MeshLab project
        ms = pymeshlab.MeshSet()

        # Add the high-poly model to the MeshLab project
        high_poly_m = pymeshlab.Mesh(high_poly_mesh.vertices, high_poly_mesh.faces)
        ms.add_mesh(high_poly_m, "high_poly")
        # Set the current mesh to the high-poly model (index 0)
        ms.set_current_mesh(0)

        # Calculate texture coordinates for the high-poly model
        ms.compute_texcoord_parametrization_triangle_trivial_per_wedge(textdim=texture_size, border=1)

        # Set the texture path for the high-poly model
        if high_poly_texture_path:
            ms.set_texture_per_mesh(textname=high_poly_texture_path, use_dummy_texture=False)

        # Add the low-poly model to the MeshLab project
        low_poly_m = pymeshlab.Mesh(low_poly_mesh.vertices, low_poly_mesh.faces)
        ms.add_mesh(low_poly_m, "low_poly")

        # Set the current mesh to the low-poly model (index 1)
        ms.set_current_mesh(1)
        # Calculate texture coordinates for the low-poly model
        ms.compute_texcoord_parametrization_triangle_trivial_per_wedge(textdim=texture_size, border=1)

        # Perform the attribute transfer to texture operation
        ms.transfer_attributes_to_texture_per_vertex(
            sourcemesh=0,
            targetmesh=1,
            attributeenum=1,  # 'Vertex Color', 'Vertex Normal', 'Vertex Quality', 'Texture Color'
            upperbound=pymeshlab.PercentageValue(2.0),
            textname='city2_texture.png',
            textw=texture_size,
            texth=texture_size,
            overwrite=False,
            pullpush=True,
        )
        # Save the baked low-poly model
        ms.save_current_mesh(output_obj_path, save_textures=True)
        print(f"Baking completed. The result has been saved to {output_obj_path}")
    except Exception as e:
        e = traceback.format_exc()
        print(f"An error occurred during baking: {e}")


if __name__ == "__main__":
    # Input the path of the high-poly model (Model A)
    high_poly_obj_path = "Tile_+117_-034.obj"
    # Input the path of the low-poly model (Model B)
    low_poly_obj_path = "city.obj"
    # Output the path of the baked model
    output_obj_path = "city2.obj"
    # Input the texture image path of the high-poly model
    high_poly_texture_path = "Tile_+117_-034_texture_1.jpg"
    bake_low_poly_model(high_poly_obj_path, low_poly_obj_path, output_obj_path, high_poly_texture_path)
  • I'm not sure if this is the right approach, but it's currently throwing an error.
  File "C:\Users\yinfu\Desktop\wippolder\mege_obj.py", line 27, in bake_low_poly_model
    ms.compute_texcoord_parametrization_triangle_trivial_per_wedge(textdim=texture_size, border=1)
  File "D:\Anaconda\Lib\site-packages\pymeshlab\__init__.py", line 41, in filter_function
    res_dict = self.apply_filter(name, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pymeshlab.pmeshlab.PyMeshLabException: Failed to apply filter: compute_texcoord_parametrization_triangle_trivial_per_wedge
Details: Inter-Triangle border is too much
  • Alternatively, are there other feasible methods to achieve similar functionality? I prefer not to use libraries like bpy to call external applications such as Blender or Maya.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions