How to change the baked lightmap for the terrain from code?

When I copy over a folder that has a scene with a terrain, the terrain data used by the scene points to the old terrain (I.e. editing one terrain, edits the terrain for both scenes). I point this to the new copy via code:

       // copies the folder, with all content included (scene, terrain, lighting data, etc)
       AssetDatabase.CopyAsset("Assets/DefaultSource", "Assets/TestDuplication/" + name);

      // Loads the new scene copy
       var originalScenePath = folder + name + "/scene.unity";
        var scenePath = folder + name + "/" + name + ".unity";
        AssetDatabase.MoveAsset(originalScenePath, scenePath);
        var scene = EditorSceneManager.OpenScene(scenePath);

        // Find the terrain asset object
        var go = GameObject.Find("Terrain");
        var terrainPath = folder + name + "/terrain.asset";
        var terrainData = AssetDatabase.LoadAssetAtPath<TerrainData>(terrainPath);

        // Set the terrain & collider to use the local terrain.
        var tc = go.GetComponent<TerrainCollider>();
        tc.terrainData = terrainData;

        var t = go.GetComponent<Terrain>();
        t.terrainData = terrainData;

This works to successfully transfer the terrain, but I CANNOT find anything to change over the baked lighting map used by the terrain.

I was able to switch the lighting settings in general with this:

        var lightSettingsPath = folder + name + "/SceneDefaultLightingSettings.lighting";
        var lightSettings = AssetDatabase.LoadAssetAtPath<LightingSettings>(lightSettingsPath);
        Lightmapping.lightingSettings = lightSettings;
        var lightingDataPath = folder + name + "/scene/LightingData.asset";
        var lightingData = AssetDatabase.LoadAssetAtPath<LightingDataAsset>(lightingDataPath);
        Lightmapping.lightingDataAsset = lightingData;

the LightSettings updated successfully for the scene, but the LightingDataAsset (which contains a reference to the scene, never updated.) Additionally, when I go into terrain → settings → baked light map, this never gets updated. I could not find any command to affect this in the terrain or terrain data component methods. I looked at the internal methods for terrain to see if I can reflect a method or property to set this, but nothing looks like the right thing. :frowning:

I even tried baking the scene again, but it still baked to the same files used by the first scene as well. :frowning:

Any advice or ideas on this would be awesome.

ok, it looks like I must have had something messed up in my tests. After I did another bake, the BakedLightMap pointed to a local file for it. It also came with a learning element, that the subfolder it hides everything matches the scene name. I hadn’t realized that until now.