Skip to content

Instantly share code, notes, and snippets.

@Theverat
Last active August 16, 2023 14:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Theverat/0707eb2bd4e99b66bc5777db2793ab67 to your computer and use it in GitHub Desktop.
Save Theverat/0707eb2bd4e99b66bc5777db2793ab67 to your computer and use it in GitHub Desktop.
from time import sleep
import array
# I assume that pyluxcore and all required libraries are in the same
# directory as this Python script. Obviously you can also move it to
# a dedicated module, like I did in BlendLuxCore.
import pyluxcore
WIDTH = 800
HEIGHT = 600
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.6 0.1 0.1
""".format(name=mat_name))
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 a transformation matrix here
transform = None
# 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
obj_name = "test_object"
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)
# A light source is also needed
light_props = pyluxcore.Properties()
light_props.SetFromString("""
scene.lights.test_light.type = sky2
""")
scene.Parse(light_props)
return scene
def build_session(scene):
config_props = pyluxcore.Properties()
config_props.SetFromString("""
renderengine.type = PATHCPU
sampler.type = SOBOL
film.width = {width}
film.height = {height}
###################################
# Imagepipelines
###################################
# Pipeline 0, plugin 0
film.imagepipelines.0.0.type = TONEMAP_LINEAR
film.imagepipelines.0.0.scale = 0.0001
# Pipeline 0, plugin 1
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 = 0.0001
# Pipeline 1, plugin 1
film.imagepipelines.1.1.type = CONTOUR_LINES
film.imagepipelines.1.1.range = 20
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
###################################
# Film outputs
###################################
# This is the pipeline with the tonemapped image as usual
film.outputs.0.type = RGB_IMAGEPIPELINE
film.outputs.0.filename = pipeline_tonemapped.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 = pipeline_contour_lines.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 = IRRADIANCE.exr
film.outputs.3.type = RAYCOUNT
film.outputs.3.filename = RAYCOUNT.exr
film.outputs.4.type = RGB
film.outputs.4.filename = RGB.exr
""".format(width=WIDTH, height=HEIGHT))
renderconfig = pyluxcore.RenderConfig(config_props, scene)
session = pyluxcore.RenderSession(renderconfig)
return session
def main():
# You have to init pyluxcore before you call anything else
pyluxcore.Init()
scene = build_scene()
session = build_session(scene)
session.Start()
sleep(3)
session.GetFilm().SaveOutputs()
# IRRADIANCE has 3 float per pixel
bufferdepth = 3
buffer = array.array("f", [0.0]) * (WIDTH * HEIGHT * bufferdepth)
session.GetFilm().GetOutputFloat(pyluxcore.FilmOutputType.IRRADIANCE, buffer, 0)
# Get the values of the pixel in the middle
x, y = 400, 300
i = (y * WIDTH + x) * bufferdepth
r = buffer[i]
g = buffer[i + 1]
b = buffer[i + 2]
print("Irradiance at x: %d, y: %d is: (%.2f, %.2f, %.2f) (R, G, B)" % (x, y, r, g, b))
session.Stop()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment