
Mon, 01/13/2025 - 18:42
Forums:
Following is my code that exports Shape to GLTF base54 encoded string. But its all grey. How do I add materials?
def export_shape_gltf(shp: TopoDS_Shape, asc: bool = False) -> str: """ Export shape to gltf Returns the base64 encoded gltf (glb) file """ doc = TDocStd_Document("pythonocc-doc-gltf-export") shape_tool = XCAFDoc_DocumentTool.ShapeTool(doc.Main()) # Clean UP and Tessellate the Shape breptools.Clean(shp) # Triangulate msh_algo = BRepMesh_IncrementalMesh(shp, True) msh_algo.Perform() # Add Shape to document. This is required for glTF export shape_tool.AddShape(shp) # SetGLTF Options a_format = RWGltf_WriterTrsfFormat.RWGltf_WriterTrsfFormat_Compact force_uv_export = True # Add Metadata a_file_info = TColStd_IndexedDataMapOfStringString() a_file_info.Add( TCollection_AsciiString("Authors"), TCollection_AsciiString("CoEdge Inc")) # Write to file (ASCII or Binary) home_dir = os.path.expanduser("~") os.makedirs(os.path.join(home_dir, "tmp"), exist_ok=True) home_dir = os.path.join(home_dir, "tmp") is_binary = True # pylint: disable=consider-using-with file_name = tempfile.NamedTemporaryFile( suffix='.glb', prefix="tmp", dir=home_dir).name if asc: is_binary = False file_name = tempfile.NamedTemporaryFile( suffix='.gltf', prefix="tmp", dir=home_dir).name logger.debug("Exporting GLTF to file : %s", file_name) rwgltf_writer = RWGltf_CafWriter(file_name, is_binary) rwgltf_writer.SetTransformationFormat(a_format) rwgltf_writer.SetForcedUVExport(force_uv_export) rwgltf_writer.SetMergeFaces(True) rwgltf_writer.SetToEmbedTexturesInGlb(True) status = rwgltf_writer.Perform(doc, a_file_info, Message_ProgressRange()) logger.info("GLTF Export Status: %s", status) file_base64 = encode_file_base64(file_name) return file_base64