Forum

Author Topic: Generate normal maps texture using hi-res mesh as reference  (Read 1404 times)

Paulov

  • Newbie
  • *
  • Posts: 43
    • View Profile
Hi.
I managed to do this using the UI, the ressults are fine, and operation is fast. Yet I was trying to automate such task but I'm having a hard time procesing a folder.

My idea is , I've low resolution meshes in one folder, hi res meshes in another folder. I manage to load the meshes but it seems like the hi res mesh is not loaded, yet I can read the vertex and face count of both in the log, may be not in the same chunk? I found no python reference like chunk.moveTo or similar.

Here is the code at the point I gave up.

Code: [Select]
import os
import Metashape

# Set the paths to the folders containing the low and high resolution meshes
low_res_folder = r'E:/LIDAR_JAPAN/MMS8_Prototype_3/MMS/03_RoadMesh/04_Texturing/01_DATA_SOURCE_POINT_CLOUDS/00_a_mesh_real_coords'
high_res_folder = r'E:/LIDAR_JAPAN/MMS8_Prototype_3/MMS/03_RoadMesh/04_Texturing/01_DATA_SOURCE_POINT_CLOUDS/03_Normals'

# Set the output folder for the normal map texture
output_folder = r'E:/LIDAR_JAPAN/MMS8_Prototype_3/MMS/03_RoadMesh/04_Texturing/02_Texture_Outputs/04_NORMAL'

# Get a list of all the files in the low resolution mesh folder
file_list = os.listdir(low_res_folder)

# Create a new document and chunk
doc = Metashape.Document()
chunk = doc.addChunk()

# loop through low res meshes
for file_name in file_list:
    # Import low res model
    low_res_file = os.path.join(low_res_folder, file_name)
    print("low_res_file path:", low_res_file)
    low_res_model = chunk.importModel(low_res_file)

    chunk.resetRegion()

    # Import high res model
    high_res_file = os.path.join(high_res_folder, file_name)
    print("high_res_file path:", high_res_file)
    high_res_model = chunk.importModel(high_res_file)

    if high_res_model is not None:
        high_res_model.move(chunk.model)

        chunk.resetRegion()

        chunk.addReference(high_res_model)

        # Generate the normal map texture
        chunk.buildTexture(texture_size=4096, texture_type=Metashape.Model.NormalMap)

        # Save the texture to the specified output folder
        texture_file = os.path.join(output_folder, os.path.splitext(file_name)[0] + "_normal_map.jpg")
        chunk.exportTexture(texture_file)

        # Print the type of the high res model after it's defined
        print(type(high_res_model))

        doc.remove(chunk)
        print("normal map bake for file = " + file_name)

    else:
        print("No high resolution mesh found for file:", file_name)