BabylonJS/Babylon.js · error

Cannot compute length of value ${a}

Error message

Cannot compute length of value ${a}

What it means

The vector Length block's _polymorphicLength computes .length() only for Vector2, Vector3, Vector4 and Quaternion inputs (via the FlowGraphTypes switch). Any other value — a plain number, boolean, string, or Matrix — hits the default case and throws 'Cannot compute length of value ...'. Scalars have no length in this API; use an absolute-value block instead.

Source

Thrown at packages/dev/core/src/FlowGraph/Blocks/Data/Math/flowGraphVectorMathBlocks.pure.ts:58

/**
 * Vector length block.
 */
export class FlowGraphLengthBlock extends FlowGraphUnaryOperationBlock<FlowGraphVector, number> {
    constructor(config?: IFlowGraphBlockConfiguration) {
        super(RichTypeAny, RichTypeNumber, (a) => this._polymorphicLength(a), FlowGraphBlockNames.Length, config);
    }

    private _polymorphicLength(a: FlowGraphVector) {
        const aClassName = _GetClassNameOf(a);
        switch (aClassName) {
            case FlowGraphTypes.Vector2:
            case FlowGraphTypes.Vector3:
            case FlowGraphTypes.Vector4:
            case FlowGraphTypes.Quaternion:
                return (a as Vector3).length();
            default:
                throw new Error(`Cannot compute length of value ${a}`);
        }
    }
}

/**
 * Configuration for normalized vector
 */
export interface IFlowGraphNormalizeBlockConfiguration extends IFlowGraphBlockConfiguration {
    /**
     * If true, the block will return NaN if the input vector has a length of 0.
     * This is the expected behavior for glTF interactivity graphs.
     */
    nanOnZeroLength?: boolean;
}

/**
 * Vector normalize block.
 */

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Feed the block a Vector2/Vector3/Vector4 or Quaternion; for scalars, use an absolute-value math block instead of Length.
  2. Insert the appropriate vector construction block (e.g. Vector3 from x,y,z) if the value must become a vector first.
  3. Fix the mis-wired connection so a vector-typed output feeds the Length block.

Example fix

// before: scalar into Length block -> throws
lengthBlock.value.connect(numberOutput); // number

// after: vector input (or use abs for scalars)
lengthBlock.value.connect(positionVectorBlock.output); // Vector3
Defensive patterns

Strategy: type-guard

Validate before calling

// before Length block runs
const LENGTH_TYPES = ['Vector2', 'Vector3', 'Vector4', 'Quaternion'];
function validLengthInput(v) {
  return v instanceof BABYLON.Vector2 || v instanceof BABYLON.Vector3 ||
         v instanceof BABYLON.Vector4 || v instanceof BABYLON.Quaternion;
}
if (!validLengthInput(lengthInput)) throw new TypeError('Length block input must be a vector or quaternion');

Type guard

function isVectorLike(v): v is BABYLON.Vector2 | BABYLON.Vector3 | BABYLON.Vector4 | BABYLON.Quaternion {
  return v != null && typeof (v as any).length === 'function';
}

Try / catch

try {
  result = lengthBlock.evaluate(context);
} catch (e) {
  if (e.message.startsWith('Cannot compute length of')) {
    // scalar path: use abs instead
    result = Math.abs(Number(lengthInput));
  } else { throw e; }
}

Prevention

When it happens

Trigger: Connecting a scalar (number/FlowGraphInteger), boolean, or Matrix output into a FlowGraphLengthBlock input and executing the block during flow graph evaluation.

Common situations: Wanting the magnitude of a scalar (should use abs, not length); accidentally wiring a matrix output where a vector was expected; a graph where the upstream block type changed from Vector3 to number.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/662ae81f99205d99. Report an issue: GitHub.