Lights#

In this lesson, we’ll explore lights in OpenUSD, schemas belonging to the UsdLux domain. Understanding lights in OpenUSD allows for accurate and realistic lighting in 3D scenes.

What is UsdLux?#

UsdLux is the schema domain that includes a set of light types and light-related schemas. It provides a standardized way to represent various types of lights, such as:

  • Directional lights (UsdLuxDistantLight)

  • Area lights, including

    • Cylinder lights (UsdLuxCylinderLight)

    • Rectangular area lights (UsdLuxRectLight)

    • Disk lights (UsdLuxDiskLight)

    • Sphere lights (UsdLuxSphereLight)

  • Dome lights (UsdLuxDomeLight)

  • Portal lights (UsdLuxPortalLight)

How Does It Work?#

Start by defining light prims within a USD scene. These light primitives consist of scene description for specific light types (e.g., UsdLuxDistantLight for directional lights) and contain attributes that provide comprehensive control over the light’s properties, such as intensity, color, and falloff. These light primitives allow for accurate lighting calculations during rendering.

Working With Python#

Here are a few relevant Python commands for working with USD lights:

1# Import the UsdLux module
2from pxr import UsdLux
3	
4# Create a sphere light primitive
5UsdLux.SphereLight.Define(stage, '/path/to/light')
6
7# Set the intensity of a light primitive
8light_prim.GetIntensityAttr().Set(500)

UsdLux has API schemas that allow you to add light behavior to prims in your scene, so you can also add light properties to meshes and volumes.

Examples#

Example 1: UsdLux and DistantLight#

UsdLux is a USD lighting schema that provides a representation for lights.

One of the schemas in UsdLux is DistantLight. A light is emitted from a distance source along the -Z axis. This is commonly known as a directional light.

 1from pxr import Usd, UsdGeom, UsdLux, UsdShade
 2
 3file_path = "_assets/distant_light.usda"
 4stage: Usd.Stage = Usd.Stage.CreateNew(file_path)
 5
 6world: UsdGeom.Xform = UsdGeom.Xform.Define(stage, "/World")
 7geo_scope: UsdGeom.Scope = UsdGeom.Scope.Define(stage, world.GetPath().AppendPath("Geometry"))
 8box_geo: UsdGeom.Cube = UsdGeom.Cube.Define(stage, geo_scope.GetPath().AppendPath("Cube"))
 9
10# Define a new Scope primitive at the path "/World/Lights" on the current stage:
11lights_scope: UsdGeom.Scope = UsdGeom.Scope.Define(stage, world.GetPath().AppendPath("Lights"))
12
13# Define a new DistantLight primitive at the path "/World/Lights/SunLight" on the current stage:
14distant_light: UsdLux.DistantLight = UsdLux.DistantLight.Define(stage, lights_scope.GetPath().AppendPath("SunLight"))
15
16stage.Save()

Note

The SunLight will not show up in the scene visually but it is displayed in the hierarchy.

Loading your model...
_assets/distant_light.usda
        
    

Example 2: Setting Light Properties#

We’re going to define two new prims, SphereLight and DistantLight, and set a few properties for them.

 1from math import pi
 2from pxr import Gf, Usd, UsdGeom, UsdLux
 3
 4file_path = "_assets/light_props.usda"
 5stage: Usd.Stage = Usd.Stage.CreateNew(file_path)
 6geom_scope: UsdGeom.Scope = UsdGeom.Scope.Define(stage, "/Geometry")
 7cube: UsdGeom.Cube = UsdGeom.Cube.Define(stage, geom_scope.GetPath().AppendPath("Box"))
 8
 9# Define a `Scope` Prim in stage at `/Lights`:
10lights_scope: UsdGeom.Scope = UsdGeom.Scope.Define(stage, "/Lights")
11
12# Define a `Sun` prim in stage as a child of `lights_scope`, called `Sun`:
13distant_light = UsdLux.DistantLight.Define(stage, lights_scope.GetPath().AppendPath("Sun"))
14# Define a `SphereLight` prim in stage as a child of lights_scope called `SphereLight`:
15sphere_light = UsdLux.SphereLight.Define(stage, lights_scope.GetPath().AppendPath("SphereLight"))
16
17# Configure the distant light's emissive attributes:
18distant_light.GetColorAttr().Set(Gf.Vec3f(1.0, 0.0, 0.0)) # Light color (red)
19distant_light.GetIntensityAttr().Set(120.0) # Light intensity
20# Lights are Xformable
21if not (xform_api := UsdGeom.XformCommonAPI(distant_light)):
22    raise Exception("Prim not compatible with XformCommonAPI")
23xform_api.SetRotate((45.0, 0.0, 0.0))
24xform_api = None
25
26# Configure the sphere light's emissive attributes:
27sphere_light.GetColorAttr().Set(Gf.Vec3f(0.0, 0.0, 1.0)) # Light color (blue)
28sphere_light.GetIntensityAttr().Set(50000.0) # Light intensity
29# Lights are Xformable
30if not (xform_api := UsdGeom.XformCommonAPI(sphere_light)):
31    raise Exception("Prim not compatible with XformCommonAPI")
32xform_api.SetTranslate((5.0, 10.0, 0.0))
33
34stage.Save()

Note

The lights will not show up in the scene visually but it is displayed in the hierarchy.

Loading your model...
_assets/light_props.usda
        
    

Key Takeaways#

OpenUSD provides a standardized way to represent various types of lights in a USD scene to ensure consistent light behavior across different applications and renderers. They support different properties and attributes, and advanced features like light filters, IES profiles and linking. Renderers can utilize USD’s lights and materials for accurate lighting calculations.

By understanding how to define and control lights within OpenUSD, developers and 3D practitioners can achieve realistic lighting, enhance visual quality, and unlock new possibilities in their projects.