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 (suggestion):
This is surprising - A) we only ever access the first mesh+primitive here, so we are assuming that the extension always produces only one primitive, B) If one of these is missing, we silently do nothing.

We need to document that if you return `Some` for the doc, the document must contain one mesh and one primitive. Then here we should check that and return an error if that's not true. Let's avoid sneaky footguns!

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 {