From da9a77ce42259e113b83c2fb103b29a7ad6c29bd Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Fri, 8 Aug 2025 03:15:16 -0700 Subject: [PATCH] GLTF: Preserve mesh name on export --- modules/gltf/gltf_document.cpp | 62 ++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp index c807f10550b..b5dd7fef537 100644 --- a/modules/gltf/gltf_document.cpp +++ b/modules/gltf/gltf_document.cpp @@ -135,6 +135,7 @@ static Ref _mesh_to_importer_mesh(Ref p_mesh) { mat_name, p_mesh->surface_get_format(surface_i)); } importer_mesh->merge_meta_from(*p_mesh); + importer_mesh->set_name(p_mesh->get_name()); return importer_mesh; } @@ -2840,13 +2841,14 @@ Error GLTFDocument::_serialize_meshes(Ref p_state) { Array meshes; for (GLTFMeshIndex gltf_mesh_i = 0; gltf_mesh_i < p_state->meshes.size(); gltf_mesh_i++) { print_verbose("glTF: Serializing mesh: " + itos(gltf_mesh_i)); - Ref import_mesh = p_state->meshes.write[gltf_mesh_i]->get_mesh(); + Ref &gltf_mesh = p_state->meshes.write[gltf_mesh_i]; + Ref import_mesh = gltf_mesh->get_mesh(); if (import_mesh.is_null()) { continue; } - Array instance_materials = p_state->meshes.write[gltf_mesh_i]->get_instance_materials(); + Array instance_materials = gltf_mesh->get_instance_materials(); Array primitives; - Dictionary gltf_mesh; + Dictionary mesh_dict; Array target_names; Array weights; for (int morph_i = 0; morph_i < import_mesh->get_blend_shape_count(); morph_i++) { @@ -3233,27 +3235,31 @@ Error GLTFDocument::_serialize_meshes(Ref p_state) { if (!target_names.is_empty()) { Dictionary e; e["targetNames"] = target_names; - gltf_mesh["extras"] = e; + mesh_dict["extras"] = e; } - _attach_meta_to_extras(import_mesh, gltf_mesh); + _attach_meta_to_extras(import_mesh, mesh_dict); weights.resize(target_names.size()); for (int name_i = 0; name_i < target_names.size(); name_i++) { real_t weight = 0.0; - if (name_i < p_state->meshes.write[gltf_mesh_i]->get_blend_weights().size()) { - weight = p_state->meshes.write[gltf_mesh_i]->get_blend_weights()[name_i]; + if (name_i < gltf_mesh->get_blend_weights().size()) { + weight = gltf_mesh->get_blend_weights()[name_i]; } weights[name_i] = weight; } if (weights.size()) { - gltf_mesh["weights"] = weights; + mesh_dict["weights"] = weights; } ERR_FAIL_COND_V(target_names.size() != weights.size(), FAILED); - gltf_mesh["primitives"] = primitives; + mesh_dict["primitives"] = primitives; - meshes.push_back(gltf_mesh); + if (!gltf_mesh->get_name().is_empty()) { + mesh_dict["name"] = gltf_mesh->get_name(); + } + + meshes.push_back(mesh_dict); } if (!meshes.size()) { @@ -4482,19 +4488,19 @@ Error GLTFDocument::_parse_texture_samplers(Ref p_state) { Error GLTFDocument::_serialize_materials(Ref p_state) { Array materials; for (int32_t i = 0; i < p_state->materials.size(); i++) { - Dictionary d; + Dictionary mat_dict; Ref material = p_state->materials[i]; if (material.is_null()) { - materials.push_back(d); + materials.push_back(mat_dict); continue; } if (!material->get_name().is_empty()) { - d["name"] = _gen_unique_name(p_state, material->get_name()); + mat_dict["name"] = _gen_unique_name(p_state, material->get_name()); } Ref base_material = material; if (base_material.is_null()) { - materials.push_back(d); + materials.push_back(mat_dict); continue; } @@ -4647,7 +4653,7 @@ Error GLTFDocument::_serialize_materials(Ref p_state) { if (has_ao) { Dictionary occt; occt["index"] = orm_texture_index; - d["occlusionTexture"] = occt; + mat_dict["occlusionTexture"] = occt; } if (has_roughness || has_metalness) { mrt["index"] = orm_texture_index; @@ -4661,7 +4667,7 @@ Error GLTFDocument::_serialize_materials(Ref p_state) { } } - d["pbrMetallicRoughness"] = mr; + mat_dict["pbrMetallicRoughness"] = mr; if (base_material->get_feature(BaseMaterial3D::FEATURE_NORMAL_MAPPING) && _image_format != "None") { Dictionary nt; Ref tex; @@ -4701,14 +4707,14 @@ Error GLTFDocument::_serialize_materials(Ref p_state) { nt["scale"] = base_material->get_normal_scale(); if (gltf_texture_index != -1) { nt["index"] = gltf_texture_index; - d["normalTexture"] = nt; + mat_dict["normalTexture"] = nt; } } if (base_material->get_feature(BaseMaterial3D::FEATURE_EMISSION)) { const Color c = base_material->get_emission().linear_to_srgb(); Array arr = { c.r, c.g, c.b }; - d["emissiveFactor"] = arr; + mat_dict["emissiveFactor"] = arr; } if (base_material->get_feature(BaseMaterial3D::FEATURE_EMISSION) && _image_format != "None") { @@ -4722,20 +4728,20 @@ Error GLTFDocument::_serialize_materials(Ref p_state) { if (gltf_texture_index != -1) { et["index"] = gltf_texture_index; - d["emissiveTexture"] = et; + mat_dict["emissiveTexture"] = et; } } const bool ds = base_material->get_cull_mode() == BaseMaterial3D::CULL_DISABLED; if (ds) { - d["doubleSided"] = ds; + mat_dict["doubleSided"] = ds; } if (base_material->get_transparency() == BaseMaterial3D::TRANSPARENCY_ALPHA_SCISSOR) { - d["alphaMode"] = "MASK"; - d["alphaCutoff"] = base_material->get_alpha_scissor_threshold(); + mat_dict["alphaMode"] = "MASK"; + mat_dict["alphaCutoff"] = base_material->get_alpha_scissor_threshold(); } else if (base_material->get_transparency() != BaseMaterial3D::TRANSPARENCY_DISABLED) { - d["alphaMode"] = "BLEND"; + mat_dict["alphaMode"] = "BLEND"; } Dictionary extensions; @@ -4750,10 +4756,10 @@ Error GLTFDocument::_serialize_materials(Ref p_state) { extensions["KHR_materials_emissive_strength"] = mat_emissive_strength; p_state->add_used_extension("KHR_materials_emissive_strength"); } - d["extensions"] = extensions; + mat_dict["extensions"] = extensions; - _attach_meta_to_extras(material, d); - materials.push_back(d); + _attach_meta_to_extras(material, mat_dict); + materials.push_back(mat_dict); } if (!materials.size()) { return OK; @@ -5858,6 +5864,10 @@ GLTFMeshIndex GLTFDocument::_convert_mesh_to_gltf(Ref p_state, MeshIn Ref gltf_mesh; gltf_mesh.instantiate(); + if (!mesh_resource->get_name().is_empty()) { + gltf_mesh->set_original_name(mesh_resource->get_name()); + gltf_mesh->set_name(_gen_unique_name(p_state, mesh_resource->get_name())); + } gltf_mesh->set_instance_materials(instance_materials); gltf_mesh->set_mesh(current_mesh); gltf_mesh->set_blend_weights(blend_weights);