From 7e3417ad8146673d116dd64554cae625412c0da9 Mon Sep 17 00:00:00 2001 From: Jonny K Date: Wed, 8 Jan 2025 14:07:46 -0600 Subject: [PATCH] Match materials in project This will see if there is a material at the path indicated by "material_name" and if there is, load the material. --- modules/gltf/editor/editor_scene_importer_blend.cpp | 4 +++- modules/gltf/editor/editor_scene_importer_blend.h | 3 ++- modules/gltf/gltf_document.cpp | 9 ++++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/modules/gltf/editor/editor_scene_importer_blend.cpp b/modules/gltf/editor/editor_scene_importer_blend.cpp index e64a571a614a..39e6b1dc4da5 100644 --- a/modules/gltf/editor/editor_scene_importer_blend.cpp +++ b/modules/gltf/editor/editor_scene_importer_blend.cpp @@ -183,6 +183,8 @@ Node *EditorSceneFormatImporterBlend::import_scene(const String &p_path, uint32_ parameters_map["export_materials"] = "PLACEHOLDER"; } else if (exports == BLEND_MATERIAL_EXPORT_EXPORT) { parameters_map["export_materials"] = "EXPORT"; + } else if (exports == BLEND_MATERIAL_EXPORT_MATCH) { + parameters_map["export_materials"] = "MATCH"; } } else { parameters_map["export_materials"] = "PLACEHOLDER"; @@ -371,7 +373,7 @@ void EditorSceneFormatImporterBlend::get_import_options(const String &p_path, Li ADD_OPTION_ENUM("blender/meshes/skins", "None,4 Influences (Compatible),All Influences", BLEND_BONE_INFLUENCES_ALL); ADD_OPTION_BOOL("blender/meshes/export_bones_deforming_mesh_only", false); ADD_OPTION_BOOL("blender/materials/unpack_enabled", true); - ADD_OPTION_ENUM("blender/materials/export_materials", "Placeholder,Export", BLEND_MATERIAL_EXPORT_EXPORT); + ADD_OPTION_ENUM("blender/materials/export_materials", "Placeholder,Export,Match", BLEND_MATERIAL_EXPORT_EXPORT); ADD_OPTION_BOOL("blender/animation/limit_playback", true); ADD_OPTION_BOOL("blender/animation/always_sample", true); ADD_OPTION_BOOL("blender/animation/group_tracks", true); diff --git a/modules/gltf/editor/editor_scene_importer_blend.h b/modules/gltf/editor/editor_scene_importer_blend.h index 17eb9e570958..f2b88744295c 100644 --- a/modules/gltf/editor/editor_scene_importer_blend.h +++ b/modules/gltf/editor/editor_scene_importer_blend.h @@ -60,7 +60,8 @@ class EditorSceneFormatImporterBlend : public EditorSceneFormatImporter { }; enum { BLEND_MATERIAL_EXPORT_PLACEHOLDER, - BLEND_MATERIAL_EXPORT_EXPORT + BLEND_MATERIAL_EXPORT_EXPORT, + BLEND_MATERIAL_EXPORT_MATCH }; enum { BLEND_MODIFIERS_NONE, diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp index dd27fef7ad00..d462c0fd9c09 100644 --- a/modules/gltf/gltf_document.cpp +++ b/modules/gltf/gltf_document.cpp @@ -4669,7 +4669,14 @@ Error GLTFDocument::_parse_materials(Ref p_state) { Ref material; material.instantiate(); if (material_dict.has("name") && !String(material_dict["name"]).is_empty()) { - material->set_name(material_dict["name"]); + if(ResourceLoader::exists("res://" + (String)material_dict["name"] + ".tres")){ + Ref cached_material = ResourceLoader::load("res://" + (String)material_dict["name"] + ".tres", "Material"); + cached_material->set_name(material_dict["name"]); + p_state->materials.push_back(cached_material); + continue; + } + + material->set_name(material_dict["name"]); } else { material->set_name(vformat("material_%s", itos(i))); }