Repository: bevyengine/bevy
File: examples/gltf/edit_material_on_gltf.rs (Rust)

Code before:
    // Get the `ColorOverride` of the entity, if it does not have a color override, return
    let Ok(color_override) = color_override.get(scene_ready.entity) else {
        info!("{} does not have a color override", scene_ready.entity);
        return;
    };

    // Iterate over all children recursively
    for descendant in children.iter_descendants(scene_ready.entity) {
        // Get the material id and name which were created from the glTF file information
        let Ok((id, material_name)) = mesh_materials.get(descendant) else {
            continue;
        };
        // Get the material of the descendant
        let Some(material) = asset_materials.get_mut(id.id()) else {
            continue;
        };

        // match on the material name, modifying the materials as necessary
        match material_name.0.as_str() {
            "LeatherPartsMat" => {
                info!("editing LeatherPartsMat to use ColorOverride tint");
                // Create a copy of the material and override base color
                // If you intend on creating multiple models with the same tint, it
                // is best to cache the handle somewhere, as having multiple materials
                // that are identical is expensive
                let mut new_material = material.into_inner_untracked().clone();
                new_material.base_color = color_override.0;

                // Override `MeshMaterial3d` with new material
                commands
                    .entity(descendant)
                    .insert(MeshMaterial3d(asset_materials.add(new_material)));
            }
            name => {
                info!("not replacing: {name}");
            }
        }
    }
}


Review comment (suggestion):
This doesn't seem like we should be using into_inner_untracked here. It seems more like we should stop using get_mut at all for this `material`.

Code after:
    // Get the `ColorOverride` of the entity, if it does not have a color override, return
    let Ok(color_override) = color_override.get(scene_ready.entity) else {
        info!("{} does not have a color override", scene_ready.entity);
        return;
    };

    // Iterate over all children recursively
    for descendant in children.iter_descendants(scene_ready.entity) {
        // Get the material id and name which were created from the glTF file information
        let Ok((id, material_name)) = mesh_materials.get(descendant) else {
            continue;
        };
        // Get the material of the descendant
        let Some(material) = asset_materials.get(id.id()) else {
            continue;
        };

        // match on the material name, modifying the materials as necessary
        match material_name.0.as_str() {
            "LeatherPartsMat" => {
                info!("editing LeatherPartsMat to use ColorOverride tint");
                // Create a copy of the material and override base color
                // If you intend on creating multiple models with the same tint, it
                // is best to cache the handle somewhere, as having multiple materials
                // that are identical is expensive
                let mut new_material = material.clone();
                new_material.base_color = color_override.0;

                // Override `MeshMaterial3d` with new material
                commands
                    .entity(descendant)
                    .insert(MeshMaterial3d(asset_materials.add(new_material)));
            }
            name => {
                info!("not replacing: {name}");
            }
        }
    }
}