Thursday, January 7, 2010

Python and Radiance

Here is the script from todays session:
#======================================================================
# Imports
#======================================================================
import os
import random

#======================================================================
# Functions
#======================================================================
#Function to write a light
def write_material_light(rad_file, name, rgb_e):
    rad_file.write("# Light material\n")
    rad_file.write("void light " + name + "\n")
    rad_file.write("0\n")
    rad_file.write("0\n")
    rad_file.write("3 " +
               str(rgb_e[0])+ " "  +
               str(rgb_e[1]) + " " +
               str(rgb_e[2]) + " " + "\n\n")

def write_material_plastic(rad_file, name, rgb, spec, rough):
    rad_file.write("# Plastic material\n")
    rad_file.write("void plastic " + name + "\n")
    rad_file.write("0\n")
    rad_file.write("0\n")
    rad_file.write("5 " +
               str(rgb[0])+ " "  +
               str(rgb[1]) + " " +
               str(rgb[2]) + " " +
               str(spec) + " " +
               str(rough) + "\n\n")

def write_surf_sphere(rad_file, name, material, position, radius):
    rad_file.write("# Sphere \n")
    rad_file.write(material + " sphere " + name + "\n")
    rad_file.write("0\n")
    rad_file.write("0\n")
    rad_file.write("4 " +
               str(position[0])+ " "  +
               str(position[1]) + " " +
               str(position[2]) + " " +
               str(radius) + "\n\n")
#======================================================================
# Main script
#======================================================================

#Create the scene file
rad_file_1 = open("D:/scene.rad", "w")

write_material_light( rad_file_1, "mat_light_1", (100, 100, 100))
write_material_plastic( rad_file_1, "mat_red", (0.7, 0.05, 0.05), 0.05, 0.05)

for i in range(2):
    position = (random.random() * 5, random.random() * 5, random.random() * 5)
    radius = random.random()
    write_surf_sphere(rad_file_1, "light_"+str(i), "mat_light_1", position, radius)

for i in range(20):
    position = (random.random() * 5, random.random() * 5, random.random() * 5)
    radius = random.random()    
    write_surf_sphere(rad_file_1, "ball_"+str(i), "mat_red", position, radius)

rad_file_1.close()

#Run radiance using oconv and rpict

#os.system("C:/Radiance/bin/oconv D:/scene.rad > D:/scene.oct")
#os.system("C:/Radiance/bin/rpict -vp 10 10 10 -vd -10 -10 -10 -av .5 .5 .5 D:/scene.oct > D:/scene.pic")

#Run radiance using rad

rif_file_1 = open("D:/scene.rif", "w")
rif_file_1.write("ZONE = I  0 5 0 5 0 5\n")
rif_file_1.write("scene = D:/scene.rad\n")
rif_file_1.close()

os.system("C:/Radiance/bin/rad D:/scene.rif")

No comments:

Post a Comment