Skip to content

How to load & save mesh with textures by using python? #5337

@SalamankaKit

Description

@SalamankaKit

I tried to use meshlib to load mesh from .obj format file with textures. but failed every time.
I did not see any examples in repo, would anybody help me?
Here is my code:

class PymeshlibOps:
    def __init__(self):
        self._mesh: Optional[mr.Mesh] = None
        self._has_mesh = False
        self._loaded_uv = mr.VertCoords2()  
        self._loaded_texture = mr.MeshTexture()  

    @property
    def has_mesh(self) -> bool:
        return self._has_mesh

    def load(self, input_path: str) -> bool:
        try:
            load_uv = mr.VertCoords2()
            load_texture = mr.MeshTexture()

            skipped_faces = int_output()
            duplicated_verts = int_output()

            load_settings = MeshLoadSettings(
                edges=None,
                colors=mr.VertColors(),
                uvCoords=load_uv, 
                normals=mr.VertCoords(),
                texture=load_texture, 
                skippedFaceCount=skipped_faces,  
                duplicatedVertexCount=duplicated_verts,
                xf=mr.AffineXf3f(),
                callback=None,
            )

            self._mesh = mr.loadMesh(input_path, load_settings)
            self._loaded_uv = load_uv
            self._loaded_texture = load_texture

            self._has_mesh = True
            return True
        except Exception as e:
            raise

    def save(self, output_path: str) -> bool:
        if not self._has_mesh or self._mesh is None:
            print("error to save empty mesh", file=sys.stderr)
            return False

        try:
            create_output_directory(output_path)
            save_settings = SaveSettings(
            onlyValidPoints=True,
            packPrimitives=True,
            colors=mr.VertColors(),
            uvMap=self._loaded_uv,
            texture=use_texture,  
            materialName="simplified_textured_mesh",
            xf=mr.AffineXf3d(),
            progress=None
            )
            mr.saveMesh(self._mesh, output_path)
            return True
        except Exception as e:
     
            return False

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