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.

Thanks!

Attachments

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()
(pow(D,10))0.1color(1,1,1);
return;
}

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 www.openshading.com 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.

Hello!

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: http://docs.chaosgroup.com/display/VRAY3/OSL+Support+in+V-Ray

http://blendersushi.blogspot.pt/2013/10/osl-circle-scatter-and-bombing-part-two.html - Blendersushi has great info about OSL and also something about porting RENDERMAN OSL scripts!

Above all my true guide is the OSL language spec : https://github.com/imageworks/OpenShadingLanguage/blob/master/src/doc/osl-languagespec.pdf -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!