Code: Select all
import sys
from time import sleep, time
WIDTH = 800
HEIGHT = 600
DEPTH = 3
sys.path.append('/home/mick/dev/DLO/lib')
import pyluxcore
def build_scene():
scene = pyluxcore.Scene()
# First, the camera
cam_props = pyluxcore.Properties()
cam_props.SetFromString("""
scene.camera.lookat.orig = 2 2 2
scene.camera.lookat.target = 0 0 0
scene.camera.up = 0 0 1
""")
scene.Parse(cam_props)
# An object with a plane mesh and red material
mat_name = "test_material"
obj_props = pyluxcore.Properties()
obj_props.SetFromString("""
scene.materials.{name}.type = matte
scene.materials.{name}.kd = 0.8 0.8 0.8
""".format(name=mat_name))
scene.Parse(obj_props)
# You could pass a transformation matrix here
transform = None
vertices = [
(1, 1, 0),
(1, -1, 0),
(-1, -1, 0),
(-1, 1, 0)
]
faces = [
(0, 1, 2),
(2, 3, 0)
]
mesh_name = "test_mesh"
# You could pass UV coordinates, vertex colors and other stuff here, additionally
scene.DefineMesh(mesh_name, vertices, faces, None, None, None, None, transform)
# Define an object that uses the shape (mesh) and the red material
add_object(scene, "plane", mesh_name, mat_name)
vertices = [
(.01, .01, 0),
(.01, -.01, 0),
(-.01, -.01, 0),
(-.01, .01, 0),
(.01, .01, .3),
(.01, -.01, .3),
(-.01, -.01, .3),
(-.01, .01, .3),
]
faces = [
(0, 1, 4),
(1, 5, 4),
(1, 2, 5),
(2, 6, 5),
(2, 3, 6),
(3, 7, 6),
(3, 0, 7),
(0, 4, 7),
]
mesh_name = "pillar"
scene.DefineMesh(mesh_name, vertices, faces, None, None, None, None, transform)
add_object(scene, "pillar", mesh_name, mat_name)
# A light source is also needed
light_props = pyluxcore.Properties()
for n in ["sun", "sky2"]:
light_props.SetFromString("""
scene.lights.{name}.type = {name}
scene.lights.{name}.gain = {gain}
scene.lights.{name}.turbidity = 3
scene.lights.{name}.dir = {dir}
scene.lights.{name}.turbidity = 3
""".format(name=n, gain="1 1 1", dir="5 3 3"))
scene.Parse(light_props)
return scene
def add_object(scene, obj_name, mesh_name, mat_name):
obj_props = pyluxcore.Properties()
obj_props.Set(pyluxcore.Property("scene.objects." + obj_name + ".shape", mesh_name))
obj_props.Set(pyluxcore.Property("scene.objects." + obj_name + ".material", mat_name))
scene.Parse(obj_props)
def build_session(scene):
config_props = pyluxcore.Properties()
config_props.SetFromString("""
renderengine.seed = 11
renderengine.type = PATHCPU
sampler.type = SOBOL
film.width = """ + str(WIDTH) + """
film.height = """ + str(HEIGHT) + """
# The first plugin: the linear tonemapper multiplies the pixel colors with the scale
film.imagepipelines.0.0.type = TONEMAP_LINEAR
film.imagepipelines.0.0.scale = 5e-5
# The second plugin: gamma correction
film.imagepipelines.0.1.type = GAMMA_CORRECTION
film.imagepipelines.0.1.value = 2.2
# Pipeline 1, plugin 0
#film.imagepipelines.1.0.type = TONEMAP_LINEAR
#film.imagepipelines.1.0.scale = 5e-5
# Pipeline 1, plugin 1
film.imagepipelines.1.1.type = CONTOUR_LINES
film.imagepipelines.1.1.range = 30000
film.imagepipelines.1.1.steps = 10
film.imagepipelines.1.1.zerogridsize = 8
# Pipeline 1, plugin 2
film.imagepipelines.1.2.type = GAMMA_CORRECTION
film.imagepipelines.1.2.value = 2.2
# This is the pipeline with the tonemapped image as usual
film.outputs.0.type = RGB_IMAGEPIPELINE
film.outputs.0.filename = image0.png
# You have to specify which imagepipeline you want as output
film.outputs.0.index = 0
# This is the pipeline with the visible contour lines
film.outputs.1.type = RGB_IMAGEPIPELINE
film.outputs.1.filename = image1.png
# You have to specify which imagepipeline you want as output
film.outputs.1.index = 1
film.outputs.2.type = IRRADIANCE
film.outputs.2.filename = image2.hdr
film.outputs.3.type = RAYCOUNT
film.outputs.3.filename = image3.hdr
film.outputs.4.type = RGB
film.outputs.4.filename = image4.hdr
""")
renderconfig = pyluxcore.RenderConfig(config_props, scene)
session = pyluxcore.RenderSession(renderconfig)
return session
def render(scene):
session = build_session(scene)
session.Start()
startTime = time()
while True:
sleep(1)
elapsedTime = time() - startTime
session.UpdateStats()
stats = session.GetStats()
print("[Elapsed time: %3d/5sec][Samples %4d][Avg. samples/sec % 3.2fM on %.1fK tris]" % (
stats.Get("stats.renderengine.time").GetFloat(),
stats.Get("stats.renderengine.pass").GetInt(),
(stats.Get("stats.renderengine.total.samplesec").GetFloat() / 1000000.0),
(stats.Get("stats.dataset.trianglecount").GetFloat() / 1000.0)))
if elapsedTime > 5.0:
break
session.Stop()
return session
def main():
pyluxcore.Init()
scene = build_scene()
session = render(scene)
session.GetFilm().Save()
import array
import numpy as np
from scipy import stats, signal
import imageio
buffer = array.array("f", [0.0]) * (HEIGHT * WIDTH * DEPTH)
session.GetFilm().GetOutputFloat(pyluxcore.FilmOutputType.IRRADIANCE, buffer, 1)
npa = np.array(buffer).reshape(HEIGHT, WIDTH, DEPTH)[::-1]
print(stats.describe(signal.medfilt(npa.reshape(HEIGHT * WIDTH, DEPTH))))
imageio.imwrite("image5.png", npa)
if __name__ == "__main__":
main()