How To: Get the Length of a Vector

There are some places where you might need to obtain the length (that is, the size or magnitude) of a vector. One example is the speed of a particle. Cycles outputs the velocity of a particle in the Particle Info node but not its speed - the velocity parameter is a vector giving the direction and the speed of the particle.

To obtain the speed, you need to find the size of the velocity vector. You can do this in one of two ways.

1. Using a node tree

This method uses a slightly complicated node tree. Fortunately you don't have to understand it to use it, and we have provided the node tree ready-built for you, as a node group.

To use it, load the group VecLength.cyG and link the vector whose size you want to know to the group Vector input. The size will be found in the Length output.

2. Using an OSL script

We also provide a simple OSL script node to do the same thing. It is used in the same way as the node group.

Which method to use?

The OSL script is simpler and a little faster but has the disadvantage that as with all OSL nodes it won't work if you are rendering on the GPU. Other than that, it doesn't matter which one you use.

Important: the returned value

What the node group and script both do is return a simple numeric value without any units. This is quite different from the data returned by some other nodes. For example, the Particle Info node returns the particle radius in metres. If your scene units are set to centimetres and the radius is 10 cm (which is 0.1 metres), the value returned by Cycles is 0.1, not 10.

If the speed of a particle is 150 cm per second (0.15 metres per second), you might expect that the speed value is returned as 0.15 after getting the length of the Velocity vector. In fact it returns 150. This is rather important if you use the returned value as an input into a node which expects a value from 0 to 1 (e.g. a Color Ramp node).


The script is part of a script pack available on the Cycles 4D website, and the node group is in our node group pack, but for convenience you can download the node tree and the script from this link.