isl-org / Open3D

Open3D: A Modern Library for 3D Data Processing
http://www.open3d.org
Other
11.36k stars 2.29k forks source link

open3d.io.write_triangle_mesh(write_triangle_uvs) not creating mtl file for colorized 3D model. Only creates a .obj file. #5329

Open cecolson opened 2 years ago

cecolson commented 2 years ago

Checklist

My Question

I am trying to create a .obj file with color using open3d. I know the write_triangle_uvs parameter allows for mtl file to be saved, however I cannot get it to work no matter what I try. This was an issue that was previously closed but I would like to reopen it with my code included below. I do not have the parameter listed here because I know its default value = True. Please help me understand what I am missing. I cannot attach the .las file I used since github does not support it, but if you try any colorized .las file I am sure you will run into the same issue.

import numpy as np
import open3d as o3d
import laspy as lp

def OutputObject(voxelGrid, voxelSize, outputPath):
    print("Generating voxel object.")
    voxels=voxelGrid.get_voxels()
    voxelMesh = o3d.geometry.TriangleMesh()
    for voxel in voxels:
        block = o3d.geometry.TriangleMesh.create_box(width=1, height=1, depth=1, create_uv_map=True)#, map_texture_to_each_face=True)
        block.paint_uniform_color(voxel.color)
        block.translate(voxel.grid_index, relative=False)
        voxelMesh+=block

    voxelMesh.translate([0.5, 0.5, 0.5], relative=True)
    voxelMesh.scale(voxelSize, [0, 0, 0])
    voxelMesh.translate(voxelGrid.origin, relative=True)
    voxelMesh.merge_close_vertices(0.0000001)
    #o3d.visualization.draw_geometries([voxelMesh])
    meshTransformed = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, -1, 0, 0], [0, 0, 0, 1]])
    o3d.io.write_triangle_mesh(outputPath, voxelMesh.transform(meshTransformed), write_ascii=True)

if __name__ == '__main__':
    pointCloud = lp.read('GreenhouseLas.las')
    pointCloudData = o3d.geometry.PointCloud()
    pointCloudData.points = o3d.utility.Vector3dVector(
        np.vstack((pointCloud.x, pointCloud.y, pointCloud.z)).transpose())
    pointCloudData.colors = o3d.utility.Vector3dVector(
        np.vstack((pointCloud.red, pointCloud.green, pointCloud.blue)).transpose()/65535)
    voxelSize = round(
        max(pointCloudData.get_max_bound() - pointCloudData.get_min_bound())*0.005, 4)
    voxelGrid = o3d.geometry.VoxelGrid.create_from_point_cloud(pointCloudData, voxel_size=voxelSize)
    #o3d.visualization.draw_geometries([voxelGrid])

    outputPath = 'GreenhouseVMesh.obj'
    OutputObject(voxelGrid, voxelSize, outputPath)
awied1404 commented 2 years ago

Did you solve the problem? If yes, how? I am having the same problem

wzds2015 commented 1 year ago

Did you solve the problem? If yes, how? I am having the same problem

cecolson commented 1 year ago

Use a different library. Open3D does not support this functionality... I used:

import pymeshlab as pml

testconnect400 commented 1 year ago

Hi everyone watching this question,

I am having the same problem. When I perform the following code

mesh = volume.extract_triangle_mesh() obj_save_path = 'Demo_model.obj' o3d.io.write_triangle_mesh(obj_save_path, mesh, write_triangle_uvs=True, print_progress=True)

Only the "Demo_model.obj" file was saved and no sight of .mtl file. However, when I load the "Demo_model.obj" to an online obj viewer, it shows color.

I'm also trying to get the mtl file so that I can load the "Demo_model.obj" with color in Unity Any help would be appreciated! Thank you all!

spirosperos commented 7 months ago

Hi everyone watching this question,

I am having the same problem. When I perform the following code

mesh = volume.extract_triangle_mesh() obj_save_path = 'Demo_model.obj' o3d.io.write_triangle_mesh(obj_save_path, mesh, write_triangle_uvs=True, print_progress=True)

Only the "Demo_model.obj" file was saved and no sight of .mtl file. However, when I load the "Demo_model.obj" to an online obj viewer, it shows color.

I'm also trying to get the mtl file so that I can load the "Demo_model.obj" with color in Unity Any help would be appreciated! Thank you all!

@testconnect400 I'm having the same issue. Did you manage to solve it?