Calculating Volume Density with Python?

Does anyone have experience using a Python script to compute volumetric density for an arbitrary mathematical function?

I’ve build a Node Tree to create a Gaussian density distribution (density = constant * exp(-r^2)) for a cube filled with volumetric emission material in Cycles. It works just fine, but it’s a rather indirect way to do something in a few lines of code. I’ve attached the .blend if you care to look. I haven’t gone to the trouble of making a custom node group, but that would be my next step if I continued with the Node route.



density-calculation.blend (464 KB)

I think that you would achieve what you’re looking for using OSL instead:

shader VolumeShaper(
output closure color CL = holdout()
float D = 0.1exp(pow(-length(sin§),2));
CL = emission()

Here’s a screenshot comparing the above script with your node setup and live result:

Thanks tree3d!

You’ve just opened up a whole new world for me! I had seen some discussion of OSL, but those articles were at a very high level. I got the impression that OSL was beyond me. I did a little looking around and found and Gottfried Hofmann’s articles on Blender Cookie.

Do you have any favorite resources or documentation on OSL? I’d love to hear about them.


I’m glad i could help!

Well… Since OSL+CYCLES+BLENDER is kind of a new thing (2 years) i could not find alot of solid information to start with BUT:

Vray supports OSL so you can get useful information from them: - Blendersushi has great info about OSL and also something about porting RENDERMAN OSL scripts!

Above all my true guide is the OSL language spec : -This along with the above and all the help you can get here should set you on the path to OSL enlightenment!

Thanks again! I’m excited to dive into OSL!