Repository: bevyengine/bevy
File: crates/bevy_gltf/src/loader/mod.rs (Rust)

Code before:
        }
        for gltf_mesh in gltf.meshes() {
            let mut primitives = vec![];
            for primitive in gltf_mesh.primitives() {
                let primitive_label = GltfAssetLabel::Primitive {
                    mesh: gltf_mesh.index(),
                    primitive: primitive.index(),
                };
                let primitive_topology = primitive_topology(primitive.mode())?;

                let mut mesh = Mesh::new(primitive_topology, settings.load_meshes);

                let mut out_doc: Option<gltf::Document> = None;
                let mut out_data: Option<Vec<Vec<u8>>> = None;
                for extension in extensions.iter_mut() {
                    extension.on_gltf_primitive(
                        load_context,
                        &gltf,
                        &primitive,
                        &buffer_data,
                        &mut out_doc,
                        &mut out_data,
                    );
                }
                let primitive = if let Some(doc) = &out_doc
                    && let Some(mesh) = doc.meshes().next()
                    && let Some(primitive) = mesh.primitives().next()
                {
                    primitive
                } else {
                    primitive
                };
                let buffer_data = if let Some(data) = &out_data {
                    data
                } else {
                    &buffer_data
                };

                // Read vertex attributes
                for (semantic, accessor) in primitive.attributes() {
                    if [Semantic::Joints(0), Semantic::Weights(0)].contains(&semantic) {
                        if !meshes_on_skinned_nodes.contains(&gltf_mesh.index()) {
                            warn!(
                        "Ignoring attribute {:?} for skinned mesh {} used on non skinned nodes (NODE_SKINNED_MESH_WITHOUT_SKIN)",
                        semantic,
                        primitive_label
                    );
                            continue;
                        } else if meshes_on_non_skinned_nodes.contains(&gltf_mesh.index()) {
                            error!("Skinned mesh {} used on both skinned and non skin nodes, this is likely to cause an error (NODE_SKINNED_MESH_WITHOUT_SKIN)", primitive_label);
                        }

Review comment (bug):
Sorry just to clarify, the last thing I want here is a check that `out_doc.meshes()` and `mesh.primitives()` contains a single element each. I'm even ok with a panicking check here, but silently allowing the wrong inputs here feels too dangerous to me.

Code after:
        }
        for gltf_mesh in gltf.meshes() {
            let mut primitives = vec![];
            for primitive in gltf_mesh.primitives() {
                let primitive_label = GltfAssetLabel::Primitive {
                    mesh: gltf_mesh.index(),
                    primitive: primitive.index(),
                };
                let primitive_topology = primitive_topology(primitive.mode())?;

                let mut mesh = Mesh::new(primitive_topology, settings.load_meshes);

                let mut out_doc: Option<gltf::Document> = None;
                let mut out_data: Option<Vec<Vec<u8>>> = None;
                for extension in extensions.iter_mut() {
                    extension.on_gltf_primitive(
                        load_context,
                        &gltf,
                        &primitive,
                        &buffer_data,
                        &mut out_doc,
                        &mut out_data,
                    );
                }

                let primitive = if let Some(doc) = &out_doc {
                    let meshes_len = doc.meshes().len();
                    if meshes_len != 1 {
                        warn!(
                            "Extension returned {} meshes, expected exactly 1. Using original primitive.",
                            meshes_len
                        );
                        primitive
                    } else if let Some(mesh) = doc.meshes().next() {
                        let primitives_len = mesh.primitives().len();
                        if primitives_len != 1 {
                            warn!(
                                "Extension returned {} primitives, expected exactly 1. Using original primitive.",
                                primitives_len
                            );
                            primitive
                        } else if let Some(doc_primitive) = mesh.primitives().next() {
                            doc_primitive
                        } else {
                            primitive
                        }
                    } else {
                        primitive
                    }
                } else {
                    primitive