Loading and repathing lightmaps

The example Toy_Excavator.vpb on how to load lightmaps from a given folder or repath lightmaps to a different location can be found in the lightmaps folder of the example directory.

Repathing Lightmaps

This code snippet is extracted from Toy_Excavator.vpb example, it shows how to find nodes with lightmaps and how to use the vrBakeService to repath them.

repath_snippet.py
 1# © 2024 Autodesk, Inc. All rights reserved.
 2
 3# Collect all nodes with a valid base or separate lightmap in subtree   
 4def findNodesWithLightmapsRecursive(node, nodesWithLightmaps):
 5    if type(node) is vrdGeometryNode:   
 6        textureBake = node.getTextureBake()
 7        if textureBake.isValid():
 8            lightmap = textureBake.getLightmap()                  
 9            if lightmap.isValid():
10                nodesWithLightmaps.append(node)
11    for child in node.getChildren():
12        findNodesWithLightmapsRecursive(child, nodesWithLightmaps)
13        
14# These example lines are based on the Toy_Excavator.vpb scene, you will need to adapt the
15# node names and the folder to your scene.        
16nodesWithLightmaps = []
17findNodesWithLightmapsRecursive(vrNodeService.findNode("Toy_Excavator"), nodesWithLightmaps)
18
19# Repathing to a folder with lightmaps for day scenario
20vrBakeService.repathLightmaps(nodesWithLightmaps, getFileIOBaseDir() + "/BakingTextures_Day/")    
21
22# Repathing to a folder with lightmaps for night scenario
23vrBakeService.repathLightmaps(nodesWithLightmaps, getFileIOBaseDir() + "/BakingTextures_Night/")    
24

Loading Lightmaps

This code snippet is extracted from Toy_Excavator.vpb example, it shows how to use the vrBakeService to load lightmaps.

load_snippet.py
 1# © 2024 Autodesk, Inc. All rights reserved.
 2
 3import re
 4import os
 5
 6# Loading lightmaps from given path using the name of the specific node
 7def loadLightmapsByNodeName(nodesWithLightmaps, path):
 8    for node in nodesWithLightmaps:
 9        lightmapPaths = []
10        # Create path to lightmap image
11        baseLightmapPath = path + node.getName() + "_Lightmap" + ".exr"
12        if os.path.isfile(baseLightmapPath):                    
13            lightmapPaths.append(baseLightmapPath)    
14            # Check if there is a corresponding separate lightmap, e.g.
15            separateLightmapPath = path + node.getName() + "_SeparateLightmap" + ".exr"
16            # The separate lightmap is optional. Only add the path if the file exists
17            if os.path.isfile(separateLightmapPath):            
18                lightmapPaths.append(separateLightmapPath)    
19            # For this node load the given files
20            vrBakeService.loadLightmaps([node], lightmapPaths)    
21        else:
22            print("Could not find base lightmap file:" + baseLightmapPath)
23
24# Loading lightmaps from given path using the name of the specific lightmap.
25# This can also be used if multiple nodes share the same name
26def loadLightmapsByLightmapName(nodesWithLightmaps, path):
27    for node in nodesWithLightmaps:
28        lightmapPaths = []
29        baseLightmap = node.getTextureBake().getBaseLightmap()
30        # We can only use the lightmap name if we have a valid lightmap
31        if baseLightmap.isValid():
32            # Create path to lightmap image
33            baseLightmapPath = path + baseLightmap.getName() + ".exr"
34            if os.path.isfile(baseLightmapPath):                    
35                lightmapPaths.append(baseLightmapPath)    
36                # Check if there is a corresponding separate lightmap, same filename but with "_SeparateLightmap"
37                separateLightmapPath = re.sub(r"_Lightmap([0-9]*)\.exr",r"_SeparateLightmap\1.exr",baseLightmapPath)
38                # The separate lightmap is optional. Only add the path if the file exists
39                if os.path.isfile(separateLightmapPath):            
40                    lightmapPaths.append(separateLightmapPath)    
41                # For this node load the given files
42                vrBakeService.loadLightmaps([node], lightmapPaths)    
43            else:
44                print("Could not find base lightmap file:" + baseLightmapPath)
45
46
47# These example lines are based on the Toy_Excavator.vpb scene, you will need to adapt the
48# node names and the folder to your scene.
49
50# Load by node name. Node names have to be unique.
51loadLightmapsByLightmapName(vrNodeService.findNodes("Exhaust_Inner"), getFileIOBaseDir() + "/BakingTextures_Night/")
52
53# Load by lightmap name Node names do not have to be unique, but lightmaps must be present
54loadLightmapsByLightmapName(vrNodeService.findNodes("Exhaust_Inner"), getFileIOBaseDir() + "/BakingTextures_Day/")
55