Add new scene and modify elements keeps editing the previous one

Hello,
I want the create a new scene, link all my objects and change some layers only on the new one and I don’t know what’s wrong with this script:



multilight_scene = bpy.data.scenes.new(name="Multilight")

for obj in context.scene.objects:
    obj.select = True
    bpy.ops.object.make_links_scene(scene=multilight_scene.name)
    

for obj in multilight_scene.objects:
    obj.layers[5] = True


The layers of the objects aren’t set on my new scene but in the old one even if I switch to the new one before with bpy.context.screen.scene=bpy.data.scenes[multilight_scene.name]

Why? I really don’t understand.

Even this script doesn’t modify the new scene’s layers:


for obj in context.scene.objects:
    obj.select = True
    bpy.ops.object.make_links_scene(scene=multilight_scene.name)
    
    bpy.data.scenes[multilight_scene.name].objects[obj.name].layers[5] = True
    # or bpy.data.scenes["Multilight"].objects[obj.name].layers[5] = True

It is kind of deceiving because what is displayed in the UI makes you think that the layers are stored with the object. Objects do have layers but they seem to be derived from a scene base of layers when a scene is changed.

A scene instances an object the same way an object instances a datablock.

Try this on the default scene.


import bpy

ob = bpy.data.objects.get("Cube")
new_scene = bpy.data.scenes.new(name="Multilight")
org_scene = bpy.data.scenes.get("Scene")
layers20 = (False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True)
layers01 = (True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False)

new_scene.objects.link(ob)
for basis in new_scene.object_bases:
    if basis.object.name == ob.name:
        basis.layers = layers20

By changing the base layer for an object in the scene, you effectively change what layer it appears on in that scene only. So the UI tooltip for layers is kind of tricking you.

Thank you. I had no idea how to do that.

What is the goal to store org_scene = bpy.data.scenes.get(“Scene”) in your example?

I did not know how to accomplish this goal until I saw your post and wondered about how layers and scenes are really hooked together.
That is just left over code from experimenting that I should have pulled out. You can ignore it.

you can write this

layers20 = (False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True)

a little bit nicer as:

layers = [False] * 20
layers[19] = True

Thanks for the trick.