bevyengine/bevy · error · CubicNurbsError

Incorrect number of weights: expected {expected}, provided {

Error message

Incorrect number of weights: expected {expected}, provided {provided}

What it means

CubicNurbsError::WeightsNumberMismatch is returned by CubicNurbs::new when the optional weights iterator yields a different count than the control points. Each NURBS control point must be paired with exactly one weight (rational basis), so a length mismatch makes the curve ill-defined. `expected` is the control-point count, `provided` is the weights count.

Source

Thrown at crates/bevy_math/src/cubic_splines/mod.rs:543

#[derive(Clone, Debug, Error)]
pub enum CubicNurbsError {
    /// Provided the wrong number of knots.
    #[error("Wrong number of knots: expected {expected}, provided {provided}")]
    KnotsNumberMismatch {
        /// Expected number of knots
        expected: usize,
        /// Provided number of knots
        provided: usize,
    },
    /// The provided knots had a descending knot pair. Subsequent knots must
    /// either increase or stay the same.
    #[error("Invalid knots: contains descending knot pair")]
    DescendingKnots,
    /// The provided knots were all equal. Knots must contain at least one increasing pair.
    #[error("Invalid knots: all knots are equal")]
    ConstantKnots,
    /// Provided a different number of weights and control points.
    #[error("Incorrect number of weights: expected {expected}, provided {provided}")]
    WeightsNumberMismatch {
        /// Expected number of weights
        expected: usize,
        /// Provided number of weights
        provided: usize,
    },
    /// The number of control points provided is less than 4.
    #[error("Not enough control points, at least 4 are required, {provided} were provided")]
    NotEnoughControlPoints {
        /// The number of control points provided
        provided: usize,
    },
}

/// Non-uniform Rational B-Splines (NURBS) are a powerful generalization of the [`CubicBSpline`] which can
/// represent a much more diverse class of curves (like perfect circles and ellipses).
///
/// ### Non-uniformity

View on GitHub (pinned to 396ca72708)

Solutions

  1. Pass `None` for weights to get uniform weight 1.0 for every control point.
  2. Make weights exactly control_points.len() long: pad with 1.0 or trim, depending on intent.
  3. If weights come from data, validate lengths together at load time and reject the asset with a clear message.

Example fix

// before
let points = vec![v0, v1, v2, v3, v4];
let weights = vec![1.0, 2.0, 1.0, 1.0]; // 4 weights, 5 points
let nurbs = CubicNurbs::new(points, Some(weights), None)?; // WeightsNumberMismatch

// after
let points = vec![v0, v1, v2, v3, v4];
let weights = vec![1.0, 2.0, 1.0, 1.0, 1.0]; // one per point
let nurbs = CubicNurbs::new(points, Some(weights), None)?;
Defensive patterns

Strategy: validation

Validate before calling

let ok = weights.as_ref().map_or(true, |w| w.len() == points.len());
if !ok { /* pad/trim weights or pass None */ }

Try / catch

match CubicNurbs::new(&points, Some(weights), knots) {
    Ok(nurbs) => nurbs,
    Err(CubicNurbsError::WeightsNumberMismatch { expected, provided }) => {
        let fixed: Vec<f32> = (0..expected).map(|i| weights.get(i).copied().unwrap_or(1.0)).collect();
        CubicNurbs::new(&points, Some(fixed), knots)?
    }
    Err(e) => return Err(e.into()),
}

Prevention

When it happens

Trigger: CubicNurbs::new(points, Some(weights), knots) where weights.len() != points.len() — e.g. points filtered or appended after weights were built, or a weight list copied from a different asset.

Common situations: Editing a NURBS asset and removing a control point without touching weights; mixing per-segment weights with per-point weights; deserializing weights from JSON with a missing/extra element.

Related errors


AI-assisted analysis of bevyengine/bevy@396ca72708 (2026-08-20). Data as JSON: /api/errors/a3795ce363e1abc9. Report an issue: GitHub.