From c50d0b47b58a37cfc9e2eacf79bea2d1cf5a091d Mon Sep 17 00:00:00 2001 From: kijai <40791699+kijai@users.noreply.github.com> Date: Mon, 14 Apr 2025 19:57:25 +0300 Subject: [PATCH] Add nodes to convert between ComfyUI native MESH object and the wrapper TRIMESH object --- nodes.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/nodes.py b/nodes.py index 54a4171..95446ac 100644 --- a/nodes.py +++ b/nodes.py @@ -1039,6 +1039,54 @@ class Hy3DLoadMesh: return (trimesh,) + +class TrimeshToMESH: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "trimesh": ("TRIMESH",), + } + } + RETURN_TYPES = ("MESH",) + OUTPUT_TOOLTIPS = ("MESH object containing vertices and faces as torch tensors.",) + + FUNCTION = "load" + CATEGORY = "Hunyuan3DWrapper" + DESCRIPTION = "Converts trimesh object to ComfyUI MESH object, which only includes mesh data" + + def load(self, trimesh): + + vertices = torch.tensor(trimesh.vertices, dtype=torch.float32) + faces = torch.tensor(trimesh.faces, dtype=torch.float32) + mesh = (self.MESH(vertices.unsqueeze(0), faces.unsqueeze(0))) + + return (mesh,) + + class MESH: + def __init__(self, vertices, faces): + self.vertices = vertices + self.faces = faces + +class MESHToTrimesh: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "mesh": ("MESH",), + } + } + RETURN_TYPES = ("TRIMESH",) + OUTPUT_TOOLTIPS = ("TRIMESH object containing vertices and faces as torch tensors.",) + + FUNCTION = "load" + CATEGORY = "Hunyuan3DWrapper" + DESCRIPTION = "Converts trimesh object to ComfyUI MESH object, which only includes mesh data" + + def load(self, mesh): + mesh_output = Trimesh.Trimesh(mesh.vertices[0], mesh.faces[0]) + return (mesh_output,) + class Hy3DUploadMesh: @classmethod def INPUT_TYPES(s): @@ -1808,6 +1856,8 @@ NODE_CLASS_MAPPINGS = { "Hy3DMeshInfo": Hy3DMeshInfo, "Hy3DFastSimplifyMesh": Hy3DFastSimplifyMesh, "Hy3DNvdiffrastRenderer": Hy3DNvdiffrastRenderer, + "TrimeshToMESH": TrimeshToMESH, + "MESHToTrimesh": MESHToTrimesh, } NODE_DISPLAY_NAME_MAPPINGS = { @@ -1842,5 +1892,7 @@ NODE_DISPLAY_NAME_MAPPINGS = { "Hy3DBPT": "Hy3D BPT", "Hy3DMeshInfo": "Hy3D Mesh Info", "Hy3DFastSimplifyMesh": "Hy3D Fast Simplify Mesh", - "Hy3DNvdiffrastRenderer": "Hy3D Nvdiffrast Renderer" + "Hy3DNvdiffrastRenderer": "Hy3D Nvdiffrast Renderer", + "TrimeshToMESH": "Trimesh to MESH", + "MESHToTrimesh": "MESH to Trimesh", }