Issue creating scenes from collections by keeping same render settings

Hi,
I’m trying to use python to automate the creation of scenes from collections by keeping the same name and settings, but I keep on getting errors…
Can someone please help me to figure it out?
Here is the script:

import bpy

def create_new_scene_for_each_collection_with_empty_type():
  # Get all of the collections in the current scene.
  collections = bpy.context.scene.collection.children

  # Iterate over the collections and create a new scene for each collection.
  for collection in collections:
    # Create a new scene with the EMPTY type.
    scene = bpy.ops.scene.new(type='EMPTY')

    # Set the name of the new scene to be the same as the collection name.
    # Fixed the error by checking if the collection is a dictionary object before accessing the `name` attribute.
    if isinstance(collection, bpy.types.Collection):
      scene.name = collection.name
    else:
      print("The collection does not have a name attribute.")

    # Link the collection to the new scene.
    scene.collection.children.link(collection)

    # Set the active scene to the new scene.
    bpy.context.window.scene = scene

# Create a new scene for each collection with the EMPTY type.
create_new_scene_for_each_collection_with_empty_type()

I only managed to get it work, but it’s not copying the render settings, with this script

import bpy

def create_new_scene_for_each_collection():
  # Get all of the collections in the current scene.
  collections = bpy.context.scene.collection.children

  # Iterate over the collections and create a new scene for each collection.
  for collection in collections:
    # Create a new scene.
    scene = bpy.data.scenes.new(collection.name)

    # Link the collection to the new scene.
    scene.collection.children.link(collection)

    # Set the active scene to the new scene.
    bpy.context.window.scene = scene

# Create a new scene for each collection and name it the same as the collection.
create_new_scene_for_each_collection()

Thank you!!

Just at a glance, on my phone so haven’t run it -

I don’t to see in your code that you’re pulling the current scene data, to determine the render settings and then doing anything with that? Feels like something is missing here.

Also, you posted two blocks of code and said initially that it doesn’t work, but then you said something one does work so I’m a little confused on that…

Hi thorn,

thank you for the response!
The second script works, but only creates the collections with default settings.
With the first script, I tried to copy the settings from the main scene, and that’s where I got stuck…

Hope it’s more clear

Why won’t you copy existing scene and just unlink all unwanted stuff?

import bpy

def create_scene_from_collection(collection, reference_scene):
    
    new_scene = reference_scene.copy()
    new_scene.name = collection.name
    
    for i in new_scene.collection.children.values():
        if i != collection:
            new_scene.collection.children.unlink(i)
        
    for i in new_scene.collection.objects:
        new_scene.collection.objects.unlink(i)
        
    return new_scene

for i in bpy.context.scene.collection.children:
    create_scene_from_collection(i, bpy.context.scene)

Thank you for your help!
I didn’t think about that because I needed to change some settings on scene basis.
I’m still learning Python, so I tried to make it with my limited skills, very basic for now…
But actually your solution did the job better, much appreciated :pray: