Is it possible to get the length of a ray inside an object with nodes? Is it possible to get the distance a ray travels inside an object with cycles material nodes?



Then map it to a 0-1 range to create a map:



E.g. to adjust the color of a Glass node to make it more transparent where there is less volume between the camera and light:



How can I do this?



The example images above were rendered with the Layer weight node. This only works on spheres.
It is always the way you ask. Ray length is the distance from previous bounce or refraction.

What might be a bit confusing - you have to apply toning to the inner side (backfacing) of your object, because outside rays return distance from other objects - as ray begins from camera, not from light source.

As far as mapping goes - approaches are different - for instance: http://lesterbanks.com/2012/10/using-blenders-raylength-node-to-create-a-re-usable-custom-absorption-shader/